C++ USES template classes to implement the chain stack
- 2020-08-22 22:36:50
- OfStack
This article examples for you to share C++ use template class to achieve the chain stack specific code, for your reference, the specific content is as follows
1. Implementation procedures:
1.Stack.h
#ifndef Stack_h
#define Stack_h
template <class T>
class Stack {
public:
Stack(){}; // The constructor
void Push(const T x); // New elements are added
bool Pop(); // Top element out of the stack
virtual bool getTop(T &x) const = 0; // Read top of stack elements by x return
virtual bool isEmpty() const = 0; // Determines whether the stack is empty
// virtual bool isFull() const = 0; // Determine if stack is full or not , Because there is no dissatisfaction with the chain stack
virtual int getSize() const = 0; // Count the number of elements in the stack
};
#endif /* Stack_h */
2.LinkedStack.h
#ifndef LinkedStack_h
#define LinkedStack_h
#include <iostream>
#include "Stack.h"
using namespace std;
template <class T>
struct LinkNode {
T data;
LinkNode<T> *link;
};
// Predeclaration of a class
template <class T>
class LinkedStack;
// Declaration of a friend function
template <class T>
ostream& operator<<(ostream& out, LinkedStack<T>& s);
template <class T>
class LinkedStack: public Stack<T> {
public:
LinkedStack(); // The constructor
~LinkedStack();// The destructor
void Push(const T x); // Into the stack
bool Pop(); // Out of the stack
bool getTop(T &x) const; // Read top of stack elements
bool isEmpty()const; // Determines whether the stack is empty
int getSize()const; // Find the number of elements on the stack
void makeEmpty(); // Empty the contents of the stack
friend ostream& operator << <T>(ostream& out, LinkedStack<T>& s); // Overloaded output function
private:
LinkNode<T> *top; // Stack top pointer, or chain head pointer
};
template <class T>
LinkedStack<T>::LinkedStack() {
// Constructor, empty stack
top = new LinkNode<T>(); // Incoming header pointer: does not hold data
top->link = NULL;
}
template <class T>
LinkedStack<T>::~LinkedStack() {
// The destructor , Free up memory space
makeEmpty();
}
template <class T>
void LinkedStack<T>::Push(const T x) {
// Push: To push the element value x Insert to the top of the chain stack, the chain head
LinkNode<T> *newNode = new LinkNode<T>(); // Create contains x The new node
if(newNode == NULL) {
cerr << " Memory space allocation failed! " << endl;
exit(1);
}
newNode->data = x;
newNode->link = top->link; // Below the pointer to the head 1 Node: the stack in the first 1 A node that holds valid data
top->link = newNode; // Move the head pointer forward
}
template <class T>
bool LinkedStack<T>::Pop() {
// Out of stack: Removes the top of the stack
if(isEmpty())
return false; // Stack empty, not out of stack
LinkNode<T> *p = top->link; // Top of the stack element
top->link = p->link; // The stack top pointer falls back to the new stack top position
delete p;
p = NULL;
return true;
}
template <class T>
bool LinkedStack<T>::getTop(T &x) const {
// Read top of stack elements
if(isEmpty())
return false;
x = top->link->data; // The stack is not empty. Returns the value of the top element. Here, top Is the header pointer, so the top element of the stack is: top->link
return true;
}
template <class T>
bool LinkedStack<T>::isEmpty()const {
// Determines whether the stack is empty
if(top->link == NULL) // The stack is empty
return true;
return false;
}
template <class T>
int LinkedStack<T>::getSize()const {
// Find the number of elements on the stack
int len = 0;
LinkNode<T> *current = top->link;
while(current != NULL) {
len++;
current = current->link;
}
return len;
}
template <class T>
void LinkedStack<T>::makeEmpty() {
// Empty the contents of the stack
LinkNode<T> *current = top->link;
while(current != NULL) {
top->link = current->link; // Save the node under the chain stack ready to be deleted 1 Three nodes to prevent loss
delete current; // The release of
current = NULL; // First points to an empty
current = top->link; // And then point to the first node of the rest of the list
}
}
template <class T>
ostream& operator<<(ostream& out, LinkedStack<T>& s) {
// Overloaded output function
LinkNode<T> *current = s.top->link;
while(current != NULL) {
out << current->data << " ";
current = current->link;
}
return out;
}
#endif /* LinkedStack_h */
3.main.cpp
#include "LinkedStack.h"
using namespace std;
int main(int argc, const char * argv[]) {
int n, x, choice, len; // val The stored values, choose Store the user's choice
bool finished = false;
LinkedStack<int> L; // object
while(!finished) {
cout << "1: Build the stack: " << endl;
cout << "2: Into the stack " << endl;
cout << "3: The stack: " << endl;
cout << "4: Read top of stack elements: " << endl;
cout << "5: Is the stack empty: " << endl;
cout << "6: Number of elements in the stack: " << endl;
cout << "7: Empty the contents of the stack: " << endl;
cout << "8: Output the value of the element in the stack: " << endl;
cout << "9: exit " << endl;
cout << " Please enter your choice [1-9] : " << endl;
cin >> choice;
switch(choice) {
case 1:
cout << " Please enter the number of Numbers to be pushed :";
cin >> n;
cout << " Please enter the number to be pushed ( Space off ) : " << endl;
for(int i=0; i < n; i++) {
cin >> x;
L.Push(x);
}
break;
case 2:
cout << " Please enter the number to be pushed :";
cin >> x;
L.Push(x);
break;
case 3:
if(L.Pop())
cout << " The stack is successful !" << endl;
else
cout << " The stack is empty !" << endl;
break;
case 4:
if(L.getTop(x))
cout << " The value of the top element of the stack is: " << x << endl;
else
cout << " The stack is empty !" << endl;
break;
case 5:
if(L.isEmpty())
cout << " The stack is empty! " << endl;
else
cout << " The stack is not empty !" << endl;
break;
case 6:
len = L.getSize();
cout << " The number of elements in the stack is: " << len << endl;
break;
case 7:
L.makeEmpty(); // Empty stack
break;
case 8:
if(L.isEmpty())
cout << " The stack is empty! " << endl;
else
cout << L << endl;
break;
case 9:
finished = true;
break;
default:
cout << " Input error, please retype !" << endl;
} // switch
} // while
return 0;
}