编写程序,实现顺序栈的创建、进栈和出栈等基本操作算法。
2017-03-22 14:34
591 查看
编写程序,实现顺序栈的创建、进栈和出栈等基本操作算法。
(1) 创建包含任意个元素的顺序栈。
(2) 进栈一个任意元素。
(3) 出栈一个任意元素。
(4) 取栈顶元素并输出。
*(5) 删除栈中值为给定值的元素。
*(6) 利用顺序表实现顺序栈元素的逆置。
#include<stdio.h>
#include<malloc.h>
#include<ctype.h>
#include <time.h>
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
ElemType stacksize;
}SqStack;
SqStack initiallist();
SqStack insert(SqStack,int);
SqStack insert1(SqStack);
void rever(SqStack,int);
void deletely(SqStack);
int main()
{
int i=0;
int j=0;
int n;
printf("\n请输入一个大于0的值使得在这个范围内随机产生栈中元素个数");
scanf("%d",&n);
srand((int)time(NULL));
int m1=rand()%n+1;
int a
;
SqStack q;
SqStack q1;
SqStack q2;
SqStack q3;
q=initiallist();
q2=insert1(q);
q1=insert(q,m1);
q3=q1;
printf("输出栈顶元素为");
printf("%d",*--(q1.top));
*++(q1.top);
printf("\n输出栈的所有元素:");
printf("\n");
while(q1.top!=q1.base)
a[i++]=*--(q1.top),printf("%d\n",*(q1.top)),j++;
srand((int)time(NULL));
int m2=rand()%(j);
printf("出栈的随机元素为");
printf("%d",a[m2]);
printf("\n");
rever(q3,m1);
deletely(q3);
}
SqStack initiallist()
{
SqStack S;
int MAXSIZE;
printf("请输入栈中最大储存的元素个数");
scanf("%d",&MAXSIZE);
S.base=new int[MAXSIZE];
if(!*S.base)
exit(-1);
S.top=S.base;
S.stacksize=MAXSIZE;
return S;
}
SqStack insert(SqStack q,int m1)
{
int k;
int m[m1];
printf("栈中元素个数为:");
printf("%d",m1);
printf("\n请输入一个大于等于0的值使得在0到这个范围内随机产生栈中元素");
scanf("%d",&k);
for(int j=0;j<m1;j++)
m[j]=rand()%k;
printf("请输出产生的随机元素\n");
for(int k1=0;k1<m1;k1++)
printf("%d\n",m[k1]);
if(q.top-q.base==q.stacksize)
printf("栈已经满");
for(int i=0;i<m1;i++)
{
*(q.top)=m[i];
q.top++;
}
return q;
}
SqStack insert1(SqStack q)
{
int k;
printf("\n任意输入一个数");
scanf("%d",&k);
if(q.top-q.base==q.stacksize)
printf("栈已经满");
*(q.top)=k;
q.top++;
printf("\n任意入栈的一个元素是");
printf("%d",*--(q.top));
printf("\n");
}
void deletely(SqStack q1)
{
int t;
int m1=0;
int j=0;
int a[10];
printf("\n请输入你要删除的元素");
scanf("%d",&t);
*(--q1.top);
while(*q1.top!=t)
a[m1++]=(*q1.top)--;
if(*(q1.top)==NULL)
exit(-1);
printf("\n删除成功");
printf("\n输出第一个找到的想要删除的数是");
printf("%d",*(q1.top));
for(int i=0;i<=m1;i++)
++(*q1.top)=a[i];
while(q1.top!=q1.base)
{
printf("\n%d",*(q1.top));
*--(q1.top);
}
}
void rever(SqStack q1,int m1)
{
int t;
int b[m1];
int p=m1-1;
int j=0;
printf("把栈中的元素弹出放在一个数组中,数组元素分别为\n");
for(int i=0;i<m1;i++)
{
--q1.top;
b[i]=*(q1.top);
printf("%d\n",b[i]);
}
if(m1%2!=0)
{
while(j!=p)
{
t=b[j];
b[j]=b[p];
b[p]=t;
++j;
--p;
}
printf("逆置后的数组元素为\n");
for(int i1=0;i1<m1;i1++)
printf("%d\n",b[i1]);
while(q1.top!=q1.base)
--(q1.top);
for(int k=m1-1;k>=0;k--)
{
++(q1.top);
*(q1.top)=b[k];
}
printf("输出逆置后的栈元素\n");
while(q1.top!=q1.base)
{
printf("%d\n",*(q1.top));
--(q1.top);
}
}
else
{
for(int v=1;v<=m1/2;v++)
{
t=b[j];
b[j]=b[p];
b[p]=t;
++j;
--p;
}
printf("逆置后的数组元素为\n");
for(int i1=0;i1<m1;i1++)
printf("%d\n",b[i1]);
while(q1.top!=q1.base)
--(q1.top);
for(int k=m1-1;k>=0;k--)
{
++(q1.top);
*(q1.top)=b[k];
}
printf("输出逆置后的栈元素\n");
while(q1.top!=q1.base)
{
printf("%d\n",*(q1.top));
--(q1.top);
}
}
}
相关文章推荐
- C语言实现顺序栈的基本操作(初始化、判断空、入栈、出栈、获取栈顶元素)
- 编写一个程序,实现顺序栈的各种基本运算。
- 编写一个程序,实现顺序环形队列的各种基本运算。
- 编写一个程序实现顺序表的各种基本运算(假设顺序表的元数基本类型为Char)
- 数据结构 P64-65 算法实现 循环队列的创建和基本操作
- 编写一个程序,实现顺序表的各种基本运算
- 编写一个程序实现顺序栈的各种基本运算(假设顺序表的元数基本类型为Char)
- 实现一个栈(元素遵守先入后出顺序),能够通过 min 方法在 O(1)时间内获取栈中的最小元素。同时,栈的基本操作:入栈(Push)、出栈(Pop),也是在O(1)时间内完成的
- 数据结构 P62-62 算法实现 单链队列的创建及基本操作
- c语言:顺序表的实现(一) 创建,插入,删除,查找,输出等基本操作实现
- 编程菜鸟的日记-初学尝试编程-顺序表的类定义及其基本操作算法(创建表、元素插入、元素删除、顺序查找、测表空、求表长、输出等)
- 编写一个一个程序实现顺序环形队列各项基本运算(假设队列基本元素类型为Char)
- 顺序栈的定义、初始化、出栈、入栈等操作 C++代码实现
- C# 实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)
- C# 用Linq的方式实现对Xml文件的基本操作(创建xml文件、增删改查xml文件节点信息)
- C# 用Linq的方式实现对Xml文件的基本操作(创建xml文件、增删改查xml文件节点信息)
- C# 实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)
- C# 实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)
- C# 实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)zz
- C# 实现对XML文件的基本操作(创建xml文件,增、删、改、查 xml节点信息)