diff --git a/algorithms/CPlusPlus/Stacks/stack_using_linked_list.C++ b/algorithms/CPlusPlus/Stacks/stack_using_linked_list.C++ new file mode 100644 index 00000000..6094ca8e --- /dev/null +++ b/algorithms/CPlusPlus/Stacks/stack_using_linked_list.C++ @@ -0,0 +1,97 @@ +#include +using namespace std; +class Node { +public: + int data; + Node *next; + Node(int val) // Parameterised Constructor + { + data = val; + next = NULL; + } +}; +class Stack +{ + Node *top; +public: +Stack() // Default Constructor + { + top = NULL; + } + void push(int); + void printStack(); + void pop(); +}; +void Stack::push(int data) +{ + // Create the new Node. + Node *newNode = new Node(data); + newNode->data = data; + newNode->next = top; + top = newNode; + +} + +void Stack::printStack() +{ + Node* ptr; + if(top==NULL) + cout<<"stack is empty"; + else { + ptr = top; + cout<<"Stack elements are: "; + while (ptr != NULL) { + cout<< ptr->data <<" "; + ptr = ptr->next; + } + } + cout<data <next; + } +} + +int main() +{ + Stack s; + int ch, val; + cout<<"1) Push in stack"<>ch; + switch(ch) { + case 1: { + cout<<"Enter value to be pushed:"<>val; + s.push(val); + break; + } + case 2: { + s.pop(); + break; + } + case 3: { + s.printStack(); + break; + } + + case 4: { + cout<<"Exit"<