制造链表会有点麻烦,有些人就会直接用到数组,但是它也有好处。如数组的大小在定义时要事先定,不能在程序中进行调整,这样一来,在程序设计中针对不同问题有时需要30个大小的数组,有时需要50个数组的大小,难于统一。我们只能够根据可能的最大需求来定义数组,常常会造成一定存储空间的浪费。
以下是代码:
#includeusing namespace std; struct node{ int data; node *next; }; node *head,*end,*p; int main(){ int x; cin>>x; head=new node; end=head; while(x!=-1){ p=new node; p->data=x; p->next=NULL; end->next=p; end=p; cin>>x; } p=head->next; while(p->next!=NULL){ cout< data< next; } cout< data; } //看了我的代码就点个赞呗,不多,就一个赞!



