C++ USES the linked list to write a simple stack example

  • 2020-05-19 05:25:03
  • OfStack

There are actually template classes for stack in C++. More powerful.

Writing your own stack will help you become more familiar with the data structure of the stack. One drawback to this stack is that it can only hold elements of type int.


#include <iostream>
using namespace std;
class Stack
{
private:
  struct Node
  {
    int data;
    Node *next;
  };
  Node *head;
  Node *p;
  int length;

public:
  Stack()
  {
    head = NULL;
    length = 0;
  }
  void push(int n)// Into the stack 
  {
    Node *q = new Node;
    q->data = n;
    if (head == NULL)
    {
      q->next = head;
      head = q;
      p = q;
    }
    else
    {
      q->next = p;
      p = q;
    }
    length ++;
  }

  int pop()// Push out and return the pushed element 
  {
    if (length <= 0)
    {
      abort();
    }
    Node *q;
    int data;
    q = p;
    data = p->data;
    p = p->next;
    delete(q);
    length --;
    return data;
  }
  int size()// Returns the number of elements 
  {
    return length;
  }
  int top()// Returns the top element of the stack 
  {
    return p->data;
  }
  bool isEmpty()// Determine if the stack is empty 
  {
    if (length == 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  void clear()// Empty the stack of all elements 
  {
    if (length > 0)
    {
      pop();
    }
  }
};
int main()
{
  // Here is the test code 
  Stack s;
  s.push(1);
  s.push(2);
  s.push(3);
  while(!s.isEmpty())
  {
    cout<<s.pop()<<endl;
  }
  return 0;
}

With a few changes to this code, the stack can hold other types of elements


#include <iostream>
using namespace std;
template<class T>class Stack
{
private:
  struct Node
  {
    T data;
    Node *next;
  };
  Node *head;
  Node *p;
  int length;

public:
  Stack()
  {
    head = NULL;
    length = 0;
  }
  void push(T n)// Into the stack 
  {
    Node *q = new Node;
    q->data = n;
    if (head == NULL)
    {
      q->next = head;
      head = q;
      p = q;
    }
    else
    {
      q->next = p;
      p = q;
    }
    length ++;
  }

  T pop()// Push out and return the pushed element 
  {
    if (length <= 0)
    {
      abort();
    }
    Node *q;
    int data;
    q = p;
    data = p->data;
    p = p->next;
    delete(q);
    length --;
    return data;
  }
  int size()// Returns the number of elements 
  {
    return length;
  }
  T top()// Returns the top element of the stack 
  {
    return p->data;
  }
  bool isEmpty()// Determine if the stack is empty 
  {
    if (length == 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  void clear()// Empty the stack of all elements 
  {
    while(length > 0)
    {
      pop();
    }
  }
};
int main()
{
  Stack<char> s;
  s.push('a');
  s.push('b');
  s.push('c');
  while(!s.isEmpty())
  {
    cout<<s.pop()<<endl;
  }
  return 0;
}

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: