帮忙看一下那里有问题,数据结构的创建,插入和删除

[复制链接]
查看11 | 回复9 | 2011-9-19 01:17:30 | 显示全部楼层 |阅读模式
#includestdio.h#includemalloc.htypedefintElemType;typedefstructNode{ElemTypedata;structNode*next;}Node,*LinkList;InitList(LinkList*L){*L=(Linklist)malloc(sizeof(Node));(*L)-next=NULL;return1:}voidCreateFromHead(LinkListL){Node*s;charc:intflag=1:while(flag){c=getchar();if(c!=\'$\'){s=(Node*)malloc(sizeof(Node));s-data=c;s-next=L-next;L-next=s;}elseflag=0;}}intDelList(LinkListL,inti,ElemType*e){Node*p,*r;intk;p=L;k=0;while(p-next!=NULLi-1){p=p-next;k=k1:}if(k!=i-1){printf(\"删除节点的位置i不合理!\");return0;}r=p-next;p-next=p-next-next;*e=r-data;free(r);return1;}intmain(){inte;LinkListL1;InitList(L1);ListInsert_L(L1,1,5);printf(\"%d\\n\",L1-next-data);ListDele_L(L1,1,e);printf(\"%d\",e);return1;}
回复

使用道具 举报

千问 | 2011-9-19 01:17:30 | 显示全部楼层
我是杨剑,你是哪个班的学生啊
回复

使用道具 举报

千问 | 2011-9-19 01:17:30 | 显示全部楼层
插入不对吧InitListInitList(LinkListL){L=(Linklist)malloc(sizeof(Node));L-next=NULL;return1:}
回复

使用道具 举报

