数据结构代码.docx
- 文档编号:1321714
- 上传时间:2023-04-30
- 格式:DOCX
- 页数:33
- 大小:20.73KB
数据结构代码.docx
《数据结构代码.docx》由会员分享,可在线阅读,更多相关《数据结构代码.docx(33页珍藏版)》请在冰点文库上搜索。
数据结构代码
//程序作者:
魏洪源
//版权所有,转载请联系:
why2942@
#include
usingnamespacestd;
structDulNode{
intdata;
DulNode*prior;
DulNode*next;
};
classDulLinkList{
private:
DulNode*head;
intn;
public:
voidinputNodeNumber();
voidcreateList();
intlistLength();
boollistInsert(inti,inte);
boollistDelete(inti);
voiddisplay();
};
voidDulLinkList:
:
inputNodeNumber()
{
cout<<"请输入你要创建的双链表的节点数:
";
do
{
cin>>n;
if(n<=0)cout<<"不能创建"< "; }while(n<=0); } voidDulLinkList: : createList() { intm; DulNode*L,*p,*s; L=newDulNode; L->data=n; L->prior=NULL; L->next=NULL; p=L; for(inti=1;i<=n;i++) { cout<<"请输入第"< "; cin>>m; s=newDulNode; s->data=m; s->next=NULL; s->prior=p; p->next=s; p=s; } head=L; } intDulLinkList: : listLength() { returnn; } boolDulLinkList: : listInsert(inti,inte) { intj=0; DulNode*p,*s; p=head; while(p! =NULL&&j if(p==NULL||j>i-1){cout<<"插入位置错误";returnfalse;} s=newDulNode; s->data=e; s->next=p->next; s->prior=p; p->next=s; s->next->prior=s; head->data++; n++; returntrue; } boolDulLinkList: : listDelete(inti) { intj=0; DulNode*p,*temp; p=head; while(p! =NULL&&j if(p==NULL||j>i-1){cout<<"删除位置错误";returnfalse;} temp=p->next; p->next=p->next->next; p->next->prior=p; deletetemp; head->data--; n--; returntrue; } voidDulLinkList: : display() { DulNode*p; p=head->next; cout<<"\n开始输出链表\n"; cout<<"链表共有"< \n"; for(inti=1;i<=n;i++) { cout<<"结点"< "< p=p->next; } cout<<"链表输出完毕\n\n"; } intmain() { intn; inti,e; intchoose=0; DulLinkListL; cout<<"\n********* 程序开始前,先创建双链表 ************\n"; L.inputNodeNumber(); L.createList(); cout<<"\n链表创建成功,输出如下: \n"; L.display(); while(choose! =3) { choose=0; cout<<"\n你可以对双链表进行的操作如下: \n"; cout<<"1.插入\n2.删除\n3.退出\n"; while(choose! =1&&choose! =2&&choose! =3) { cout<<"请输入1或2或3选择插入或删除或退出: "; cin>>choose; if(choose! =1&&choose! =2&&choose! =3)cout<<"输入错误,请重新输入\n"; } if(choose==1) { while (1) { cout<<"\n双链表插入前为: \n"; L.display(); cout<<"请输入要插入的数: "; cin>>e; cout<<"请输入要插入的位置: "; cin>>i; if(L.listInsert(i,e)){cout<<"\n双链表插入后为: \n";L.display();break;} elsecout<<"输入的插入位置错误,请重试\n"; } } elseif(choose==2) { while (1) { cout<<"\n双链表删除前为: \n"; L.display(); cout<<"请输入删除的结点的序号: "; cin>>i; if(L.listDelete(i)){cout<<"\n双链表删除后为: \n";L.display();break;} elsecout<<"输入的结点序号错误,请重试\n"; } } elseif(choose==3) { cout<<"\n双链表最后为: \n"; L.display(); continue; } } return0; } //程序设计: 魏洪源 //版权所有,转载请联系: why2942@ // 设计一个栈(Stack)类。 栈是一种具有后进先出(LIFO)特 // 点的存储实体。 自己设计其中的数据成员和成员函数。 要求 // 包含缺省构造函数、拷贝构造函数,其他成员函数自己设计。 #include usingnamespacestd; constintmax=50; classStack { public: Stack();//缺省构造函数 Stack(constStack&a);//拷贝构造函数 boolPush(intitem);//进栈 boolPop(int&del);//出栈 boolGetTop(int&topnumber);//取栈顶元素 boolMakeEmpty();//置空 boolDisplay();//打印所有栈元素 ~Stack();//析构函数 private: int*base; inttop; boolIsFull();//判断是否满 boolIsEmpty();//判断是否为空 }; intmain() { cout<<"1.push"< cout<<"2.pop"< cout<<"3.display"< cout<<"4.copy"< cout<<"5.exit"< cout<<"Inputnumberstodotheoperation: "; intoper; cin>>oper; Stacks; do{//循环测试 if(oper==1) { cout<<"Inputwhattopush: "; intp; cin>>p; if(s.Push(p))s.Display(); elsecout<<"Stackisfull! "< } elseif(oper==2) { intdele; if(s.Pop(dele)) cout<<"deletednumber: "< elsecout<<"EmptyStack"< } elseif(oper==3) { s.Display(); } elseif(oper==4) { Stackcpy(s); cout<<"Newstack: "; cpy.Display(); } elseif(oper==5)break; else { cout<<"Wrongnumber! "< } cout<<"Inputnumberstodotheoperation: "; cin>>oper; }while(oper! =5); return0; } boolStack: : IsFull()//判断是否满 { if(top>=max-1)returntrue; elsereturnfalse; } boolStack: : IsEmpty()//判断是否为空 { if(top==-1)returntrue; elsereturnfalse; } Stack: : Stack()//缺省构造函数 { base=newint[max]; top=-1; } Stack: : Stack(constStack&a)//拷贝构造函数 { base=newint[max]; inti; for(i=0;i<=a.top;i++) base[i]=a.base[i]; top=a.top; } boolStack: : Push(intitem)//进栈 { if(IsFull())returnfalse; top++; base[top]=item; returntrue; } boolStack: : Pop(int&del)//出栈 { if(IsEmpty())returnfalse; del=base[top]; top--; returntrue; } boolStack: : GetTop(int&topnumber)//取栈顶元素 { if(IsEmpty()){cout<<"Empty! "< topnumber=base[top]; returntrue; } boolStack: : MakeEmpty()//置空 { top=-1; returntrue; } boolStack: : Display()//打印所有栈元素 { if(top==-1){cout<<"Emptystack! "< inti; for(i=0;i<=top;i++) { cout< } cout< returntrue; } Stack: : ~Stack()//析构函数 { delete[]base; } #include usingnamespacestd; typedefstructLNode { intdata; structLNode*next; }LNode,*LinkList; voidCreateList_L(LinkList&L,intn)//创建链表 { L=newLNode; L->next=NULL; for(inti=n;i>0;--i) { LinkListp=newLNode; cin>>p->data; p->next=L->next; L->next=p; } } intListInsert_L(LinkList&L,inti,inte)//插入元素 { LinkListP=L; intj=0; while(P&&j { P=P->next; ++j; } if(! P||j>i-1) return0; LinkLists=newLNode; s->data=e; s->next=P->next; P->next=s; return1; } intGetElem_L(LinkListL,inti,int&e) //获得相应位置元素 { LinkListP=L->next; intj=1; while(P&&j { P=P->next; ++j; } if(! P||j>i) return0; e=P->data; return1; } voidshow(LinkListL) //显示函数 { LinkListp=L->next; cout<<"口->"; while(p->next) { cout< p=p->next; } cout< } intListDelete_L(LinkList&L,inti,int&e) //删除函数 { LinkListP=L; intj=0; while((P->next)&&(j { P=P->next; ++j; } if(! ((P->next)||(j return0; LinkListQ=P->next; P->next=Q->next; e=Q->data; free(Q); return1; } voidMergeList_L(LinkList&La,LinkList&Lb,LinkList&Lc) { LinkListpc; LinkListpa=La->next; LinkListpb=Lb->next; Lc=pc=La; while(pa&&pb) { if(pa->data<=pb->data) { pc->next=pa; pc=pa; pa=pa->next; } else { pc->next=pb; pc=pb; pb=pb->next; } } pc->next=pa? pa: pb; free(Lb); } intmain() { intlen_a,len_b; LinkListList_a,List_b,List_c; cout<<"请输入第a链表的长度"< cin>>len_a; cout<<"输入第a链表的这"< "< CreateList_L(List_a,len_a); show(List_a); cout<<"请输入第b链表的长度"< cin>>len_b; cout<<"输入第b链表的这"< "< CreateList_L(List_b,len_b); show(List_b); cout< loop: cout<<"选择你要进行的操作: "< cout< cout<<"1.获得a链表的元素: "< cout<<"2.获得b链表的元素: "< cout<<"3.在a链表中插入元素: "< cout<<"4.在b链表中插入元素: "< cout<<"5.删除a链表中的元素: "< cout<<"6.删除b链表中的元素: "< cout<<"7.合并链表a和链表b: "< cout< intnum; cin>>num; if(num==1) { intget_a_i,get_a_e; cout<<"输入你要获得元素在a链表中的位置: "< cin>>get_a_i; GetElem_L(List_a,get_a_i,get_a_e); cout<<"取得的元素是: "< } elseif(num==2) { intget_b_i,get_b_e; cout<<"输入你要获得元素在b链表中的位置: "< cin>>get_b_i; GetElem_L(List_b,get_b_i,get_b_e); cout<<"取得的元素是: "< } elseif(num==3) { intinsert_a_i,insert_a_e; cout<<"请输入您要插入的位置: "< cin>>insert_a_i; cout<<"请输入您要插入的元素: "< cin>>insert_a_e; ListInsert_L(List_a,insert_a_i,insert_a_e); show(List_a); } elseif(num==4) { intinsert_b_i,insert_b_e; cout<<"请输入您要插入的位置: "< cin>>insert_b_i; cout<<"请输入您要插入的元素: "< cin>>insert_b_e; ListInsert_L(List_b,insert_b_i,insert_b_e); show(List_b); } elseif(num==5) { intdel_a_i,del_a_e; cout<<"输入你要删除元素所在的位置: "; cin>>del_a_i; ListDelete_L(List_a,del_a_i,del_a_e); show(List_a); cout<<"删除的元素是: "< } elseif(num==6) { intdel_b_i,del_b_e; cout<<"输入你要删除元素所在的位置: "; cin>>del_b_i; ListDelete_L(List_b,del_b_i,del_b_e); show(List_b); cout<<"删除的元素是: "< } elseif(num==7) { MergeList_L(List_a,List_b,List_c); show(List_c); } else { cout<<"输入错误,只能输入1——7! 请重新输入: "< gotoloop; } return0; } 求C语言数据结构中的链表创建,插入和删除代码急求一段能运行,能编译(没一点错误,不然还要我找错误)的程序,能在c上运行的,简短的就行,只要包含了链表的创建,插入和删除就行。 不用太长,越简短越好,附上注释,谢谢。 比如学生成绩的输入,插入类似的问题。 提问者: 回头是岸1194-门吏三级其他回答 共4条/*创建包含学号、姓名节点的单链表。 其节点数任意个,表以学号为序,低学号的在前,高学号的在后,以输入姓名为空作结束。 在此链表中,要求删除一个给定姓名的节点,并插入一个给定学号和姓名的节点。 */ #include #include #include #include structnode/*节点的数据结构*/ { 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(he
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 代码