首页 > 其他 > 详细

我真是日了

时间:2019-03-06 10:22:38      阅读:123      评论:0      收藏:0      [点我收藏+]

尾插法(无头结点头指针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;
}

 

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

我真是日了

原文:https://www.cnblogs.com/yundong333/p/10481094.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!