C語言順序棧
阿新 • • 發佈:2018-11-11
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1000
typedef int ElemType;
typedef struct //定義結構體
{
ElemType data[MAXSIZE];//定義資料域
int top;//定義頭結點
} SeqStack;
SeqStack Push(SeqStack s)//入棧
{
ElemType x;
s.top = -1;
if(s.top == MAXSIZE)
{
printf("Stack overflow!");
}
printf("請輸入你想入棧的元素:\n");
scanf("%d",&x);
s.top++;//棧頂元素後移
s.data[s.top] = x;//賦值x
return s;
}
SeqStack Poo(SeqStack s)//出棧
{
printf("出棧的元素為:\n");
printf("%d",s.data[s.top]);//出棧元素
s.top--;
return s;
}
int main()
{
ElemType a;
SeqStack s;
s.top = -1;
while(1)
{
printf("1,入棧\n");
printf("2,出棧\n");
printf("請輸入你的操作:\n");
scanf("%d",&a);
switch (a)
{
case 1:
{
s = Push(s);
system("cls");
break;
}
case 2:
{
Poo(s);
system("pause");
system("cls");
break;
}
}
}
return 0;
}
#include <stdlib.h>
#define MAXSIZE 1000
typedef int ElemType;
typedef struct //定義結構體
{
ElemType data[MAXSIZE];//定義資料域
int top;//定義頭結點
} SeqStack;
SeqStack Push(SeqStack s)//入棧
{
ElemType x;
s.top = -1;
if(s.top == MAXSIZE)
{
printf("Stack overflow!");
}
printf("請輸入你想入棧的元素:\n");
scanf("%d",&x);
s.top++;//棧頂元素後移
s.data[s.top] = x;//賦值x
return s;
}
SeqStack Poo(SeqStack s)//出棧
{
printf("出棧的元素為:\n");
printf("%d",s.data[s.top]);//出棧元素
s.top--;
return s;
}
int main()
{
ElemType a;
SeqStack s;
s.top = -1;
while(1)
{
printf("1,入棧\n");
printf("2,出棧\n");
printf("請輸入你的操作:\n");
scanf("%d",&a);
switch (a)
{
case 1:
{
s = Push(s);
system("cls");
break;
}
case 2:
{
Poo(s);
system("pause");
system("cls");
break;
}
}
}
return 0;
}