数据结构 栈和队列基本操作Word下载.docx
《数据结构 栈和队列基本操作Word下载.docx》由会员分享,可在线阅读,更多相关《数据结构 栈和队列基本操作Word下载.docx(13页珍藏版)》请在冰豆网上搜索。
![数据结构 栈和队列基本操作Word下载.docx](https://file1.bdocx.com/fileroot1/2022-12/12/6bf92349-29df-4c28-baa9-df6d2b3f92e7/6bf92349-29df-4c28-baa9-df6d2b3f92e71.gif)
e=*--s.top;
returne;
/*
voidconversion()//数制转换
intN;
printf("
请输入十进制数N:
"
);
scanf("
%d"
&
N);
Sqstacks;
Initstack(s);
while(N)
{
Push(s,N%8);
N=N/8;
}
转换的八进制为:
while(s.top!
=s.base)
printf("
Pop(s));
\n"
voidmain()
conversion();
*/
intn;
inttemp;
请输入初始化栈的元素个数:
n);
for(inti=1;
i<
=n;
i++)
请输入第%d个元素:
i);
scanf("
temp);
Push(s,temp);
初始化的栈出栈为:
while(s.top!
%d"
//链栈的基本操作(括号的匹配检查)
typedefstructSNode
chardata;
structSNode*next;
}SNode,*LinkStack;
//初始化栈
voidInit(LinkStack&
top)
top=(LinkStack)malloc(sizeof(SNode));
top->
next=NULL;
//创建栈
voidcreat(LinkStack&
top,intn)
inti;
SNode*p;
pleaseinputthedataforLinkListNodes:
for(i=n;
i>
0;
i--)
{
输入%d个元素:
p=(LinkStack)malloc(sizeof(SNode));
p->
data);
p->
next=top->
next;
top->
next=p;
//进栈操作
voidPush(LinkStack&
top,chare)
SNode*q;
q=(LinkStack)malloc(sizeof(SNode));
q->
data=e;
next=q;
//出栈操作
voidPop(LinkStack&
top,char&
e)
e=top->
next->
data;
q=top->
next=q->
free(q);
voiddisplay(LinkStack&
p=top;
while(p->
next!
=NULL)
p->
p=p->
//测试程序
chare,a;
LinkStackstack1;
Init(stack1);
请输入括号以#号结束!
while
(1)
%c"
a);
if(a=='
#'
)
break;
('
||a=='
['
Push(stack1,a);
elseif(a=='
)'
{
if(stack1->
next==NULL)
{
printf("
括号不匹配!
Push(stack1,a);
break;
}
Pop(stack1,e);
if(e!
='
}
]'
if(stack1->
if(stack1->
括号匹配!
//链队列的基本操作
typedefintQElemType;
typedefstructQNode{
intdata;
structQNode*next;
}QNode,*QueuePtr;
QueuePtrfront;
QueuePtrrear;
}LinkQueue;
//构造一个空队列Q
voidInitQueue(LinkQueue&
Q)
Q.rear=Q.front=(QueuePtr)malloc(sizeof(QNode));
Q.front->
voidEnQueue(LinkQueue&
Q,inte)
QNode*p;
p=(QueuePtr)malloc(sizeof(QNode));
p->
Q.rear->
Q.rear=p;
voidDeQueue(LinkQueue&
Q,int&
p=Q.front->
e=p->
next=p->
if(Q.rear==p)
Q.rear=Q.front;
free(p);
LinkQueueQ;
InitQueue(Q);
你要入队的元素个数:
inttemp;
输入第%d个元素:
EnQueue(Q,temp);
出队的元素为:
while(!
(Q.front==Q.rear))
DeQueue(Q,e);
e);
//应用栈和队列判断回文
//栈
voidInitLinkStack(LinkStack&
e1)
e1=top->
//队列
Q,chare)
Q,char&
e2)
e2=p->
//回文判断函数
intHuiwen(LinkStack&
S,LinkQueue&
chare1,e2;
Pop(S,e1);
DeQueue(Q,e2);
if(S->
next==NULL&
&
Q.front==Q.rear)
return1;
if(e1!
=e2)
return0;
chare,e1,e2;
LinkStackS;
InitLinkStack(S);
请输入字符串:
e);
if(e=='
\n'
Push(S,e);
EnQueue(Q,e);
/*
printf("
字符串是回文字符串!
字符串不是回文字符串!
*/
inta;
a=Huiwen(S,Q);
if(a==1)
elseif(a==0)