《杨程-栈和队列》由会员分享,可在线阅读,更多相关《杨程-栈和队列(7页珍藏版)》请在金锄头文库上搜索。
1、 数据结构实验报告栈和队列姓名:杨 程 班级:073112 学号: 20111001132 1、实验目的 (1)熟练掌握栈的逻辑结构和操作规则,能在相应的实际问题中正确选用该结构。 (2)熟练掌握栈的 2 种存储结构实现方法(顺序栈和链栈) ,两种存储结构和基本运算 的实现算法,注意栈空盒满的判断条件及它们的描述方法。 (3)熟练掌握队列的逻辑结构和操作规范,能在相应的实际问题中正确选用该结构。 (4)掌握循环队列与链队列两种存储结构的实现,熟练掌握各种队列基本运算的实现。 2、实验要求 (1)顺序栈的插入、删除,栈顶数据元素的读取。 (2)链栈的插入、删除,栈顶数据元素的读取。 (3)循环队
2、列的插入、删除。 (4)链队列的插入、删除。 3、实验内容 1、 实现顺序栈各种基本运算 #include #include #include #include #define STACK_INIT_SIZE 100; #define STACKINCREMENT 10; typedef struct int *base;int *top;int stacksize; SqStack; typedef int ElemType; int InitStack(SqStack S.base=(int *)malloc(size*sizeof(ElemType);if(!S.base) return
3、 0; S.top=S.base; /置栈S为空栈S.stacksize=STACK_INIT_SIZE; return 1; int GetTop(SqStack S,int e=*(S.top-1);return 1; int Push(SqStack S.base=(ElemType*)realloc(S.base,(S.stacksize+stackinvrement)*sizeof(ElemType);if(!S.base) return 0; /*存储分配失败*/S.stacksize+=STACKINCREMENT; *S.top+=e;return 1; int Pop(SqS
4、tack e=*-S.top;return 1; void OutputStack(SqStack q=S.top-1;for(int i=0;i #include #include #define MAXQSIZE 5 enum BOOLFalse,True; typedef struct /定义队列结构 char elemMAXQSIZE; /队列体 int front; /队头指针 int rear; /队尾指针 SqQueue; void initial(SqQueue /初始化一个队列 BOOL En_SqQueue(SqQueue /将一个元素入队列 BOOL De_SqQueue
5、(SqQueue /将一个元素出队列 void Print_SqQueue(SqQueue);/显示队列中所有元素 void main() SqQueue S; char ch,j; int flag=1; BOOL temp; printf(“本程序实现循环队列的操作。n“); printf(“可以进行入队列,出队列等操作。n“); initial(S); /初始化队列 while(flag) printf(“请选择n“); printf(“1.显示队列所有元素n“); printf(“2.入队列n“); printf(“3.出队列n“); printf(“4.退出程序n“); scanf(
6、“ %c“, switch(j) case 1:Print_SqQueue(S); /显示队列中所有元素 break; case 2:printf(“请输入队的元素(一个字符):“); scanf(“ %c“, /输入要入队列的字符 temp=En_SqQueue(S,ch);/入队列 if(temp=False) printf(“队列已满!n“); Print_SqQueue(S); break; case 3:temp=De_SqQueue(S,ch); /出队列 if(temp!=False) printf(“删除了一个元素:%cn“,ch);/若队列不空,显示出队列的元素 Print_
7、SqQueue(S); else printf(“队列为空!n“);/否则队列为空 break; default:flag=0;printf(“程序运行结束,按任意键结束!n“); getch(); void initial(SqQueue /队头指针及队尾指针同置为 0 BOOL En_SqQueue(SqQueue /若队列已满,返回 False Q.elemQ.rear=ch; Q.rear=(Q.rear+1)%MAXQSIZE; /修改队尾指针 return True; BOOL De_SqQueue(SqQueue /若队列已空,返回True ch=Q.elemQ.front; Q.front=(Q.front+1)%MAXQSIZE; /修改队头指针 return True; /成功出队列,返回 True void Print_SqQueue(SqQueue Q) /显示队列中所有元素 int i; if(Q.front=Q.rear) printf(“队列为空!n“); else i=Q.front; while(i!=Q.rear) printf(“%c “,Q.elemi); i+; if(i=MAXQSIZE) i=i%MAXQSIZE; printf(“n“); 实验结果