堆栈的多种实现方式
Posted beauty-of-wisdom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了堆栈的多种实现方式相关的知识,希望对你有一定的参考价值。
何谓堆栈
堆栈是一种只能在一端进行插入或删除操作的线性表,属于逻辑结构。有数组与指针两种实现方式。
堆栈的主要特点为后进先出,每次进栈的新元素都在原来的栈顶元素之上,每次出栈的元素也是原来的栈顶元素。如下图:
下面给出堆栈的两种实现方式。
堆栈之指针实现:
#include<cstdio> #include<cstdlib> using namespace std; typedef struct Stack *Link; typedef struct Stack Snode; struct Stack { int data; Link next; }; Link init()//初始化栈并返回头指针 { Link p; p=NULL; return p; } Link push(Link Head,int x)//入栈 { Link p; p=(Snode*)malloc(sizeof(Snode)); if(p==NULL) { printf(" Memory Error! "); return Head; } else { p->data=x; p->next=Head; return p; } } Link pop(Link Head)//出栈 { Link p=Head; if(p==NULL) { printf(" Stack is Empty! "); return Head; } Head=Head->next; free(p); return Head; } int gettop(Link Head)//取栈顶元素的值 { if(Head==NULL) { printf(" Stack is Empty! "); return -1; } return Head->data; } bool empty(Link Head) { if(Head==NULL) return 1; return 0; } void display(Link Head) { if(Head==NULL) { printf(" Stack is Empty! "); return; } Link p; p=Head; while(p!=NULL) { printf("%d",p->data); p=p->next; } return; } int lenth(Link Head) { Link p=Head; int sum=0; while(p!=NULL) { sum++; p=p->next; } return sum; } Link Set_NULL(Link Head) { if(Head==NULL) { printf(" Stack is Already Empty! "); return Head; } Link p; p=Head; while(Head!=NULL) { p=Head; Head=Head->next; free(p); } return Head; } int main() { int i,x; Link head1; head1=init(); while(i!=6) { system("cls"); printf(" 1.Input a stack data"); printf(" 2.Output a stack data"); printf(" 3.Empty or Not"); printf(" 4.Display a top of stack"); printf(" 5.Display the lenth of stack"); printf(" 6.Exit and Free Stack "); printf(" Stack is: "); display(head1); printf(" "); scanf("%d",&i); switch(i) { case 1: while(1) { system("cls"); printf(" -.Input a stack data"); printf(" -.Output a stack data"); printf(" -.Empty or Not"); printf(" -.Display a top of stack"); printf(" -.Display the lenth of stack"); printf(" -.Exit and Free Stack "); printf(" Stack is: "); display(head1); printf(" "); printf(" input a number: until enter -1: "); scanf("%d",&x); if(x==-1) break; head1=push(head1,x); } break; case 2: head1=pop(head1); break; case 3: if(empty(head1)) printf(" Stack is empty "); else printf(" Stack is not empty "); system("pause"); break; case 4: printf(" The top is %d ",gettop(head1)); system("pause"); break; case 5: printf(" The length of stack is %d ",lenth(head1)); system("pause"); break; } } system("cls");; head1=Set_NULL(head1); display(head1); system("pause"); return 0; }
堆栈之数组实现:
//简单数组模拟栈 #include<iostream> #include<cstdlib> #define MAXN 1000//栈能容纳的最多元素个数 using namespace std; int stack[MAXN]; int top = -1;//初始化栈顶指针为-1 int pop()//栈顶元素出栈并获取出栈的元素值 { int temp; if(top<0) { cout<<" The stack is empty! "; return -1; } temp=stack[top--]; return temp; } void push(int value) { if(top>=MAXN) cout<<" The stack is full! "; else stack[++top]=value; } void display()//显示栈中元素 { for(int tmp = top ; tmp >= 0 ; -- tmp) cout<<stack[tmp]<<" "; cout<<" "; } int main() { int ins; while(1) { cout<<"Please enter a value,(0=exit,-1=pop) "; cin>>ins; if(ins==0) exit(0); else if(ins!=-1) push(ins); else if(ins==-1) pop(); system("cls"); display(); } return 0; }
其实,堆栈还有其他实现方式,我们可以通过字符串来实现字符栈呀!此时的插入与删除操作只需对串首(尾)进行添加或删除字符就OK了!
就这些了,若有不足,请指正!
以上是关于堆栈的多种实现方式的主要内容,如果未能解决你的问题,请参考以下文章