《数据结构》实验报告
实验序号 4 实验项目名称栈的操作
附源程序清单
1.
#include<iostream>
#define MaxSize 100using namespace std;typedef char ElemType;type de f s truct
{
ElemType data[MaxSize] ;int top;
}S qStack;void InitStack(SqStack*st)//初始化栈
{st->top=-1;
}int StackEmpty(SqStack*st) //判断栈为空{if(st->top==-1)return 0;//为空elsereturn-1;//不为空
}void Push(SqStack*st,ElemType x) //元素进栈{if(st->top==MaxSize-1)
{printf("栈上溢出!\n");
}else
{st->top++; //移动栈顶位置st->data[st->top]=x; //元素进栈
}
}void Pop(SqStack*st,ElemType&e) //出栈{if(st->top==-1)
{printf("栈下溢出\n");
}else
{e=st->data[st->top] ; //元素出栈st->top--; //移动栈顶位置}
}int main()
{
SqStack L;
SqStack*st=&L;
ElemType c;int i;
InitStack(st);printf("输入回车结束入栈");while((c=getchar())!='\n')
{if(c=='(')
Pu s h(st,c);if((i=StackEmpty(s t))==-1)
{if(c==')')
Pop(st,c);
}if(c==')'&&(i=StackEmpty(st))==0){printf("右括号多出,配对失败");goto loop;
}
}if(StackEmpty(st)==-1)printf("左括号多出,配对失败");elseprintf("配对成功");loop:return 0;
}
2.
#include<iostream>
#define MaxSize 100using namespace std;typedef int ElemType;type de f s truct
{
ElemType data[MaxSize] ;int top;
}S qStack;void InitStack(SqStack*st)
{st->top=-1;
}int StackEmpty(SqStack*st)
{return(st->top=-1);
}void Push(SqStack*st,ElemType *x,int k){int i;for(i=1;i<k;i++)
{if(st->top==MaxSize-1)
{printf("栈上溢出");
}else
{st->top++;st->data[st->top]=x[i-1];printf("%d\n",st->data[st->top]);}
}
}void Pop(SqStack*st,ElemType&e,int k){int i;for(i=1;i<k;i++)
{if(st->top==-1)
{printf("栈下溢出");
}else
{e=st->data[st->top] ;printf("%d\n",e);st->top--;
}
}
}int main()
{
SqStack L;
SqStack*st=&L;
ElemType e;
ElemType num[9]={1,2,3,4,5,6,7,8,9};InitStack(st);printf("入栈元素是:\n");
Pu s h(st,nu m,10);printf("出栈元素是:\n");
Pop(st,e,10);return 0;
}
3.
#include<iostream>
#include<stack>using namespace std;int main()
{char a,i;stack<char>s;while((a=getchar())!='\n')
{
switch(a)
{case'(':s.pu s h(a);//入栈continue;case'[':s.pu s h(a);continue;case')':if(!s.empty())
{if(s.top()=='(')
{s.pop(); //出栈continue;
}else
{printf("配对失败1");goto loop;
}
}case']':if(!s.empty())
{if(s.top()=='[')
{s.pop(); //出栈continue;
}else
{printf("配对失败2");goto loop;
}s.pop(); //出栈}
}
}if(s.e mpty())
{printf("配对成功");
}else
{printf("配对失败3");}loop:return 0;
}
老周互联怎么样?老周互联隶属于老周网络科技部旗下,创立于2019年12月份,是一家具有代表性的国人商家。目前主营的产品有云服务器,裸金属服务器。创办一年多以来,我们一直坚持以口碑至上,服务宗旨为理念,为用户提供7*24小时的轮班服务,目前已有上千多家中小型站长选择我们!服务宗旨:老周互联提供7*24小时轮流值班客服,用户24小时内咨询问题可提交工单,我们会在30分钟内为您快速解答!另免费部署服务器...
我们先普及一下常识吧,每年9月的第一个星期一是美国劳工节。于是,有一些服务商会基于这些节日推出吸引用户的促销活动,比如RackNerd有推出四款洛杉矶和犹他州独立服务器,1G带宽、5个独立IP地址,可以配置Windows和Linux系统,如果有需要独立服务器的可以看看。第一、劳工节促销套餐这里有提供2个套餐。两个方案是选择犹他州的,有2个方案是可以选择洛杉矶机房的。CPU内存SSD硬盘配置流量价格...
spinservers是Majestic Hosting Solutions,LLC旗下站点,主营美国独立服务器租用和Hybrid Dedicated等,spinservers这次提供的大硬盘、大内存服务器很多人很喜欢。TheServerStore自1994年以来,它是一家成熟的企业 IT 设备供应商,专门从事二手服务器和工作站业务,在德克萨斯州拥有40,000 平方英尺的仓库,库存中始终有数千台...