if中可以賦值給head的理由很簡單。
創(chuàng)新互聯(lián)建站專注于政和企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),商城網(wǎng)站開發(fā)。政和網(wǎng)站建設(shè)公司,為政和等地區(qū)提供建站服務(wù)。全流程按需搭建網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
因?yàn)?if(PTScount(head) == 0)
if判斷的就是看它是不是 第一個(gè)元素。
如果是第一個(gè)元素,自然直接將ins賦值給head。
head所代表的就是第一個(gè)元素。
如果到了else這里,那么很明顯就不是第一個(gè)元素了。
那這個(gè)時(shí)候肯定就不可以直接復(fù)制給head了呀,因?yàn)閔ead可是代表第一個(gè)元素呀。
所以,你這是肯定改不了的。
主函數(shù)里調(diào)用就像寫函數(shù)定義一樣,比如調(diào)用創(chuàng)建表的,就這樣:
#include?stdio.h
struct?Linklist?{?
...
};
typedef?Linklist*?LinkList;
int?CreateList(LinkList?LstMe)?{
...
}??
int?main()?{
LinkList?LstDemo?=?(LinkList)?malloc?(sizeof(Linklist));
CreateList(LstDemo);?//?調(diào)用建表
free?(LstDemo);
return?0;
}
1、添加頭文件"stdio.h"
2、struct student * creat(void),但是你的main函數(shù)中返回接收卻是用的int*類型。
3、主函數(shù)main應(yīng)當(dāng)明確聲明為void main()
4、最為嚴(yán)重的是:struct student * creat(void) 函數(shù)體中使用了局部變量struct student *head; struct student *p1,*p2; 但是函數(shù)結(jié)束的地方卻要返回這些指針,因此返回的值是無效數(shù)據(jù)。返回時(shí),已經(jīng)不再作用域了,是無效的空間。建議把這些數(shù)據(jù)當(dāng)作輸入?yún)?shù),指針類型的。就可以正確的接收分配的struct student空間以及指針了。
5、struct student * creat(void) 函數(shù)體中,臨時(shí)指針*p1、*p2再函數(shù)返回之前應(yīng)當(dāng)設(shè)置為NULL,避免因?yàn)榫植孔兞康淖饔糜蚪Y(jié)束導(dǎo)致相關(guān)的空間被清除。
總之,我建議把struct student * creat(void)定義修改為:
void creat(struct student **head).
以上內(nèi)容經(jīng)過調(diào)試,可以使用。
==================我的程序,經(jīng)過完整的調(diào)試
#include "stdafx.h"
#include "malloc.h"
#include "stdio.h"
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
void creat(struct student **head) /*定義函數(shù),此函數(shù)帶回一個(gè)指向鏈表頭的指針*/
{
struct student *p1,*p2;
n=0;
do
{
p1=(struct student *)malloc(LEN); /*開辟一個(gè)新單元*/
p1-next=NULL;
scanf("%ld,%f",p1-num,p1-score);
if(p1-num==0)
break;
n++;
if(n==1)
*head=p1;
else
p2-next=p1;
p2=p1;
} while(p1-num!=0) ;
p1=NULL;
p2=NULL;
}
void main()
{
struct student *p;
creat(p);
if(p!=NULL)
do
{
printf("%ld %5.1f\n",p-num,p-score);
p=p-next;
}while(p!=NULL);
flushall(); //清除鍵盤緩沖區(qū),避免輸入混淆
getchar(); //等待鍵盤任意輸入,以便觀察運(yùn)算結(jié)果
}
看我的回答怎么樣?
網(wǎng)站欄目:c語言鏈?zhǔn)浇Y(jié)構(gòu)定義函數(shù) c語言鏈?zhǔn)骄幊?/a>
當(dāng)前路徑:http://chinadenli.net/article40/hihieo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站策劃、網(wǎng)站維護(hù)、微信小程序、、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)