实验二栈队列的实现及应用学习资料Word下载.docx
《实验二栈队列的实现及应用学习资料Word下载.docx》由会员分享,可在线阅读,更多相关《实验二栈队列的实现及应用学习资料Word下载.docx(20页珍藏版)》请在冰豆网上搜索。
#defineSTACKINCREMENT10
#defineOK1
#defineERROR0
typedefcharSElemType;
/*顺序栈的存储类型*/
typedefstruct//definestructureSqStack()
{
SElemType*base;
SElemType*top;
intstacksize;
}SqStack;
/*构造空顺序栈*/
intInitStack(SqStack*S)//InitStack()sub-function
S->
base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!
S->
base)
{
printf("
分配空间失败!
\n"
);
return(ERROR);
}
top=S->
base;
stacksize=STACK_INIT_SIZE;
printf("
栈初始化成功!
return(OK);
}//InitStack()end
/*取顺序栈顶元素*/
intGetTop(SqStack*S,SElemType*e)//GetTop()sub-function
if(S->
top==S->
栈为空!
//ifemptySqStack
*e=*(S->
top-1);
}//GetTop()end
/*将元素压入顺序栈*/
intPush(SqStack*S)//Push()sub-function
SElemTypee;
top-S->
base>
stacksize)
S->
base=(SElemType*)realloc(S->
base,(S->
stacksize+
STACKINCREMENT*sizeof(SElemType)));
if(!
{
printf("
存储空间分配失败!
return(ERROR);
}
base+S->
stacksize;
stacksize+=STACKINCREMENT;
fflush(stdin);
//清除输入缓冲区,否则原来的输入会默认送给变量x
请输入要入栈的元素的值:
"
e=getchar();
*S->
top++=e;
}//Push()end
/*将元素弹出顺序栈*/
intPop(SqStack*S,SElemType*e)//Pop()sub-function
*e=*--S->
top;
}//Pop()end
voiddisplay(SqStack*s)
if(s->
top==s->
else{
while(s->
top!
=s->
s->
top=s->
top-1;
%c->
*(s->
top));
}
intmain()
intchoice;
SqStacks;
do
===============================\n"
0:
退出\n"
1:
初始化栈\n"
2:
入栈\n"
3:
出栈\n"
4:
读取栈顶元素\n"
5:
显示栈中元素\n"
输入操作选择代码(0-5):
scanf("
%d"
&
choice);
while(choice<
0||choice>
5){printf("
输入有误,请重新输入(0-5):
scanf("
switch(choice)
case0:
exit
(1);
case1:
InitStack(&
s);
break;
case2:
printf("
2\n"
Push(&
case3:
Pop(&
s,&
e);
出栈元素的值是:
%c\n"
e);
case4:
GetTop(&
栈顶元素的值是:
case5:
栈中元素的值是为:
display(&
}while(choice);
return0;
(2)运行结果
(3)结果分析
顺序表通过设置栈顶运用线性结构实现先进先出功能。
2.任务一
(2):
完成下列程序,该程序实现栈的链式存储结构,构建链栈(栈中的元素依次为China,Japan,France,India,Australia),依次进行进栈和出栈操作,判断栈空和栈满操作,返回栈顶元素操作。
要求生成链栈时,从键盘上读取数据元素。
string.h>
typedefcharDataType;
/*链式栈的存储类型*/
typedefstructSNode//definestructureLinkStack
{DataTypedata[20];
structSNode*next;
}SNode,*LinkStack;
voidInitStack_L(LinkStack*top)
top=(LinkStack)malloc(sizeof(SNode));
top->
next=NULL;
\n\n栈初始化成功!
\n\n"
/*取链式栈顶元素*/
intGetTop_L(LinkStack*top,DataTypee[])//GetTop_L()sub-function
{if(!
next)
{printf("
链栈为空!
return(ERROR);
else
{strcpy(e,top->
next->
data);
}//GetTop_L()end
/*将元素压入链式栈*/
intPush_L(LinkStack*top)//Push_L()sub-function
{SNode*q;
DataTypee[20];
q=(LinkStack)malloc(sizeof(SNode));
if(!
q)
\n"
//清除输入缓冲区,否则原来的输入会默认送给变量e
\n请输入要入栈的元素的值:
gets(e);
strcpy(q->
data,e);
q->
next=top->
next;
top->
next=q;
}//Push_L()end
/*将元素弹出链式栈*/
intPop_L(LinkStack*top,DataTypee[])//Pop_L()sub-function
\n"
strcpy(e,top->
q=top->
next=q->
free(q);
}//Pop_L()end
voiddisplay(LinkStack*top)
LinkStackp=top->
p)
while(p)
{
%s->
p->
p=p->
^\n"
}
charchoice;
DataTypee[20]="
;
LinkStacks=NULL;
do
%c"
&
while(choice<
'
0'
||choice>
5'
)
switch(choice)
case'
:
1'
InitStack_L(&
break;
2'
Push_L(&
3'
Pop_L(&
s,e);
4'
GetTop_L(&
%s\n"
e);
栈中元素的值是:
"
display(&
}while(choice);
return0;
链表通过设置栈顶运用指针实现先进先出功能
3.任务二:
完成下列程序,该程序实现循环队列的存储和基本操作,构建循环队列,完成键盘缓冲区的功能,每输入一个字符,链入缓冲区队列中;
每输出一个字符,将该字符从缓冲区中删除。
#defineMAXQSIZE100
/*定义QElemType为int或别的自定义类型*/
typedefcharQElemType;
/*顺序队列的存储类型*/
typedefstructSqQueue//definestructureSqQueue
{QElemType*base;
intfront;
intrear;
}SqQueue;
/*构造空顺序队列*/
intInitQueue(SqQueue*Q)//InitQueue()sub-function
{Q->
base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
Q->
front=Q->
rear=0;
队列初始化成功!
}//InitQueue()end
/*求顺序队列长度*/
intQueueLength(SqQueue*Q)//QueueLength()sub-function
{return((Q->
rear-Q->
front+MAXQSIZE)%MAXQSIZE);
/*在顺序队列尾插入新元素*/
intEnQueue(SqQueue*Q,QElemTypee)//EnQueue()sub-function
{if((Q->
rear+1)%MAXQSIZE==Q->
front)
队列已满!
return(ERROR);
Q->
base[Q->
rear]=e;
rear=(Q->
rear+1)%MAXQSIZE;
}//EnQueue()end
/*在顺序队列头删除旧元素*/
intDeQueue(SqQueue*Q,QElemTypee)//DeQueue()sub-function
{if(Q->
front==Q->
rear)
队列为空!
e=Q->
front];
front=(Q->
front+1)%MAXQSIZE;
}//DeQueue()end
voiddisplay(SqQueue*Q){
if(Q->
inti=Q->
front;
while((i+1)%MAXQSIZE!
=Q->
{
Q->
base[i]);
i++;
QElemTypee;
intchoice;
SqQueueQ;
InitQueue(&
Q);
==============================\n"
0、退出\n"
1、入队\n"
2、出队\n"
3、显示队列元素\n"
你操作选择是(0-3):
case1:
//清除输入缓冲区
请输入要入队的字符或字符串,以'
#'
结束:
while((e=getchar())!
='
)
EnQueue(&
Q,e);
case2:
DeQueue(&
case3:
}while(choice>
0&
&
choice<
=3);
循环队列通过设置队首和队尾实现先进后出功能
实验总结:
1.在本次试验中我学会了如何实现的栈的顺序存储以及链式存储。
2.以及懂得了栈的基本特性:
仅在表尾进行删除和插入操作、先进后出。