堆栈的多种实现方式

何谓堆栈

  堆栈是一种只能在一端进行插入或删除操作的线性表,属于逻辑结构。有数组与指针两种实现方式。

  堆栈的主要特点为后进先出,每次进栈的新元素都在原来的栈顶元素之上,每次出栈的元素也是原来的栈顶元素。如下图:

堆栈

  下面给出堆栈的两种实现方式。

堆栈之指针实现:

#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("\nMemory Error!\n");
        return Head;
    }
    else {
        p->data=x;
        p->next=Head;
        return p;
    }
}
Link pop(Link Head)//出栈 
{
    Link p=Head; 
    if(p==NULL)
    {
        printf("\nStack is Empty!\n");
        return Head;
    }
    Head=Head->next;
    free(p);
    return Head;
}
int gettop(Link Head)//取栈顶元素的值 
{
    if(Head==NULL)
    {
        printf("\nStack is Empty!\n");
        return -1;
    }
    return Head->data;
}

bool empty(Link Head)
{
    if(Head==NULL) return 1;
    return 0;
}

void display(Link Head)
{
    if(Head==NULL)
    {
        printf("\nStack is Empty!\n");
        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("\nStack is Already Empty!\n");
        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("\n 1.Input a stack data");
    printf("\n 2.Output a stack data");
    printf("\n 3.Empty or Not");
    printf("\n 4.Display a top of stack");
    printf("\n 5.Display the lenth of stack");
    printf("\n 6.Exit and Free Stack\n");
    printf("\n  Stack is:  ");
    display(head1);
    printf("\n");

    scanf("%d",&i);
    switch(i)
    {
      case 1: while(1)
              {     
                system("cls");
                printf("\n -.Input a stack data");
                printf("\n -.Output a stack data");
                printf("\n -.Empty or Not");
                printf("\n -.Display a top of stack");
                printf("\n -.Display the lenth of stack");
                printf("\n -.Exit and Free Stack\n");
                printf("\n  Stack is:  ");
                display(head1);
                printf("\n");
                printf("\ninput a number: until enter -1:\n");
                scanf("%d",&x);
                if(x==-1)
                  break;
                head1=push(head1,x);
              }
              break;
     case 2: head1=pop(head1);
             break;
     case 3: if(empty(head1))
               printf("\nStack is empty\n");
             else
               printf("\nStack is not empty\n");
             system("pause");
             break;
     case 4: printf("\n The top is  %d\n",gettop(head1));
             system("pause");
             break;
     case 5: printf("\n The length of stack is %d\n",lenth(head1));
             system("pause");
             break;
    }
  }
  system("cls");;
  head1=Set_NULL(head1);
  display(head1);
  system("pause");
  return 0;
}
View Code

 堆栈之数组实现:

//简单数组模拟栈 
#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<<"\nThe stack is empty!\n";
    return -1;
  }
  temp=stack[top--];
  return temp;
}

void push(int value)
{ 
  if(top>=MAXN)
    cout<<"\nThe stack is full!\n";
  else
    stack[++top]=value;
}

void display()//显示栈中元素
{
  for(int tmp = top ; tmp >= 0 ; -- tmp)
    cout<<stack[tmp]<<" ";
  cout<<"\n";
}

int main()
{
  int ins;
  while(1)
  {
    cout<<"Please enter a value,(0=exit,-1=pop)\n";
    cin>>ins;
    if(ins==0)
      exit(0);
    else if(ins!=-1)
      push(ins);
    else if(ins==-1)
      pop();
    system("cls");
    display();
  }  
  return 0;
}
View Code

  其实,堆栈还有其他实现方式,我们可以通过字符串来实现字符栈呀!此时的插入与删除操作只需对串首(尾)进行添加或删除字符就OK了!

  就这些了,若有不足,请指正!

猜你喜欢

转载自www.cnblogs.com/Beauty-of-wisdom/p/10090370.html