我真是日了

尾插法(无头结点头指针head里直接就有内容的)创建链表

#include<iostream>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<malloc.h>
using namespace std;
struct ListNode{
   int number;
   struct ListNode *next;

};
ListNode* CreateList(ListNode* head){
          ListNode *p,*q;
          int SIZE,i=0;
          head=NULL;
          q=head;
          cin>>SIZE;//输入链表元素的个数
          while(i<SIZE){
             p=(ListNode*)malloc(sizeof(ListNode));
             cin>>p->number;
             if(head==NULL){ //空的一团 漂浮着 无实体 所以出现第一个新元素的时候直接等过去就行 head=p; 之后再是next
                head=p;
                q=p;
             }
             else{
                 q->next=p;
                 q=p;
             }
             i++;
          }
          q->next=NULL;

          return head;
     /*整个链表输入过程不用换行*/
}
void ShowList(ListNode *head){

     while(head!=NULL){
        cout<<head->number<<" ";
        head=head->next;
     }
     cout<<endl;
}
int main(){

   ListNode *head1,*head2;
   head1=CreateList(head1);
   head2=CreateList(head2);
   //链首指针要在主函数里
   ShowList(head1);
   ShowList(head2);
   return 0;
}

真他妈难以置信我这一个学期过了弄绷链表连接了  之前写的代码全他妈不对

猜你喜欢

转载自www.cnblogs.com/yundong333/p/10481094.html
今日推荐