千问 | 2011-9-19 01:17:30 | 显示全部楼层
#includeiostreamusingnamespacestd;typedefstructnode{chardata;structnode*next;}link;link*get(link*l,inti){link*p;intj=0;p=l;while((ji)next!=NULL)){p=p-next;j;}if(j==i)returnp;elsereturnNULL;}link*ins(link*l,charch,inti){link*p,*s;p=get(l,i-1);if(p==NULL)cout\"输入有误\"endl;else{s=(link*)malloc(sizeof(link));s-data=ch;s-next=p-next;p-next=s;}returnl;}link*find(link*l,charch){link*p;inti=0;intj=0;p=l;while(p!=NULL){i;if(p-data!=ch)p=p-next;else{cout\"您查找的数据在第\"i-1\"个位置.\"endl;j=1;p=p-next;}}if(j!=1)cout\"您查找的数据不在线性表中.\"endl;returnl;}link*del(link*l,inti){link*p,*s;p=get(l,i-1);if(p==NULL)cout\"输入有误\"endl;else{s=p-next;p-next=s-next;free(s);}returnl;}link*add(link*l){link*p,*s;cout\"请输入一串单字符数据,以*结束!\"endl;charch;link*HEAD;link*R,*P,*L;HEAD=(link*)malloc(sizeof(link));HEAD-next=NULL;R=HEAD;getchar();ch=getchar();while(ch!=\'*\'){P=(link*)malloc(sizeof(link));P-data=ch;P-next=NULL;R-next=P;R=R-next;getchar();ch=getchar();}L=HEAD;cout\"当前输入的线性表为:\"endl;P=L;P=P-next;if(L!=NULL)do{coutP-data\"\";P=P-next;}while(P!=NULL);coutendl;p=l;while(p-next!=NULL)p=p-next;s=L;p-next=s-next;p=l;returnl;}link*print(link*l){inti,k;charch;link*p,*q;cout\"当前线性表为:\"endl;p=l;p=p-next;if(l!=NULL)do{coutp-data\"\";p=p-next;}while(p!=NULL);coutendl;cout\"请选择您要的操作:\";cout\"1、插入\";cout\"2、查找\";cout\"3、删除\";cout\"4、合并\";cout\"0、退出\";coutendl;cink;if(k==1){cout\"请输入您要插入的数据值:\";cinch;cout\"请输入您要插入的位置:\";cini;p=ins(l,ch,i);q=print(l);}elseif(k==2){cout\"请输入您要查找的数据值:\";cinch;p=find(l,ch);q=print(l);}elseif(k==3){cout\"请输入您要删除的数据的位置:\";cini;p=del(l,i);q=print(l);}elseif(k==4){p=add(l);q=print(l);}elseif(k==0);else{cout\"输入错误!\"endl;q=print(l);}returnl;}intmain(){cout\"请输入一串单字符数据,以*结束!\"endl;charch;//link*head;link*r,*p,*q,*l;l=(link*)malloc(sizeof(link));l-next=NULL;r=l;ch=getchar();//getchar();while(ch!=\'*\'){p=(link*)malloc(sizeof(link));p-data=ch;p-next=NULL;r-next=p;r=r-next;ch=getchar();//getchar();}//l=head;q=print(l);return0;}c语言的#includestdio.h#includemalloc.h#defineN8typedefstructnode{intdata;structnode*next;}node;node*createsl(){node*p,*s,*h;intj=1,x;p=s=h=(node*)malloc(sizeof(node));h-next=NULL;printf(\"pleaseinputthedatatocreatethelist,endwith-1or%dnupmbers\\n\",N);while(x!=-1=N){printf(\"number%d:\",j);scanf(\"%d\",s=(node*)malloc(sizeof(node));s-data=x;if(h-next==NULL)h-next=s;elsep-next=s;p=s;j;}p-next=NULL;returnh;}intaccess(node*h,inti){node*p;intj=1;p=h-next;while(p!=NULL){if(p-data==i)break;p=p-next;j;}if(p!=NULL){printf(\"findthenumberinposition:%d\\n\",j);return(p-data);}else{printf(\"can\'tfindthenumberinthelist!\\n\");return-1;}}voidinsertsl(node*h,inti){node*p,*t;intj=1;p=h-next;;while(p-next!=NULL){p=p-next;j;}t=(node*)malloc(sizeof(node));t-data=i;t-next=p-next;p-next=t;printf(\"insertsuccessinposition%d\\n\",j1);}voiddeletesl(node*h,inti){node*p,*s,*q;intj=1;p=h;while(p-next!=NULL){q=p-next;if(q-data==i)break;p=p-next;j;}if(p-next==NULL){printf(\"Can\'tfindthenumberyouwanttodelete.\\n\");return;}else{s=p-next;p-next=s-next;free(s);printf(\"deletesuccessinposition%d\\n\",j1);}}voidprint(node*h){printf(\"\\nprintallthedatainthelist:\");node*s;s=h-next;if(s!=NULL){while(s!=NULL){printf(\"%d\",s-data);s=s-next;}}elseprintf(\"thelistisempty!%d\");printf(\"\\n\");}intmain(){node*p;inta;p=createsl();printf(\"\\nyouneedfindthenumber:\\n\");scanf(\"%d\",access(p,a);printf(\"\\npleaseinputthenumberyouwanttoinsert:\\n\");scanf(\"%d\",insertsl(p,a);printf(\"\\npleaseinputthenumberyouwanttodelete:\\n\");scanf(\"%d\",deletesl(p,a);print(p);return0;}
回复

使用道具 举报

千问 | 2011-9-19 01:17:30 | 显示全部楼层
#includestdio.h#includemalloc.htypedefstructNode//定义节点{intdata;//可以为其他类型structNode*next;}List;voidcreate(structNode*inti;L=(List*)malloc(sizeof(List));L-next=NULL;for(i=0;in;i){s=(List*)malloc(sizeof(List));s-data=a;s-next=L-next;L-next=s;}}intinsert(List*List*p=L,*s;while(ji-1p=p-next;}if(p==NULL)return0;else{s=(List*)malloc(sizeof(List));s-data=e;s-next=p-next;p-next=s;return1;}}intdelete(List*List*p=L,*q;while(ji-1p=p-next;}if(p==NULL)return0;else{q=p-next;if(q==NULL)return0;e=q-data;p-next=q-next;free(p);return1;}}
回复

使用道具 举报

千问 | 2011-9-19 01:17:30 | 显示全部楼层
InitList(LinkList*L)函数没有返回类型ListDele_L(L1,1,e);intDelList(LinkListL,inti,ElemType*e)最后一个变量一个是int,一个是指针函数名也不正确,还有函数不知道是在干什么??
回复

使用道具 举报

