如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
数据结构实验报告实验二栈及队列姓名:吴波学号:20115844班级:计科2011-01设计时间:25/42013LAGO3-1.CPP源代码#include<stdio.h>#include<malloc.h>typedefcharElement;typedefstructlinknode{Elementch;structlinknode*next;}Linkstack;voidInitstack(Linkstack*&s)//初始化函数{s=(Linkstack*)malloc(sizeof(Linkstack));s->next=NULL;}boolStackempty(Linkstack*&s)//判空函数{return(s->next==NULL);}voidPush(Linkstack*&s,Elemente)//元素入栈函数{Linkstack*p;p=(Linkstack*)malloc(sizeof(Linkstack));p->ch=e;p->next=s->next;s->next=p;}intLenlinkstack(Linkstack*&s)//计算链栈长度{inti=0;Linkstack*p=s;while(p->next!=NULL)p=p->next,i++;returni;}voidDisplay(Linkstack*&s)//输出链栈函数{Linkstack*p=s;while(p->next!=NULL)p=p->next,printf("%c\t",p->ch);printf("\n");}voidDestroylink(Linkstack*&s)//释放链栈{Linkstack*p=s,*q=s->next;while(q!=NULL){free(p);p=q;q=p->next;}free(p);}/************************\***主函数***\************************/intmain(void){Linkstack*s;printf("(1)初始化链栈\n");Initstack(s);printf("(2)判断是否为空:%d\n",Stackempty(s));printf("(3)依次入栈“a,b,c,d,e”\n");Push(s,'a');Push(s,'b');Push(s,'c');Push(s,'d');Push(s,'e');printf("(4)判断是否为空:%d\n",Stackempty(s));printf("(5)计算链栈长度:\t%d\n",Lenlinkstack(s));printf("(6)输出从栈顶到栈底元素:");Display(s);printf("(7)判断是否为空:%d\n",Stackempty(s));printf("(8)释放链栈\n");Destroylink(s);}运行效果:ALOGO3-2源代码:#include<stdio.h>#defineMaxsize20#include<malloc.h>typedefcharElemtype;typedefstruct{Elemtypech[Maxsize];intfront,rear;}SqQueue;voidIntlist(SqQueue*&q)//初始化函数;{q=(SqQueue*)malloc(sizeof(SqQueue));q->front=q->rear=0;}boolQueueEmpty(SqQueue*q)//判断是否为空{return(q->front==q->rear);}boolenQueue(SqQueue*&q,Elemtypee)//进队列{if((q->rear+1)%Maxsize==q->front)returnfalse;q->rear=(q->rear+1)%Maxsize;q->ch[q->rear]=e;returntrue;}booldeQueue(SqQueue*&q,Elemty