mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-14 02:30:40 +08:00
Add a simple class stack, and example
This commit is contained in:
35
data_structure/stk/stack.h
Normal file
35
data_structure/stk/stack.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/* This class specifies the basic operation on a stack as a linked list */
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
/* Definition of the node */
|
||||
template <class Type>
|
||||
struct node
|
||||
{
|
||||
Type data;
|
||||
node<Type> *next;
|
||||
};
|
||||
|
||||
/* Definition of the stack class */
|
||||
template <class Type>
|
||||
class stack
|
||||
{
|
||||
public:
|
||||
void display(); /* Show stack */
|
||||
stack(); /* Default constructor*/
|
||||
~stack(); /* Destructor */
|
||||
bool isEmptyStack(); /* Determine whether the stack is empty */
|
||||
void push (Type item); /* Add new item to the stack */
|
||||
Type top(); /* Return the top element of the stack */
|
||||
void pop(); /* Remove the top element of the stack */
|
||||
void clear();
|
||||
|
||||
stack<Type> operator=(stack<Type> & otherStack);
|
||||
// Overload "=" the assignment operator.
|
||||
private:
|
||||
node<Type> *stackTop; /* Pointer to the stack */
|
||||
int size;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user