栈的基本操作
2018-03-15 13:47
211 查看
堆栈(简称栈)是一种最常用和最重要的数据结构,是一种只能在一端进行插入或删除数据操作的线性表。表中允许进行插入、删除操作的一端称为栈顶。栈顶当前位置是动态的,栈顶当前位置由一个称为栈顶指针的位置指示器表示。表的另一端称为栈底。当栈中没有数据元素时,称为空栈。栈的插入操作通常称为进栈或入栈。栈的删除操作通常称为退栈或出栈。
栈的主要特点是“后进先出”,及后入栈的元素先弹出。每次进栈的数据元素都放在原当前栈顶元素之上,成为新的栈顶元素,每次出栈的数据都是原当前栈顶元素。
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef int ElemType;
typedef struct Stack *link;
typedef struct Stack Snode;
struct Stack // 初始化设置
{
ElemType data;
struct Stack *next;
};
link init() //初始化栈
{
link p;
p=NULL;
return p;
}
link push (link Head,ElemType x) //入栈
{
link p;
p=(link)malloc(sizeof(Snode));
if(p==NULL)
{
cout<<"\nMemory Error\n";
return Head;
}
p->data=x;
p->next=Head;
return p;
}
link pop(link Head) //出栈
{
link p;
p=Head;
if(p==NULL)
cout<<"\nStack is Empty\n";
else
{
p=p->next;
free(Head);
}
return p;
}
int lenth(link Head) //获得栈内元素个数
{
int len=0;
link p;
p=Head;
while(p!=NULL)
{
len++;
p=p->next;
}
return len;
}
ElemType gettop (link Head) //取栈顶元素
{
if(Head==NULL)
{
cout<<"Stack is Empty!\n";
return -1;
}
else
return Head->data;
}
int empty (link Head)//判断栈是否为空
{
if(Head==NULL)
return 1;
else
return 0;
}
void display (link Head) //显示栈元素
{
link p;
p=Head;
if(p==NULL)
cout<<"\nStack is empty\n";
else
do
{
cout<<p->data<<" ";
p=p->next;
}
while(p!=NULL);
}
link setnull(link Head) //释放栈
{
link p;
p=Head;
while(p!=NULL)
{
p=p->next;
free(Head);
Head=p;
}
return Head;
}
int main()
{
int i,x;
link head1;
head1=init();
while(i!=6)
{
cout<<"\n1.Input a stack data";
cout<<"\n2.Output a stack data";
cout<<"\n3.Empty or Not";
cout<<"\n4.Display a top of stack";
cout<<"\n5.Display and the length of stack";
cout<<"\n6.Exit and Free Stack\n";
cout<<"\n Stack is: ";
display(head1);
cout<<"\n";
cin>>i;
switch(i)
{
case 1:
while(1)
{
system("cls");
cout<<"\n-.Input a stack data";
cout<<"\n-.Output a stack data";
cout<<"\n-.Empty or Not";
cout<<"\n-.Display a top of stack";
cout<<"\n-.Exit and Free Stack\n";
cout<<"\n-.Stack is ";
display (head1);
cout<<"\n";
cout<<"\nInput a number : Until Enter -1:\n";
cin>>x;
if(x==1)break;
head1=push(head1,x);
}
break;
case 2:
head1=pop(head1);
break;
case 3:
if(empty(head1))
cout<<"\nStack is Empty\n";
else
cout<<"\nStack is not Empty\n";
system("pause");
break;
case 4:
cout<<"\n The Top is "<<gettop(head1)<<endl;
system("pause");
break;
case 5:
cout<<"\nThe Length of Stack is "<<lenth(head1)<<endl;
system("cls");
break;
}
}
system("cls");
head1=setnull(head1);
display(head1);
getchar();
return 0;
}
栈的主要特点是“后进先出”,及后入栈的元素先弹出。每次进栈的数据元素都放在原当前栈顶元素之上,成为新的栈顶元素,每次出栈的数据都是原当前栈顶元素。
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef int ElemType;
typedef struct Stack *link;
typedef struct Stack Snode;
struct Stack // 初始化设置
{
ElemType data;
struct Stack *next;
};
link init() //初始化栈
{
link p;
p=NULL;
return p;
}
link push (link Head,ElemType x) //入栈
{
link p;
p=(link)malloc(sizeof(Snode));
if(p==NULL)
{
cout<<"\nMemory Error\n";
return Head;
}
p->data=x;
p->next=Head;
return p;
}
link pop(link Head) //出栈
{
link p;
p=Head;
if(p==NULL)
cout<<"\nStack is Empty\n";
else
{
p=p->next;
free(Head);
}
return p;
}
int lenth(link Head) //获得栈内元素个数
{
int len=0;
link p;
p=Head;
while(p!=NULL)
{
len++;
p=p->next;
}
return len;
}
ElemType gettop (link Head) //取栈顶元素
{
if(Head==NULL)
{
cout<<"Stack is Empty!\n";
return -1;
}
else
return Head->data;
}
int empty (link Head)//判断栈是否为空
{
if(Head==NULL)
return 1;
else
return 0;
}
void display (link Head) //显示栈元素
{
link p;
p=Head;
if(p==NULL)
cout<<"\nStack is empty\n";
else
do
{
cout<<p->data<<" ";
p=p->next;
}
while(p!=NULL);
}
link setnull(link Head) //释放栈
{
link p;
p=Head;
while(p!=NULL)
{
p=p->next;
free(Head);
Head=p;
}
return Head;
}
int main()
{
int i,x;
link head1;
head1=init();
while(i!=6)
{
cout<<"\n1.Input a stack data";
cout<<"\n2.Output a stack data";
cout<<"\n3.Empty or Not";
cout<<"\n4.Display a top of stack";
cout<<"\n5.Display and the length of stack";
cout<<"\n6.Exit and Free Stack\n";
cout<<"\n Stack is: ";
display(head1);
cout<<"\n";
cin>>i;
switch(i)
{
case 1:
while(1)
{
system("cls");
cout<<"\n-.Input a stack data";
cout<<"\n-.Output a stack data";
cout<<"\n-.Empty or Not";
cout<<"\n-.Display a top of stack";
cout<<"\n-.Exit and Free Stack\n";
cout<<"\n-.Stack is ";
display (head1);
cout<<"\n";
cout<<"\nInput a number : Until Enter -1:\n";
cin>>x;
if(x==1)break;
head1=push(head1,x);
}
break;
case 2:
head1=pop(head1);
break;
case 3:
if(empty(head1))
cout<<"\nStack is Empty\n";
else
cout<<"\nStack is not Empty\n";
system("pause");
break;
case 4:
cout<<"\n The Top is "<<gettop(head1)<<endl;
system("pause");
break;
case 5:
cout<<"\nThe Length of Stack is "<<lenth(head1)<<endl;
system("cls");
break;
}
}
system("cls");
head1=setnull(head1);
display(head1);
getchar();
return 0;
}
相关文章推荐
- SQL基本操作
- Linux文件基本操作
- linux下mysql基本的操作
- MongoDB数据库和集合的基本操作
- QThread类基本操作 (补充)
- Git基本操作
- Maven学习记录之maven基本操作命令,maven本地工厂的创建,maven骨架的生成,以及在eclipse中创建maven工程:
- 顺序表基本操作C++实现
- vim编辑器基本操作
- C语言实现栈的基本操作
- svn基本操作
- python学习第一讲(基本的操作)
- Git基本操作
- 二叉树的基本操作
- HibernateTemplate提供非常多的常用方法来完成基本的操作
- 单链表基本操作
- MongoDB基本操作
- 二叉树的基本操作
- java对File基本操作
- 学习二:MySql数据库的基本操作