欢迎来到天天文库
浏览记录
ID:38212002
大小:27.50 KB
页数:3页
时间:2019-06-07
《C语言单链表的建立》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C语言单链表的建立、输出、插入和删除的算法。LinkList#include#include#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineNULL0typedefcharElemType;typedefstructLNode{ElemTypedata;LNode*next;}LNode,*LinkList;intCreateList(LinkList&L){LNode*s;charc;intflag=1;cout<<"请输入线性表中的元
2、素值('#'结束):"<>c;if(c!='#'){s=(LNode*)malloc(sizeof(LNode));//为读入的字符分配存储空间s->data=c;s->next=L->next;L->next=s;}elseflag=0;}returnOK;}intShowList(LinkListL){LinkListp;inti=0;p=L->next;cout<<"链表逻辑结构如下:"<next)cout<data<<"->";elsecou
3、t<data;p=p->next;}cout<next&&jnext;j++;}if(j!=i){cout<<"插入位置不合理"<4、eof(LNode));q->data=e;q->next=p->next;p->next=q;returnOK;}intDeleteList(LinkList&L,inti,intl){LinkListp,q;p=Locate(L,i-1);if(p=NULL){cout<<"删除位置不合法!";cout<next==NULL){cout<<"删除位置不合法!";cout<next;p->next=p->next->next;free(q);}}}voidmain(){LinkLis5、tL;chare;inti;L=(LinkList)malloc(sizeof(LNode));L->next=NULL;CreateList(L);ShowList(L);cout<<"输入需要插入点的位置:";cin>>i;cout<>e;cout<
4、eof(LNode));q->data=e;q->next=p->next;p->next=q;returnOK;}intDeleteList(LinkList&L,inti,intl){LinkListp,q;p=Locate(L,i-1);if(p=NULL){cout<<"删除位置不合法!";cout<next==NULL){cout<<"删除位置不合法!";cout<next;p->next=p->next->next;free(q);}}}voidmain(){LinkLis
5、tL;chare;inti;L=(LinkList)malloc(sizeof(LNode));L->next=NULL;CreateList(L);ShowList(L);cout<<"输入需要插入点的位置:";cin>>i;cout<>e;cout<
此文档下载收益归作者所有