Skip to content

Instantly share code, notes, and snippets.

@miladabc
Created July 7, 2018 16:36
Show Gist options
  • Save miladabc/fe645eddd554a2f29ec722066f53aeca to your computer and use it in GitHub Desktop.
Save miladabc/fe645eddd554a2f29ec722066f53aeca to your computer and use it in GitHub Desktop.
Stack implementation with Linked List
//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