📜  在C++中使用类模板实现堆栈

📅  最后修改于: 2021-05-30 03:59:48             🧑  作者: Mango

任务是使用C++中的类模板实现一些重要的堆栈功能,例如pop(),push(),display(),topElement(),isEmpty(),isFull()。堆栈是遵循特定操作顺序的线性数据结构。顺序可以是LIFO(后进先出)或FILO(后进先出)。

简单的想法是将数据类型作为参数传递,这样我们就不必为不同的数据类型编写相同的代码。例如,一家软件公司可能需要对不同的数据类型使用sort()。无需编写和维护多个代码,我们可以编写一个sort()并将数据类型作为参数传递。
C++添加了两个新的关键字来支持模板: ‘template’‘typename’ 。第二个关键字始终可以用关键字“ class ”代替。

插图:

例子:

C++14
// C++ Program to Implement stack using Class Templates
  
// Including input output libraries
#include 
// Header File including all string functions
#include 
  
using namespace std;
  
// Taking size of stack as 10
#define SIZE 5
  
// Class
// To represent stack using template by class
// taking class in template
template  class Stack {
    // Pubic access modifier
public:
    // Empty constructor
    Stack();
  
    // Method 1
    // To add element to stack which can be any type
    // using stack push() operation
    void push(T k);
  
    // Method 2
    // To remove top element from stack
    // using pop() operation
    T pop();
  
    // Method 3
    // To print top element in stack
    // using peek() method
    T topElement();
  
    // Method 4
    // To check whether stack is full
    // using isFull() method
    bool isFull();
  
    // Method 5
    // To check whether stack is empty
    // using isEmpty() method
    bool isEmpty();
  
private:
    // Taking data member top
    int top;
  
    // Intialising stack(array) of given size
    T st[SIZE];
};
  
// Method 6
// To initialise top to
// -1(default constructor)
template  Stack::Stack() { top = -1; }
  
// Method 7
// To add element element k to stack
template  void Stack::push(T k)
{
  
    // Checking whether stack is completely filled
    if (isFull()) {
        // Display message when no elements can be pushed
        // into it
        cout << "Stack is full\n";
    }
  
    // Inserted element
    cout << "Inserted element " << k << endl;
  
    // Incrementing the top by unity as element
    // is to be inserted
    top = top + 1;
  
    // Now, adding element to stack
    st[top] = k;
}
  
// Method 8
// To check if the stack is empty
template  bool Stack::isEmpty()
{
    if (top == -1)
        return 1;
    else
        return 0;
}
  
// Utility methods
  
// Method 9
// To check if the stack is full or not
template  bool Stack::isFull()
{
    // Till top in inside the stack
    if (top == (SIZE - 1))
        return 1;
    else
  
        // As top can not exceeeds th size
        return 0;
}
  
// Method 10
template  T Stack::pop()
{
    // Intialising a variable to store popped element
    T popped_element = st[top];
  
    // Decreasing the top as
    // element is getting out from the stack
    top--;
  
    // Returning the element/s that is/are popped
    return popped_element;
}
  
// Method 11
template  T Stack::topElement()
{
    // Intialising a variable to store top element
    T top_element = st[top];
  
    // Returning the top element
    return top_element;
}
  
// Method 12
// Main driver method
int main()
{
  
    // Creating object of Stack class in main() method
    // Dec;aring objects of type Integer and String
    Stack integer_stack;
    Stack string_stack;
  
    // Adding elements to integer stack object
    // Custom integer entries
    integer_stack.push(2);
    integer_stack.push(54);
    integer_stack.push(255);
  
    // Adding eleements to string stack
    // Custom string entries
    string_stack.push("Welcome");
    string_stack.push("to");
    string_stack.push("GeeksforGeeks");
  
    // Now, removing element from integer stack
    cout << integer_stack.pop() << " is removed from stack"
         << endl;
  
    // Removing top element from string stack
    cout << string_stack.pop() << " is removed from stack "
         << endl;
  
    // Print and display the top element in integer stack
    cout << "Top element is " << integer_stack.topElement()
         << endl;
  
    // Print and display the top eleement in string stack
    cout << "Top element is " << string_stack.topElement()
         << endl;
  
    return 0;
}


输出
Inserted element 2
Inserted element 54
Inserted element 255
Inserted element Welcome
Inserted element to
Inserted element GeeksforGeeks
255 is removed from stack
GeeksforGeeks is removed from stack 
Top element is 54
Top element is to

输出说明:

这里,使用单个堆栈类实现了两种数据类型(integer和字符串)。首先,使用两个对象,一个用于整数类,第二个用于字符串类,使用堆栈类的push()isFull()方法元素插入这两个类中。使用堆栈类的pop和isEmpty()函数删除元素。最后,使用top element()函数为每个类打印top元素。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”