千问 | 2011-9-19 01:17:30 | 显示全部楼层
调试成功后,,多加点分就行了,,,#includeiostreamusingnamespacestd;typedefstructnode{chardata;structnode*next;}link;link*get(link*l,inti){link*p;intj=0;p=l;while((ji)next!=NULL)){p=p-next;j;}if(j==i)returnp;elsereturnNULL;}link*ins(link*l,charch,inti){link*p,*s;p=get(l,i-1);if(p==NULL)cout\"输入有误\"endl;else{s=(link*)malloc(sizeof(link));s-data=ch;s-next=p-next;p-next=s;}returnl;}link*find(link*l,charch){link*p;inti=0;intj=0;p=l;while(p!=NULL){i;if(p-data!=ch)p=p-next;else{cout\"您查找的数据在第\"i-1\"个位置.\"endl;j=1;p=p-next;}}if(j!=1)cout\"您查找的数据不在线性表中.\"endl;returnl;}link*del(link*l,inti){link*p,*s;p=get(l,i-1);if(p==NULL)cout\"输入有误\"endl;else{s=p-next;p-next=s-next;free(s);}returnl;}link*add(link*l){link*p,*s;cout\"请输入一串单字符数据,以*结束!\"endl;charch;link*HEAD;link*R,*P,*L;HEAD=(link*)malloc(sizeof(link));HEAD-next=NULL;R=HEAD;getchar();ch=getchar();while(ch!=\'*\'){P=(link*)malloc(sizeof(link));P-data=ch;P-next=NULL;R-next=P;R=R-next;getchar();ch=getchar();}L=HEAD;cout\"当前输入的线性表为:\"endl;P=L;P=P-next;if(L!=NULL)do{coutP-data\"\";P=P-next;}while(P!=NULL);coutendl;p=l;while(p-next!=NULL)p=p-next;s=L;p-next=s-next;p=l;returnl;}link*print(link*l){inti,k;charch;link*p,*q;cout\"当前线性表为:\"endl;p=l;p=p-next;if(l!=NULL)do{coutp-data\"\";p=p-next;}while(p!=NULL);coutendl;cout\"请选择您要的操作:\";cout\"1、插入\";cout\"2、查找\";cout\"3、删除\";cout\"4、合并\";cout\"0、退出\";coutendl;cink;if(k==1){cout\"请输入您要插入的数据值:\";cinch;cout\"请输入您要插入的位置:\";cini;p=ins(l,ch,i);q=print(l);}elseif(k==2){cout\"请输入您要查找的数据值:\";cinch;p=find(l,ch);q=print(l);}elseif(k==3){cout\"请输入您要删除的数据的位置:\";cini;p=del(l,i);q=print(l);}elseif(k==4){p=add(l);q=print(l);}elseif(k==0);else{cout\"输入错误!\"endl;q=print(l);}returnl;}intmain(){cout\"请输入一串单字符数据,以*结束!\"endl;charch;//link*head;link*r,*p,*q,*l;l=(link*)malloc(sizeof(link));l-next=NULL;r=l;ch=getchar();//getchar();while(ch!=\'*\'){p=(link*)malloc(sizeof(link));p-data=ch;p-next=NULL;r-next=p;r=r-next;ch=getchar();//getchar();}//l=head;q=print(l);return0;}
回复

使用道具 举报

