c简单的链表错误及改正
生活随笔
收集整理的這篇文章主要介紹了
c简单的链表错误及改正
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
????以下代碼運行時崩潰:
#include <iostream>
using namespace std;
struct node
{
int num;
struct node * next;
}; node * creat()
{
node * head=NULL;node*HEAD=head;
cout<<"輸入數字,組成鏈表,0退出"<<endl;
int a;
while(cin>>a&&a!=0)
{
node * add=new node;
add->num=a;
head->next=add;
head=head->next;
}
head->next=NULL;
return HEAD;
}
int main()
{
node* head=creat();
while(head->next!=NULL)
{
cout<<head->next->num;
head=head->next;
}
原因:
最初你的head=null,并沒有指向一個節點,head->next沒有意義。 解決:head = new head; 動態分配一個頭結點轉載于:https://my.oschina.net/u/264056/blog/79661
總結
以上是生活随笔為你收集整理的c简单的链表错误及改正的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到别人怀孕了怎么回事啊
- 下一篇: 梦到前男友死了什么预兆