Stacks implementation

Stacks implementation

本文关键字:implementation Stacks      更新时间:2023-10-16

我很难理解推送和弹出是如何使用堆栈的。我理解它们是如何工作的,比如说,它们把一个数字推到堆栈上,最后一个推上的数字就会弹出。我也理解指针背后的逻辑以及它们是如何运作的。我不明白的是代码应该如何编写。

我的程序应该让用户创建一个堆栈(确定它有多大),然后选择在堆栈上推送内存(数字)或将其弹出

这是我到目前为止得到的东西,我被卡住了。我通过cplusplus.com进行了研究,阅读了几乎所有关于这些东西的内容,但仍然无法弄清楚程序应该如何布局和运行。

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
int choice;
int * arrPtr = NULL;
int * stack;
int * top;
int val, capacity, size; 
//stack would point to the location of the first element,
// top would point to the location where the next element will be stored,
// capacity is the number of elements that can be stored in the stack,
// size would be the number of elements actually stored in the stack
int main()
{
//This is the menu
cout << "1. Create" << endl;
cout << "2. Push" << endl;
cout << "3. Pop" << endl;
cout << "4. Count" << endl;
cout << "5. Display" << endl;
cout << "6. Exitn" << endl;
cin >> choice;
//I figured i would use choices for the menu and went ahead and wrote it out
switch(choice)
{
    case 1:
        create();
        break;
    case 2:
        push();
        break;
    case 3:
        pop();
        break;
    case 4:
        count(0);
        break;
    case 5:
        display();
        break;
    case 6:
        exit();
    default:
        cout << "Please enter correct choice (1-4)!";
        break;
    }
    return 0;   
}   //end main
void create()
{
    cout << "Enter the size of the stack you wish to create: ";
    int capacity = 0;
    cin >> capacity;
    arrPtr = new int[capacity];
} //end create function
//From here it went wrong, I cannot figure it out.           
void push(){
    for (int i = 0; i < capacity; i++)
    {
        cout << "Enter the number you wish to put on the stack: ";
        cin >> val;
        push(val)
    }//end for
}//end push

请帮我理解这一点。

