新学链表,高手帮忙找找错~~~
程序代码:
/*设计一个单向链表。从标准输入读取若干整数,建立链表, 每次读入的数放入链表结尾。当用户输入 0时,结束链表的建立工作。然后从前往后依次输出链表节点中的内容*/ #include<iostream> using namespace std; typedef struct node { int elem; struct node *next; }Node; int main() { Node *head, *p, *last, *q; head = last = NULL; p = new Node; if(p == NULL) cout<<"there is no space"<<endl; cout<<"please input integers to build the link:"; cin>>p->elem; p->next = NULL; if(p->elem == 0) { head = last = p; } else while(p->elem != 0) { last->next = p; p->next = NULL; last = p; p = new Node; if(p == NULL) cout<<"there is not space"<<endl; cin>>p->elem; } q = head; cout<<"Link elements:"; while(q != NULL) { cout<<q->elem<<" "; q = q->next; } delete p; return 0; }运行不了,为什么啊?