C++中怎么實現(xiàn)鏈表操作,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
成都創(chuàng)新互聯(lián)是專業(yè)的臨清網(wǎng)站建設公司,臨清接單;提供成都網(wǎng)站設計、網(wǎng)站制作,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行臨清網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
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; }
看完上述內(nèi)容,你們掌握C++中怎么實現(xiàn)鏈表操作的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!