数据结构--栈的操作
2010-10-08 17:22
141 查看
#include <stdio.h>
#include <stdlib.h>
#define Maxsize 100 /*设顺序表的最大长度为100,可依具体情况分配空间*/
//#define NULL -1
typedef int datatype;
typedef struct
{
datatype stack[Maxsize];
int top;/*栈顶指针*/
}SeqStack;//顺序栈类型定义
//构造一个空栈
SeqStack *InitStack()
{
SeqStack *s;/* s为顺序栈类型变量的指针*/
s=(SeqStack *)malloc(sizeof(SeqStack));
if(!s)
{
printf("空间不足/n");
return NULL;
}
else
{
s->top=-1;
return s;
}
}
datatype GetTop(SeqStack *s)
{
if(s->top == -1)
{
printf("/n栈是空的!");
return 0;
}
else
return s->stack[s->top];
}
//入栈
SeqStack *Push(SeqStack *s,datatype x)
{
if(s->top == Maxsize-1 )
{
printf("/n栈是满的!");
return NULL;
}
else
{
s->top++;
s->stack[s->top]=x;
return s;
}
}
//出栈
datatype Pop(SeqStack *s)
{
if(s->top == -1)
{
printf("/n栈已经空了!");
return 0;
}
s->top--;
return s->stack[s->top+1];
}
//判别空栈
datatype SeqStackEmpty(SeqStack *s)
{
if(s->top ==-1)
{
printf("此栈是空栈!");
return 1;
}
else
{
printf("此栈不是空栈!");
return 0;
}
}
void display(SeqStack *p)
{
int t ;
t = p->top;
if(p->top==-1)
{
printf("/n此栈是空的");
}
else
while(t!=-1)
{
printf("%d->",p->stack[t]);
t--;
}
}
int main()
{
int arr[8]={1,2,3,4,5,6,7,8},i;
SeqStack *p;
p=InitStack();
//入栈
for(i=0;i<8;i++)
Push(p,arr[i]);
//遍历栈
printf("新栈的元素是: ");
display(p);
printf("/n");
//得栈顶元素
printf("栈顶元素是: ");
printf("%d",GetTop(p));
printf("/n");
//判别空栈
printf("判断是否为空栈?: ");
SeqStackEmpty(p);
printf("/n");
//出栈
printf("出栈元素:");
printf("%d",Pop(p));
printf("/n");
//出栈后的栈里元素是:
printf("出栈后的栈里元素是: ");
display(p);
printf("/n");
return 0;
}
#include <stdlib.h>
#define Maxsize 100 /*设顺序表的最大长度为100,可依具体情况分配空间*/
//#define NULL -1
typedef int datatype;
typedef struct
{
datatype stack[Maxsize];
int top;/*栈顶指针*/
}SeqStack;//顺序栈类型定义
//构造一个空栈
SeqStack *InitStack()
{
SeqStack *s;/* s为顺序栈类型变量的指针*/
s=(SeqStack *)malloc(sizeof(SeqStack));
if(!s)
{
printf("空间不足/n");
return NULL;
}
else
{
s->top=-1;
return s;
}
}
datatype GetTop(SeqStack *s)
{
if(s->top == -1)
{
printf("/n栈是空的!");
return 0;
}
else
return s->stack[s->top];
}
//入栈
SeqStack *Push(SeqStack *s,datatype x)
{
if(s->top == Maxsize-1 )
{
printf("/n栈是满的!");
return NULL;
}
else
{
s->top++;
s->stack[s->top]=x;
return s;
}
}
//出栈
datatype Pop(SeqStack *s)
{
if(s->top == -1)
{
printf("/n栈已经空了!");
return 0;
}
s->top--;
return s->stack[s->top+1];
}
//判别空栈
datatype SeqStackEmpty(SeqStack *s)
{
if(s->top ==-1)
{
printf("此栈是空栈!");
return 1;
}
else
{
printf("此栈不是空栈!");
return 0;
}
}
void display(SeqStack *p)
{
int t ;
t = p->top;
if(p->top==-1)
{
printf("/n此栈是空的");
}
else
while(t!=-1)
{
printf("%d->",p->stack[t]);
t--;
}
}
int main()
{
int arr[8]={1,2,3,4,5,6,7,8},i;
SeqStack *p;
p=InitStack();
//入栈
for(i=0;i<8;i++)
Push(p,arr[i]);
//遍历栈
printf("新栈的元素是: ");
display(p);
printf("/n");
//得栈顶元素
printf("栈顶元素是: ");
printf("%d",GetTop(p));
printf("/n");
//判别空栈
printf("判断是否为空栈?: ");
SeqStackEmpty(p);
printf("/n");
//出栈
printf("出栈元素:");
printf("%d",Pop(p));
printf("/n");
//出栈后的栈里元素是:
printf("出栈后的栈里元素是: ");
display(p);
printf("/n");
return 0;
}
相关文章推荐
- 自学Python之Python基础:(五)Python数据结构常用操作
- 栈的简单基本操作(数据结构)
- 数据结构初学者顺序表的操作(精简版)
- 【数据结构】C语言实现栈的进栈,出栈,遍历,清空等操作
- 数据结构面试之一——单链表常见操作
- 【数据结构】栈结构操作示例
- [数据结构]队列的基本操作
- 数据结构-栈有关操作算法
- 一步一步学习数据结构(2)-链表及其操作
- COJ 0995 WZJ的数据结构(负五)区间操作
- 数据结构单链表的实现以及相关操作
- 数据结构_完成单链表的后续操作实现
- 数据结构-单向链表相关操作算法
- 数据结构-二叉树的基本操作
- 数据结构--二叉树的创建和相关操作
- 您希望通过将所有ArrayList对象替换为相应的泛型版本以提高应用程序的效率,并使得代码更易于使用。当结构体或其他值类型存储在这些数据结构中时,会导致装箱/拆箱操作,这时就需要这么做。
- 数据结构——单向链表的基本操作C语言描述(克服对头结点和尾结点的操作)
- Python 数据结构基本操作
- 数据结构的顺序表及其操作
- 四、文件IO——内核数据结构和原子操作