欢迎来到天天文库
浏览记录
ID:28038859
大小:83.93 KB
页数:11页
时间:2018-12-07
《数据结构(链表栈队列树)》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、目录链表栈队列输出链表#include#include#defineLENsizeof(structStudent)structStudent{longnum;floatscore;structStudent*next;};intn;structStudent*creat()//建立链表的函数{structStudent*head;structStudent*pl,*p2;n=0;pl=p2=(structStudent*)malloc(LEN);printf(”plaeseinputdata:’’);
2、scanf("%ld,%r,&pl-〉num,&p1->score);head=NULL;while(pl->num!=0){n=n+l;if(n==l)head=pl;elsep2->next=pl;p2=pl;p1=(structStudent*)malloc(LEN);printff’plaeseinputdata:");scanf(’’%ld,%f’,&p1-〉num,&p1->score);}p2->next=NULL;return(head);}voidprint(structStudent*head)//输出链表的函数{str
3、uctStudent*p;printf("Now,These%drecordsare:",n);p=head;if(head!=NULL)do{printf("%ld%5.1f",p->num,p->score);p=p-〉next;}while(p!=NULL);}voidmain(){structStudent*head;head=creat();//调用creat函数,返回第一个节点的起始地址print(head);"调用print函数}栈的顺序存储及其实现#include#include4、h>#defineMAXLEN10typedefstruct{intdata[MAXLENJ;inttop;[SeqStack;/*初始化空栈*/voidInitSeqStack(SeqStack*s){s->top=-l;//置空栈intPush(SeqStack*$,intx)/*将元素x入栈到栈s*/{if(s->top==MAXLEN-1)//栈满{return0;}else//入找操作{s-〉top++;s->datafs->topl=x;return1;intStackEmpty(SeqStack*s)/*判断栈是否为空*/if5、(s->top==-l)//找为空{return1;}else{return0;}}intPop(SeqStack*s,int*x)/*出栈,将栈顶元素放入x所指向的存储单元中*/{if(StackEmpty(s))//栈空{return0;}else//出拽{*x=s-〉data[s-〉top];s->top-;return1;}}voidmain(){SeqStacksl,*p;intfinished=O;intchoice;inty,z;p=&sl;InitSeqStack(p);while(!finished){printf("6、请选择操作类型山入栈2:出栈3:结束n);printf("您的选择是:");scanf(H%d",&choice);switch(choice)casel:printf(n诺输入入栈元素的值:scanf(H%du,&y);if(Push(p,y)){printf("入栈成功11");}break;case2:if(Pop(p,&z)){printff出栈:%d'z);}else{printf(•’岀栈失败!");}break;case3:finished=l;队列的应用#include"stdio.h”#include"co7、nio.h"#defineMaxsize10typedefintdatatype;typedefstruct{datatypedatafMaxsize];//存放元素的数组intfront;intrear;JseqQueue;seqQueue*init_queue()//初始化{seqQueue*p;p=(seqQueue*)malloc(sizeof(seqQueue));p->front=p->rear=0;returnp;voiden_queue(seqQueue*p,datatypex)//将值为x的元素插入到队列p中{if((p-8、>rear+1)%Maxsize==p->front)printf(HThequeueisfullcan’ten_queue’’);return(l);}p-〉data[p-
4、h>#defineMAXLEN10typedefstruct{intdata[MAXLENJ;inttop;[SeqStack;/*初始化空栈*/voidInitSeqStack(SeqStack*s){s->top=-l;//置空栈intPush(SeqStack*$,intx)/*将元素x入栈到栈s*/{if(s->top==MAXLEN-1)//栈满{return0;}else//入找操作{s-〉top++;s->datafs->topl=x;return1;intStackEmpty(SeqStack*s)/*判断栈是否为空*/if
5、(s->top==-l)//找为空{return1;}else{return0;}}intPop(SeqStack*s,int*x)/*出栈,将栈顶元素放入x所指向的存储单元中*/{if(StackEmpty(s))//栈空{return0;}else//出拽{*x=s-〉data[s-〉top];s->top-;return1;}}voidmain(){SeqStacksl,*p;intfinished=O;intchoice;inty,z;p=&sl;InitSeqStack(p);while(!finished){printf("
6、请选择操作类型山入栈2:出栈3:结束n);printf("您的选择是:");scanf(H%d",&choice);switch(choice)casel:printf(n诺输入入栈元素的值:scanf(H%du,&y);if(Push(p,y)){printf("入栈成功11");}break;case2:if(Pop(p,&z)){printff出栈:%d'z);}else{printf(•’岀栈失败!");}break;case3:finished=l;队列的应用#include"stdio.h”#include"co
7、nio.h"#defineMaxsize10typedefintdatatype;typedefstruct{datatypedatafMaxsize];//存放元素的数组intfront;intrear;JseqQueue;seqQueue*init_queue()//初始化{seqQueue*p;p=(seqQueue*)malloc(sizeof(seqQueue));p->front=p->rear=0;returnp;voiden_queue(seqQueue*p,datatypex)//将值为x的元素插入到队列p中{if((p-
8、>rear+1)%Maxsize==p->front)printf(HThequeueisfullcan’ten_queue’’);return(l);}p-〉data[p-
此文档下载收益归作者所有