温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++中怎么实现链表操作

发布时间:2021-07-15 11:42:00 来源:亿速云 阅读:114 作者:Leah 栏目:编程语言

C++中怎么实现链表操作,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

C++链表操作代码示例:

// linklist.cpp : 定义控制台应用程序的入口点。   #include "stdafx.h"   #include "malloc.h"   #include "stdlib.h"   #define NULL 0   #define LEN sizeof(struct student)   struct student   {   long num;   float score;   struct student* next;   };   int n;   struct student* creat()   {   struct student *head;   struct student *p1,*p2;   n=0;   p1=p2=(struct student*)malloc(LEN);   scanf("%ld",&p1->num);   scanf("%f",&p1->score);   head=NULL;   while (p1->num!=0)   {   n++;   if (n==1)   {   head=p1;   }   else   {   p2->next=p1;   }   p2=p1;   p1=(struct student*)malloc(LEN);   scanf("%ld",&p1->num);   if (p1->num==0)   break;   scanf("%f",&p1->score);   }   p2->next=NULL;   return (head);   };   void print(struct student *head)   {   struct student *p;   printf("\n New,These %d records are:\n",n);   p=head;   if (head!=NULL)   {   do   {   printf("%d %5.1f\n",p->num,p->score);   pp=p->next;   }while (p!=NULL);   }   }   struct student* insert(struct student *head,struct student *stud)   {   struct student *p0, *p1, *p2;   p1=head;   p0=stud;   if (head==NULL)   {   head=p0;   p0->next=NULL;//insert into head point   }   else   {   while ((p0->num>p1->num)&&(p1->next!=NULL))   {   p2=p1; //p2 is point to just p1 point to node;   p1p1=p1->next;   }   if (p0->num<=p1->num)   {   if (p1==head)   {   head=p0;//insert into before first node   }   else   {   p2->next=p0;//insert into after point p2   }   p0->next=p1;   }   else   {   p1->next=p0; //insert into after last point   p0->next=NULL;   }   }   n++;   return(head);   };   struct student* del(struct student *head,long num)   {   struct student *p1, *p2;   if (head==NULL)   {   printf("\n list Null!\n");   return (head);   }   p1=head;   while (num!=p1->num&&p1->next!=NULL)   //find num if equal p1->num   {   p2=p1;   p1p1=p1->next;   }   if (num==p1->num)   {   if (p1==head)   head=p1->next;//delete head node because num=head.num   else   p2->next=p1->next;//delete node. node is not head point   printf("delete:%ld\n",num);   n--;   }   else   {   printf("%ld not been found!\n",num);   }   return (head);   };   int _tmain(int argc, _TCHAR* argv[])   {   struct student *head,*end;   head=creat();   print(head);   struct student insertnode;   insertnode.num=3;   insertnode.score=900;   head=insert(head,&insertnode);   print(head);   head=del(head,3);   print(head);   return 0;   }

看完上述内容,你们掌握C++中怎么实现链表操作的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI