Created
July 7, 2018 16:36
-
-
Save miladabc/fe645eddd554a2f29ec722066f53aeca to your computer and use it in GitHub Desktop.
Stack implementation with Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Milad Abbasi | |
//06-07-2018 | |
//Stack implementation with Linked List | |
#include <iostream> | |
using namespace std; | |
class Node | |
{ | |
public: | |
int data; | |
Node *next; | |
}; | |
class Stack | |
{ | |
Node *top; | |
public: | |
Stack() | |
{ | |
top = NULL; | |
} | |
void push(int value) | |
{ | |
Node *tmp = new Node; | |
tmp->data = value; | |
if(isEmpty()) | |
{ | |
tmp->next = NULL; | |
top = tmp; | |
} | |
else | |
{ | |
tmp->next = top; | |
top = tmp; | |
} | |
} | |
void pop(void) | |
{ | |
if(!isEmpty()) | |
{ | |
Node *tmp = new Node; | |
tmp = top; | |
top = top->next; | |
delete tmp; | |
} | |
else | |
cout<<"Stack is empty!"; | |
} | |
int peek(void) | |
{ | |
return top->data; | |
} | |
bool isEmpty(void) | |
{ | |
return top == NULL; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment