使用链接列表堆叠在C++中的实现

时间:2020-02-23 14:41:54  来源:igfitidea点击:

在本文中,我们将看到如何使用链接列表在C++中实现堆栈。

堆叠作为抽象数据类型

堆栈是一个有序数据结构,用于存储Lifo中的数据类型(首先换止)订单。
这意味着进入持续的元素首先退出(已处理)。
这就像一堆同心戒指一遍左侧。
当然,当我们开始逐个(河内塔)时,将首先挑选最后一个。
堆栈是一种类似的数据类型,其中插入和删除都在同一侧完成,即顶部。
堆栈只持有类似的数据类型。

堆栈的操作

type_t =任何数据类型

基本操作:

Push(Type_t data)::它将数据类型Type_t的数据插入到堆栈中

Type_t Pop():从堆栈中删除最顶部元素并返回它

其他操作:

Type_t top(): 返回最顶层元素

bool isEmpty():如果堆栈为空,则返回true,否则返回false

bool isFull():返回false如果堆栈已满,否则返回false

int size():返回堆栈的大小

使用单链列表堆叠实现

链接列表本身是一个数据结构,其中每个元素都有数据和指针(下一个)指向下一个元素。

|数据|下一个指针|

如上所述的每个节点都是堆栈元素

class stackNode {
//class for stack element which is SLL
  public:
    int data; //data
    stackNode *next; //next pointer
    stackNode(int a) { //constructor
        data = a;
        next = NULL;
    }
};

准备工作

  • 单链列表类/结构
  • 顶尖指针

让我们来检查一个示例以了解推送和流行实现

例子:

让插入的元素是

1,2,3

因此,要使用链接列表实现堆栈,

当我们推动我们实际执行的元素时,请在头单链列表中插入元素。

当我们弹出一个元素我们实际执行,从单链列表的头部删除元素。

C++实现:

#include <bits/stdc++.h>
using namespace std;
 
 
class stackNode { 
//class for each node which is a single stack element
  public:
    int data;
    stackNode *next;
    stackNode(int a) { //constructor
        data = a;
        next = NULL;
    }
};
 
stackNode *top=NULL; //top of stack initialized to NULL
int size=0;//size of stack initialized to 0
 
 
void push(int x) { //push operation
 
    stackNode* node=(stackNode*)(malloc(sizeof(stackNode)));
    node->data=x;
    node->next=top;
    top=node;
    cout<<x<<" is pushed\n";
    size++;
}
 
bool isEmpty(){ //isEmpty function
  if(top==NULL && size==0)
  return true;
  else
  return false;
}
 
int pop() { //pop operation
 
    if(isEmpty()){
      cout<<"stack is empty\n";
      return INT_MIN;
    }
    else{
        size--;
        int temp=top->data;
        stackNode* tempNode=top;
        top=top->next;
        free(tempNode);
        return temp;
    }
}
 
int top_stack(){ //top() operation
  if(isEmpty()){
    cout<<"stack is empty\n";
    return INT_MIN;
  }
  else{
    return top->data;
  }
 
}
 
//main function
int main(){
 
  //menu for operations
  //press 1 for push (with data)
  //press 2 for pop()
  //press 3 for top()
  //press 4 for size()
  //press 0 to exit()
 
 
 
  cout<<"press 1 for push\n";
  cout<<"press 2 for pop()\n";
  cout<<"press 3 for top()\n";
  cout<<"press 4 for size()\n";
  cout<<"press 0 for exit\n";
 
 
  int choice;
  cout<<"press your choice\n";
  cin>>choice;
  while(choice){
    if(choice==1){
      int data;
      cout<<"Enter element\n";
      cin>>data;
      push(data);
    }
    else if(choice==2){
      int item=pop();
      if(item==INT_MIN){}
      else
      cout<<"Popped element: "<<item<<endl;
    }
    else if(choice==3){
      int item=top_stack();
      if(item==INT_MIN){}
      else
      cout<<"Top element: "<<item<<endl;
    }
    else if(choice==4){
      cout<<"Size is: "<<size<<endl;
    }
    else
    cout<<"Invalid number, try again!\n";
    cout<<"press your choice\n";
    cin>>choice;
 
  }
 
  cout<<"Exiting...\n";
  return 0;
}