创建std::array。创建迭代器。将迭代器设置到数组的末尾。(端义array.end()

Push:递减迭代器,插入值
Pop:返回值,递增迭代器
计数:在堆栈上不是标准的,但您可以通过从末尾减去当前迭代器来获得它
Peek:返回值

显然,你想确保你没有推离数组的前面,或者从后面弹出,所以你应该添加一些检查。

堆叠真的很简单。希望这能有所帮助。

编辑:实现,未经测试的代码,有点像这个的思想流

template <typename T, std::size_t N>
class Stack {
public:
  Stack();
  void push(T value);
  T pop();
  T peek() const;
  std::size_t count() const;
private:
  std::array<T, N> m_baseArray;
  std::array<T>::iterator m_it;
};
template <typename T, std::size_t N>
Stack::Stack() : m_baseArray(), m_it(m_baseArray.end()) { }
template <typename T, std::size_t N>
void Stack::push(T value) {
  if (m_it == m_baseArray.begin())
    throw std::exception();
  --m_it;
  *m_it = value;
}
template <typename T, std::size_t N>
T Stack::pop() {
  if (m_it == m_baseArray.end())
    throw std::exception();
  T res = *m_it;
  ++m_it;
  return res;
}
template <typename T, std::size_t N>
T Stack::peek() const {
  if (m_it == m_baseArray.end())
    throw std::exception();
  return *m_it;
}
template <typename T, std::size_t N>
std::size_t Stack::count() const {
  return m_baseArray.end() - m_it;
}

使用std::vector 的最简单堆栈

#include <iostream>
#include <vector>
using namespace std;
template<typename T>
class Stack{
private: 
  vector<T> theArray;
public:
  Stack(){ theArray.clear(); }
  void push(T data) { theArray.push_back(data); }
  T pop() { T retData = theArray.back(); theArray.pop_back(); return retData; }
  void display() {
    for(size_t i = theArray.size() - 1; i != (size_t)-1; i--){
      cout << theArray[i] << 'n';
    }
  }
};
int main(int argc, char *argv[])
{
  Stack<int> s;
  s.push(10);
  s.push(20);
  s.push(30);
  s.display();
  int ret = s.pop();
  cout << "After popping : " << ret << 'n';
  s.display();
  return 0;
}

我在Mac上编译了这段代码,但它应该在安装了bash的PC上的Linux上正常工作。

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
int choice;
int capacity; /*size of stack*/ 
int stack[1000];/*THIS IS WHERE ALL THE STACK ENTRIES WILL BE STORED*/
int top=0; /* THIS WILL KEEP CHECK OF THE INDEX OF TOP MOST ELEMENT */
//stack would point to the location of the first element,
// top would point to the location where the next element will be stored,
// capacity is the number of elements that can be stored in the stack,
// size would be the number of elements actually stored in the stack
void create() //Set size of stack
{
    cout << "Enter the size of the stack you wish to create: ";
    cin >> capacity;
} //end create function

void push(int n) //Enter data into stack
{
/* Ensures that there isn't a stack overflow */
    if(top<capacity)
    {
        stack[top++]=n; 
    }

    else
    {
        cout<<"Stack FULL!!n";
    }
    //end for
}//end push
void pop() //Remove data from stack
{
    /* Ensures that there isn't a stack underflow */
    if(top>0)
    {
        cout<<"Popped entry is:"<<stack[top-1]<<"n";
        top--;   
    }
    else
    {
        cout<<"Stack is EMPTY!!n";
    }

}
void count() //Count number of elements
{
    cout<<"Stack size is:"<<top<<"n"; 
}
void display() //Print elements from lowest stack entry to highest 
{
    int i=0;
    cout<<"Your stack is:n";
    while(i<top)
    {
        cout<<i+1<<") "<<stack[i]<<"n";
        i++;
    }
}

int main()
{
    system("clear");  //Start with blank screen

    int exitCheck=1,entry;
    //I figured i would use choices for the menu and went ahead and wrote it out -- This is good approach!!
    cout<<"Welcome user n";
    create(); /*Size of stack can be set only once to prevent inconsistency */
    while(exitCheck == 1)  /* This is the menu */
    {
        cout << "1. Push" << endl;
        cout << "2. Pop" << endl;
        cout << "3. Count" << endl;
        cout << "4. Display" << endl;
        cout << "5. Exitn" << endl;
        cin >> choice; //Choice should be placed here as we need userinput during each turn 
        switch(choice)
        {
            case 1:
                cout<< "Enter your data: ";
                cin>>entry;
                push(entry);
                break;
            case 2:
                pop();
                break;
            case 3:
                count();
                break;
            case 4:
                display();
                break;
            case 5:
                {
                    exitCheck=1;
                    break;
                } /*exit in this case wont achieve a proper result in a concole based app thats why i replaced it with loop exit condition */
            default:
                cout << "Please enter correct choice (1-5)!n";
                break;
            }
        cout<< "Enter 1 to continue else anything else to quit:n";
        cin>> exitCheck;

    }
    cout<<"Thanks for using this code!!n";    
return 0;   
}
  //end main

在这个特定的程序中,所有可以执行的基本操作都是使用数组实现的。

这里,数组是私有的,因此不能使用常规数组操作对数组进行操作,而只能在公共函数(即堆栈操作)的帮助下进行操作。

阵列的容量最初取为4,每次阵列容量超过动态分配时都会加倍。

#include<iostream>
using namespace std;
class StackUsingArray
{
    int * data;
    int nextInput;
    int capacity;
    
    
    public:
    StackUsingArray()
    {
        data = new int[4];
        nextInput = 0;
        capacity = 4; //lets just assign a random capacity and later we double it everytime the capacity is used up
    }
    
    void push(int element)
    {
        if(nextInput == capacity)
        {
            cout<<"The capacity of the stack was full but is now doubled so don't worry ! "<<endl;
            int * newData = new int[2 * capacity];
            for(int i = 0; i < capacity; i++)
            {
                newData[i] = data[i];
            }
            delete [] data;
            data = newData;
            capacity = capacity * 2;
        }
        data[nextInput] = element;
        nextInput++;
    }
    
    void pop()
    {
        if(nextInput == 0)
        {
            cout<<"The stack is empty !"<<endl;
            return;
        }
        nextInput--;
        cout<<"The poped element is : "<<data[nextInput]<<endl;
    }
    
    //this is the function that is used to display the elements in the stack
    void display()
    {
        if(nextInput == 0)
        {
            cout<<"The stack is empty !"<<endl;
            return;
        }
        for(int i = 0; i < nextInput; i++)
        {
            cout<<data[i]<<" ";
        }
        cout<<endl;
    }
    
    //this fuction just displays the element at the top of the stack
    void top()
    {
        if(nextInput == 0)
        {
            cout<<"The stack is empty !"<<endl;
            return;
        }
        cout<<"The element on top of the stack is : "<<data[nextInput - 1]<<endl;
    }
    
    //the function returns the size of the stack that is the number of elements in the stack
    void size()
    {
        cout<<"The total number of elements in the stack is : "<<nextInput<<endl;
    }
    
    //this function helps us to know wether the stack is empty or not
    void isEmpty()
    {
        if(nextInput == 0)
        {
            cout<<"The stack is empty !"<<endl;
        }
        else
        {
            cout<<"The stack is not empty !"<<endl;
        }
    }
};

int main()
{
    int n,element;
    StackUsingArray s;
    while(true)
    {
        cout<<"**********************************************************************************************"<<endl;
        cout<<"1. Push"<<endl<<"2. Pop"<<endl<<"3. Display"<<endl<<"4. Top"<<endl<<"5. size"<<endl<<"6. Empty??"<<endl<<"7. Exit"<<endl;
        cout<<"**********************************************************************************************"<<endl;
        cout<<"Chose the opertaion you wish to perform on the stack : ";
        cin>>n;
        cout<<"**********************************************************************************************"<<endl;
        switch(n)
        {
            case 1: cout<<"Enter the data you want to insert on top of the stack : ";
                    cin>>element;
                    s.push(element);
                    break;
            case 2: s.pop();
                    break;
            case 3: s.display();
                    break;
            case 4: s.top();
                    break;
            case 5: s.size();
                    break;
            case 6: s.isEmpty();
                    break;
            case 7: exit(0);
            default: cout<<"Enter a valid choice ! "<<endl;
                    break;
        }
    }
}