千问 | 2011-9-19 01:17:30 | 显示全部楼层
#includestdio.h#includestdlib.htypedefstructLNode{intdata;structLNode*next;}LNode,*Llist;LNode*creat_head();//创建一个空表voidcreat_list(LNode*,int);//创建一个长度为n的线性链表voidinsert_list(LNode*,int,int);//插入一个元素intdelete_list(LNode*,int);//删除一个元素main(){LNode*head,*p;intn;intx,i;intb;clrscr();head=creat_head();printf(\"n=\");scanf(\"%d\",creat_list(head,n);for(p=head-next;p!=NULL;){printf(\"%d\",p-data);p=p-next;}printf(\"\\n*****************************************************\\n\");printf(\"x=\");scanf(\"%d\",printf(\"\\ninserti=\");scanf(\"%d\",insert_list(head,x,i);for(p=head-next;p!=NULL;){printf(\"%d\",p-data);p=p-next;}printf(\"\\n*********************************************************\\n\");printf(\"deletei=\");scanf(\"%d\",b=delete_list(head,i);for(p=head-next;p!=NULL;){printf(\"%d\",p-data);p=p-next;}printf(\"\\ndeleteb=%d\",b);getch();}//创建一个空链表LNode*creat_head(){LNode*p;p=(Llist)malloc(sizeof(LNode));p-next=NULL;return(p);}//创建一个长度为n的线性链表voidcreat_list(LNode*head,intn){LNode*p,*q;inti;p=head;for(i=1;i=n;i){q=(Llist)malloc(sizeof(LNode));printf(\"data:\");scanf(\"%d\",data);q-next=NULL;p-next=q;p=q;}}//插入一个元素voidinsert_list(LNode*head,intx,inti){intj=0;LNode*p,*s;p=head;while((p!=NULL)i-1)){p=p-next;j;}if(p==NULL)exit(0);s=(Llist)malloc(sizeof(LNode));s-data=x;s-next=p-next;p-next=s;}//删除一个元素intdelete_list(LNode*head,inti){LNode*p,*q;intj=0;intx;p=head;while((p!=NULL)i-1)){p=p-next;j;}if(p==NULL)exit(0);q=p-next;p-next=q-next;x=q-data;free(q);return(x);}大哥这可是我一个字符一个字符的敲进去的啊~~~!!!我是学软件工程的,这是线性表的链式存储结构..
回复

使用道具 举报

千问 | 2011-9-19 01:17:30 | 显示全部楼层
#includestdio.h#includestdlib.h#defineN10//顺序表的最大容量intlength=0;//顺序表的当前元素个数#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1#defineOVERFLOW-2#defineLIST_INIT_SIZE100//线性表存储的空间初始化分配量#defineLISTINCREAMENT10//线性表存储空间的分配增量typedefstructLNode//线性单链表存储结构{intdata;structLNode*next;}LNode,*LinkList;intCreatList_L(LinkList//分配一个空间给链表,作为头结点if(!L)exit(OVERFLOW);L-next=NULL;returnOK;}intDestroyList_L(LinkListreturnOK;}intListInsert_L(LinkList//p指针定位于i-1LNode*s;intj=0;while(pi-1){p=p-next;j;}//定位if(!p||ji-1)returnERROR;//如果i1或大于链表元素个数1s=(LNode*)malloc(sizeof(LNode));if(!s)exit(OVERFLOW);s-data=e;//完成插入操作s-next=p-next;p-next=s;returnOK;}intListDelet_L(LinkList{LinkListp=L;LNode*q;intj=0;while(!pi-1){p=p-next;j;}//p指针定位于i-1;if(!p-next||ji-1)returnERROR;e=p-next-data;//完成删除操作q=p-next;p-next=p-next-next;free(q);returnOK;}intListTraverse_L(LinkListL,intn)//链表的遍历{inti=0;if(!L)returnERROR;L=L-next;while(L){if(L-data==n)returni;L=L-next;i;}returnFALSE;}intInverseSingleList_L(LinkListnext||!L-next-next)//如果链表少于2个Node那么链表不需要改变顺序returnOK;LNode*p,*q;p=L-next;//第一次因为p是最后一个连接所以把p-next设为空q=p-next;p-next=NULL;p=q;while(p){q=p-next;//用q去保留p后面一个Node;p-next=L-next;L-next=p;p=q;}returnOK;}intmain(){intList[N];LinkListL;intch,exit=\'N\';do{system(\"CLS\");printf(\"\\t\\t********************************************\\n\");printf(\"\\t\\t*1.创建一个顺序表.........(1)*\\n\");printf(\"\\t\\t*2.在顺序表中查找元表.........(2)*\\n\");printf(\"\\t\\t*3.在顺序表中插入元表.........(3)*\\n\");printf(\"\\t\\t*4.在顺序表中删除元表.........(4)*\\n\");printf(\"\\t\\t*5.退出.........(5)*\\n\");printf(\"\\t\\t********************************************\\n\");printf(\"\\n请选择操作代码:\");ch=getchar();switch(ch){case\'1\':printf(\"\\n请输入十个元素\");CreatList_L(L);for(length=0;lengthN;length){scanf(\"%d\",getchar();ListInsert_L(L,length1,List[length]);}printf(\"\\n创建成功!\");getchar();break;case\'2\':scanf(\"%d\",if(ListTraverse_L(L,List[0]))printf(\"该元素存在该年表的第%d个位置\",ListTraverse_L(L,List[0]));elseprintf(\"不存在该元素\");getchar();break;case\'3\':scanf(\"%d%d\",ListInsert_L(L,length,List[0]);system(\"pause\");break;case\'4\':scanf(\"%d\",ListDelet_L(L,length,List[0]);system(\"pause\");break;case\'5\':printf(\"\\n您是否真的要退出程序(Y/N):\");getchar();exit=getchar();break;default:getchar();printf(\"\\n无效输入,请重新选择...:\");getchar();break;}}while(exit!=\'y\'}
回复

