Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
stack.h
1 /* This class specifies the basic operation on a stack as a linked list */
2 #ifndef DATA_STRUCTURES_STACK_H_
3 #define DATA_STRUCTURES_STACK_H_
4 
5 #include <cassert>
6 #include <iostream>
7 
8 /* Definition of the node */
9 template <class Type>
10 struct node {
11  Type data;
12  node<Type> *next;
13 };
14 
15 /* Definition of the stack class */
16 template <class Type>
17 class stack {
18  public:
19  /** Show stack */
20  void display() {
21  node<Type> *current = stackTop;
22  std::cout << "Top --> ";
23  while (current != NULL) {
24  std::cout << current->data << " ";
25  current = current->next;
26  }
28  std::cout << "Size of stack: " << size << std::endl;
29  }
30 
31  /** Default constructor*/
32  stack() {
33  stackTop = NULL;
34  size = 0;
35  }
36 
37  /** Destructor */
38  ~stack() {}
39 
40  /** Determine whether the stack is empty */
41  bool isEmptyStack() { return (stackTop == NULL); }
42 
43  /** Add new item to the stack */
44  void push(Type item) {
45  node<Type> *newNode;
46  newNode = new node<Type>;
47  newNode->data = item;
48  newNode->next = stackTop;
49  stackTop = newNode;
50  size++;
51  }
52 
53  /** Return the top element of the stack */
54  Type top() {
55  assert(stackTop != NULL);
56  return stackTop->data;
57  }
58 
59  /** Remove the top element of the stack */
60  void pop() {
61  node<Type> *temp;
62  if (!isEmptyStack()) {
63  temp = stackTop;
64  stackTop = stackTop->next;
65  delete temp;
66  size--;
67  } else {
68  std::cout << "Stack is empty !" << std::endl;
69  }
70  }
71 
72  /** Clear stack */
73  void clear() { stackTop = NULL; }
74 
75  /** Overload "=" the assignment operator */
76  stack<Type> &operator=(const stack<Type> &otherStack) {
77  node<Type> *newNode, *current, *last;
78 
79  /* If stack is no empty, make it empty */
80  if (stackTop != NULL) {
81  stackTop = NULL;
82  }
83  if (otherStack.stackTop == NULL) {
84  stackTop = NULL;
85  } else {
86  current = otherStack.stackTop;
87  stackTop = new node<Type>;
88  stackTop->data = current->data;
89  stackTop->next = NULL;
90  last = stackTop;
91  current = current->next;
92  /* Copy the remaining stack */
93  while (current != NULL) {
94  newNode = new node<Type>;
95  newNode->data = current->data;
96  newNode->next = NULL;
97  last->next = newNode;
98  last = newNode;
99  current = current->next;
100  }
101  }
102  size = otherStack.size;
103  return *this;
104  }
105 
106  private:
107  node<Type> *stackTop; /**< Pointer to the stack */
108  int size;
109 };
110 
111 #endif // DATA_STRUCTURES_STACK_H_
stack::pop
void pop()
Definition: stack.h:60
node
Definition: avltree.cpp:13
node
struct list node
std::queue
STL class.
std::cout
stack_linkedList
Definition: queue_using_linkedlist.cpp:10
linkedlist
Definition: queue_using_linkedlist.cpp:6
stack::push
void push(Type item)
Definition: stack.h:44
stack::top
Type top()
Definition: stack.h:54
data
int data[MAX]
test data
Definition: hash_search.cpp:24
std::endl
T endl(T... args)
stack::stack
stack()
Definition: stack.h:32
stack::operator=
stack< Type > & operator=(const stack< Type > &otherStack)
Definition: stack.h:76
std
STL namespace.
show
void show(const struct tower *const F, const struct tower *const T, const struct tower *const U)
Definition: tower_of_hanoi.cpp:19
main
int main()
Definition: avltree.cpp:134
stack
Definition: stack.h:17
Queue_Array
Definition: queue_using_array.cpp:13
stack::display
void display()
Definition: stack.h:20
stack::isEmptyStack
bool isEmptyStack()
Definition: stack.h:41
stack::stackTop
node< Type > * stackTop
Definition: stack.h:107
stack::~stack
~stack()
Definition: stack.h:38
std::cin
std::exit
T exit(T... args)
stack::clear
void clear()
Definition: stack.h:73
std::next
T next(T... args)