顺序表栈的基本操作
2017-12-11 20:08
274 查看
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef int datatype;
/*
定义结构体,data指存储在栈中的数据
top 是指向栈顶元素
*/
typedef struct
{
datatype data[MAXSIZE];
int top;
}SeqStack;
/*
初始化栈,将栈顶置空
*/
SeqStack *Init_SeqStack()
{
SeqStack *s;
s = (SeqStack*)malloc(sizeof(SeqStack));
s->top = -1;
return s;
}
/*判栈空*/
int Empty_SeqStack(SeqStack *s)
{
if (s->top == -1) return 1;
else return 0;
}
void push_SeqStack(SeqStack *s, datatype x)
{
if (s->top == MAXSIZE - 1) printf("对不起,栈已经达到最大容量了");
else
{
s->top++;
s->data[s->top] = x;
printf("%d-入栈成功\n", x);
}
}
/*出栈*/
void Pop_SeqStack(SeqStack *s, datatype x)
{
if (Empty_SeqStack(s)) printf("栈空\n");
else
{
x = s->data[s->top];
s->top--;
printf("%d出栈成功 !\n",x);
}
}
/*
取得栈顶元素
*/
datatype Top_SeqStack(SeqStack *s)
{
if (Empty_SeqStack(s)) return 0;
else
return s->data[s->top];
}
void print_SeqStack(SeqStack *s) {
while (s->top != -1)
{
printf("%d ",s->data[s->top]);
s->top--;
}
}
int main()
{
SeqStack *s;
datatype x;
int a[5] = {1,2,3,4,5};
int i;
s = Init_SeqStack(); //对栈进行初始化
printf("原数组的中数据是:\n");
for ( i = 0; i < 5; i++)
{
printf("%d ",a[i]);
}
printf("\n");
//将上面声明的数组进行入栈操作
for(int i = 0; i < 5; i++)
{
push_SeqStack(s, a[i]);
}
//将上面的入栈的数据进行出栈操作
// print_SeqStack(s);
//将入栈的数据都出栈
//printf("\n出栈的数据是:");
//for (int i = 0; i < 5; i++)
//{
// Pop_SeqStack(s, a[i]);
//}
//查看出栈之后栈中的数据
//print_SeqStack(s);
//取得栈顶元素
printf("\n将栈顶元素进行输出:");
x = Top_SeqStack(s);
printf("%d",x);
getch();
}
#include<malloc.h>
#include<stdlib.h>
#define MAXSIZE 100
typedef int datatype;
/*
定义结构体,data指存储在栈中的数据
top 是指向栈顶元素
*/
typedef struct
{
datatype data[MAXSIZE];
int top;
}SeqStack;
/*
初始化栈,将栈顶置空
*/
SeqStack *Init_SeqStack()
{
SeqStack *s;
s = (SeqStack*)malloc(sizeof(SeqStack));
s->top = -1;
return s;
}
/*判栈空*/
int Empty_SeqStack(SeqStack *s)
{
if (s->top == -1) return 1;
else return 0;
}
void push_SeqStack(SeqStack *s, datatype x)
{
if (s->top == MAXSIZE - 1) printf("对不起,栈已经达到最大容量了");
else
{
s->top++;
s->data[s->top] = x;
printf("%d-入栈成功\n", x);
}
}
/*出栈*/
void Pop_SeqStack(SeqStack *s, datatype x)
{
if (Empty_SeqStack(s)) printf("栈空\n");
else
{
x = s->data[s->top];
s->top--;
printf("%d出栈成功 !\n",x);
}
}
/*
取得栈顶元素
*/
datatype Top_SeqStack(SeqStack *s)
{
if (Empty_SeqStack(s)) return 0;
else
return s->data[s->top];
}
void print_SeqStack(SeqStack *s) {
while (s->top != -1)
{
printf("%d ",s->data[s->top]);
s->top--;
}
}
int main()
{
SeqStack *s;
datatype x;
int a[5] = {1,2,3,4,5};
int i;
s = Init_SeqStack(); //对栈进行初始化
printf("原数组的中数据是:\n");
for ( i = 0; i < 5; i++)
{
printf("%d ",a[i]);
}
printf("\n");
//将上面声明的数组进行入栈操作
for(int i = 0; i < 5; i++)
{
push_SeqStack(s, a[i]);
}
//将上面的入栈的数据进行出栈操作
// print_SeqStack(s);
//将入栈的数据都出栈
//printf("\n出栈的数据是:");
//for (int i = 0; i < 5; i++)
//{
// Pop_SeqStack(s, a[i]);
//}
//查看出栈之后栈中的数据
//print_SeqStack(s);
//取得栈顶元素
printf("\n将栈顶元素进行输出:");
x = Top_SeqStack(s);
printf("%d",x);
getch();
}
相关文章推荐
- 顺序表基本操作归纳整理
- 顺序表基本操作
- 数据结构-顺序表和链表的基本操作
- 顺序表基本操作
- 实验A—1 顺序表基本操作
- PTA 顺序表基本操作(10 分)
- 1.1顺序表基本操作的设计与实现
- 顺序表静态与动态实现及基本的操作(增删查改等)
- 顺序表基本操作
- 2007.08.24静态单链表的基本操作及顺序表和链表的比较
- 顺序表基本操作(完整)
- 1_顺序表基本操作
- 线性表的基本操作(顺序表)
- 顺序表基本操作的代码实现:C++实现
- 顺序表基本操作
- 线性表-顺序表和单链表的定义与基本操作
- 顺序表基本操作(插入、删除)
- 线性表-顺序表和单链表的定义与基本操作
- 动态顺序表基本操作
- 用Java实现顺序表及其基本操作