使用道具 举报

千问 | 2011-9-19 01:17:30 | 显示全部楼层
/*创建包含学号、姓名节点的单链表。其节点数任意个,表以学号为序,低学号的在前,高学号的在后,以输入姓名为空作结束。在此链表中,要求删除一个给定姓名的节点,并插入一个给定学号和姓名的节点。*/#includestdio.h#includestdlib.h#includemalloc.h#includestring.hstructnode/*节点的数据结构*/{intnum;charstr[20];structnode*next;};main(){/*函数声明*/structnode*creat(structnode*head);structnode*insert(structnode*head,char*pstr,intn);structnode*delet(structnode*head,char*pstr);voidprint(structnode*head);structnode*head;charstr[20];intn;head=NULL;/*做空表*/head=creat(head);/*调用函数创建以head为头的链表*/print(head);/*调用函数输出节点*/printf(\"\\ninputinsertednum,name:\\n\");gets(str);/*输入学号*/n=atoi(str);gets(str);/*输入姓名*/head=insert(head,str,n);/*将节点插入链表*/print(head);/*调用函数输出节点*/printf(\"\\ninputdeletedname:\\n\");gets(str);/*输入被删姓名*/head=delet(head,str);/*调用函数删除节点*/print(head);/*调用函数输出节点*/system(\"pause\");return0;}/***创建链表************/structnode*creat(structnode*head){chartemp[30];structnode*p1,*p2;p1=p2=(structnode*)malloc(sizeof(structnode));printf(\"inputnum,name:\\n;\");printf(\"exit:doubletimesEnter!\\n\");gets(temp);gets(p1-str);p1-num=atoi(temp);p1-next=NULL;while(strlen(p1-str)0){if(head==NULL)head=p1;elsep2-next=p1;p2=p1;p1=(structnode*)malloc(sizeof(structnode));printf(\"inputnum,name:\\n\");printf(\"exit:doubletimesEnter!\\n\");gets(temp);gets(p1-str);p1-num=atoi(temp);p1-next=NULL;}returnhead;}/********************//**********插入节点**********/structnode*insert(structnode*head,char*pstr,intn){structnode*p1,*p2,*p3;p1=(structnode*)malloc(sizeof(structnode));strcpy(p1-str,pstr);p1-num=n;p2=head;if(head==NULL){head=p1;p1-next=NULL;}else{while(np2-numnext!=NULL){p3=p2;p2=p2-next;}if(n=p2-num)if(head==p2){head=p1;p1-next=p2;}else{p3-next=p1;p1-next=p2;}else{p2-next=p1;p1-next=NULL;}}return(head);}/*************************//*****删除节点*************/structnode*delet(structnode*head,char*pstr){structnode*temp,*p;temp=head;if(head==NULL)printf(\"\\nListisnull!\\n\");else{temp=head;while(strcmp(temp-str,pstr)!=0next!=NULL){p=temp;temp=temp-next;}if(strcmp(temp-str,pstr)==0){if(temp==head){head=head-next;free(temp);}else{p-next=temp-next;printf(\"deletestring:%s\\n\",temp-str);free(temp);}}elseprintf(\"\\nnofindstring!\\n\");}return(head);}/**********************************//**********链表各节点的输出**********/voidprint(structnode*head){structnode*temp;temp=head;printf(\"\\noutputstrings:\\n\");while(temp!=NULL){printf(\"\\n%d----%s\\n\",temp-num,temp-str);temp=temp-next;}}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行