这个链表程序出现段错误,是什么原因,如何解决? C语言程序使用链表结构出现段错误
c\u8bed\u8a00\u94fe\u8868\u6bb5\u9519\u8bef41\u884c\u6539\u6210if(!pdk->num)\u8bd5\u8bd5\u3002
/* \u5934\u6587\u4ef6\u7684\u5305\u542b\u5e94\u5199\u6e05\u695a */#include#include /* \u539f\u6765\u7684\u5b9a\u4e49\u65b9\u5f0f\u867d\u7136\u53ef\u4ee5\uff0c\u4f46\u662f\u6bd4\u8f83\u5947\u602a */typedef struct element{int x;int hash;int position;struct element* next;}S_HASH;typedef S_HASH* Hash;void Insert(int hash,int position,int x,Hash P){Hash tmp = NULL;/* \u672a\u521d\u59cb\u5316\uff0c\u4f1a\u5bfc\u81f4\u63a5\u4e0b\u6765\u7684if\u8bed\u53e5\u5224\u65ad\u65e0\u6548 */if(tmp == NULL){printf("Out of Space!");}else{tmp->x=x;tmp->hash=hash;tmp->position=position;tmp->next=P->next;P->next=tmp;}}Hash FindPosition(int hash,int position,int x,Hash L){int count;Hash P;count=position-hash;P=L;if(count>0){while(count>=0){if(P->position>=hash){P=P->next;count--;}else if(x>P->x){P=P->next;}if(P->next==NULL) break;}}else if(count==0){while(x>P->x)P=P->next;}else{count+=11;while(count>0){if(P->position>position){P=P->next;count--;}else if(x>P->x){P=P->next;}if(P->next==NULL)break;}}return P;}void Printresult(Hash L){while(L!=NULL){printf("%d",L->x);L=L->next;}}int main(void){int N,i,x,hash;Hash P,L,tmp;scanf("%d",&N);scanf("%d",&x);L = (Hash)malloc(sizeof(struct element));/* malloc\u65f6\u9700\u6307\u660e\u6570\u636e\u7c7b\u578b */if(L==NULL){printf("Out of Space!");}else{L->x=x;L->hash=x%N;L->position=0;L->next=NULL;}for(i=1;inext;/* \u4e0d\u6e05\u695a\u4e3a\u4ec0\u4e48\u4f1a\u52a0\u4e0a"" */L->next=NULL;while(P!=NULL){tmp=P->next;free(P);P=tmp;}free(L);}}/* \u7f3a\u5c11\u4e00\u4e2a} */\u53ea\u6539\u4e86\u7f16\u8bd1\u9519\u8bef\uff0c\u903b\u8f91\u4e0a\u6709\u65e0\u9519\u8bef\u6ca1\u770b
//代码段1:
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int no;
struct student *next;
}student;
int main()
{
student *head=NULL,*p1=NULL,*p2=NULL,*p=NULL;
int oneItem;
printf("Input No:");
scanf("%d",&oneItem);
if(oneItem==0) //先检查输入的号码是否为0
{
printf("
没有学生数据.
");
return 0;
}
//确认了输入号码不为0,再分配动态内存,保存数据
p1=(student *)malloc(sizeof(student));
if(p1==NULL)
{
printf("
分配动态内存出错.
");
return 0;
}
p1->no=oneItem;
p1->next=NULL; //必须让p1->next等于NULL
head=p1; //head指向第1个节点
p2=p1; //p2指向当前节点,p1是新节点
while(1)
{
printf("Input No:");
scanf("%d",&oneItem);
if(oneItem==0) //先检查输入的号码是否为0
{
break;
}
//确认了输入号码不为0,再分配动态内存,保存数据
p1=(student *)malloc(sizeof(student));
if(p1==NULL)
{
printf("
分配动态内存出错.
");
return 0;
}
p1->no=oneItem;
p1->next=NULL;
p2->next=p1; //将新节点p1加在p2的末尾
p2=p1; //p2指向当前节点
}
p=head;
while(p) //语句while(p)就是相当于while(p!=NULL)
{
printf("%d
",p->no);
p=p->next;
}
return 0;
}
//代码段2的问题:
......
while(p1->no)
{
n++;
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(student *)malloc(sizeof(student));
//新节点p1->next没有设定为NULL
printf("Input Student's No.:");
scanf("%d",&p1->no);
//假设链表已经有2个学生数据,此时n=2,
//p1提前分配了内存,然后,屏幕输入数字0,此时p1->no等于0,
//n仍然是2个数据,但是,实际有3个数据,最后的数据是0,
//退出了while(p1->no)循环之后,while(n)循环只显示2个数据,
//最后那个节点p1成为了"游离"的数据,没有加入链表里.
}
......
while(n)
{
printf("
%d",p->no);
p=p->next;
n--;
}
......
绛旓細//浠g爜娈1锛#include<stdio.h>#include<stdlib.h>typedef struct student{ int no; struct student *next;}student;int main(){ student *head=NULL,*p1=NULL,*p2=NULL,*p=NULL; int oneItem; printf("Input No:"); scanf("%d",&oneItem); if(oneItem==0) //鍏...
绛旓細杩欎竴娈典富鍑芥暟浠g爜涓殑create()鍑芥暟鎵杩斿洖鐨勫煎苟娌℃湁璧嬬粰缁撴瀯鍙橀噺head,鎵浠ヤ箣鍚庣殑鍑芥暟鎵璁块棶鐨勭粨鏋勫彉閲忔槸涓嶅瓨鍦ㄧ殑锛屾墍浠ヤ細鍑虹幇娈甸敊璇紝鎻愮ず娈甸敊璇富瑕佹槸鍚庨潰璋冪敤鐨刦ree()涓殑鍙橀噺骞朵笉瀛樺湪绌洪棿鍦板潃锛屾墍浠ユ彁绀烘閿欒銆
绛旓細1銆佸湪杩涘叆鍑芥暟鏃讹紝濡傛灉head==null锛屽垯浼氬嚭鐜版閿欒锛屽鍔犻檺鍒讹紝濡傛灉head==null 灏眗eturn銆2銆乻truct student 瀵硅薄涓瀹氳鎶婃垚鍛榥ext鍒濆鍖栦负null銆3銆侀摼琛╯truct student鐨勬瘡涓垚鍛樿涔堟槸鍏ㄥ眬鍙橀噺锛岃涔堟槸鍔ㄦ佸垎閰嶇┖闂寸殑锛屼笉鑳芥槸鍑芥暟鍐呯殑灞閮ㄥ彉閲忋3銆佷笉鐭ラ亾浣爌1鐨勭┖闂存湁娌℃湁閲婃斁鎺夛紝涓嶇劧浼氶犳垚鍐呭瓨娉勬紡 ...
绛旓細缁撴瀯浣撴病鏈変竴涓垵濮嬪寲鐨勫嚱鏁帮紝鎵浠ead 涔熸病鏈夋槑纭殑鎸囧悜 灏辨槸璇磆ead鐜板湪鏄┖鐨 鎵浠ヨ闂畁ext閭i噷鏃跺欎細鍑洪敊 闈炴硶璁块棶 寤鸿鍐欎竴涓垵濮媓ead鐨勫嚱鏁
绛旓細1銆佸彉閲忎竴瀹氳鍒濆鍖 2銆佷綘鐨凬ode *create(Node *head,int n)鍑芥暟鏄湁杩斿洖鍊硷紝浣犳病杩斿洖锛屾寜浣犵殑鎰忔濆簲璇ユ槸瑕佽繑鍥瀐ead 3銆乨isplay锛堬級璋冪敤鐨刪ead鍙傛暟杩樻槸鍒濆鍊硷紝鎵浠ヨ繖涓鍙 = p -> next娈甸敊璇 4銆佽浣犲垵濮嬪寲鍙橀噺鐗瑰埆鏄寚閽坔ead = NULL锛沜reat鍑芥暟杩斿洖head缁檇isplay灏監K浜 ...
绛旓細/* 鏈垵濮嬪寲锛屼細瀵艰嚧鎺ヤ笅鏉ョ殑if璇彞鍒ゆ柇鏃犳晥 */if(tmp == NULL){printf("Out of Space!");}else{tmp->x=x;tmp->hash=hash;tmp->position=position;tmp->next=P->next;P->next=tmp;}}Hash FindPosition(int hash,int position,int x,Hash L){int count;Hash P;count=position-hash;...
绛旓細malloc浜х敓娈甸敊璇鍙兘鏄涓嬪師鍥狅細1銆佹寚閽堥潪娉曪紝姣斿浣跨敤娌℃湁鍒濆鍖栫殑鎸囬拡(娌℃湁涓烘鎸囬拡鎸囧悜鐨勫璞″垎閰嶇┖闂)锛屾垨鐫Free鎺変箣鍚庡啀娆′娇鐢ㄣ2銆佹暟缁勮闂秺鐣,璁块棶鐨勫厓绱犱笅鏍囪秴杩囨暟缁勫洿闀 3銆佺紦瀛樻孩鍑猴紝瀵逛簬杩欑while{do}鐨绋嬪簭锛岃繖涓闂鏈瀹规槗鍙戠敓锛屽姝printf鎴栫潃strcat鏈夊彲鑳藉皢鏌愪釜buff濉弧锛屾孩鍑猴紝鎵浠ユ瘡娆...
绛旓細newDate) return 1; if(head==NULL) { head=(struct date *)malloc(sizeof(struct date)); if(!head) return 1; head->next=newDate; } else tail->next=newDate; tail=newDate; } scanf("%d",&num);...
绛旓細鍦↖nQueue閲岄潰锛屽彧鏇存柊浜唕ear锛屾病鏈夋洿鏂癴ront tempNode -> m_next = queue -> m_rear; queue -> m_rear = tempNode;OutQueue閲岄潰灏变笉璇翠簡銆傛渶澶х殑闂鏄細浣犵殑rear鍜宖ront涓涓寚鍚戝熬涓涓寚鍚戝ご锛屼絾鏄閾捐〃鑺傜偣鏄崟鍚戠殑锛宺ear鎸囧悜鐨勮妭鐐圭殑next鏄線鍓嶇殑锛宖ront鐨刵ext鏄線鍚庣殑锛屼綘璁╁彧鏈変竴涓...
绛旓細while(o){o=o->next;printf("%d ",o->data);} 椤哄簭鍙嶄簡锛屽綋o涓烘渶鍚庝竴涓妭鐐规椂锛宱=o->next鍚巓=NULL;鎵浠rintf("%d",o->data)浼鍑虹幇娈甸敊璇紝鍋滄宸ヤ綔