Created
February 16, 2014 09:36
-
-
Save bitwiser/9031760 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include<cstdio> | |
#include<iostream> | |
using namespace std; | |
class LinkedList{ | |
int *arr; | |
int length,siz; | |
public: | |
LinkedList(int); | |
void push(int); | |
int Size(); | |
int pop(); | |
bool isEmpty(); | |
bool isFull(); | |
int Top(); | |
void display(); | |
}; | |
LinkedList::LinkedList(int n){ | |
arr = new int[n]; | |
if(arr==NULL){ | |
cout<<"Not enough memory"; | |
return; | |
} | |
siz = n; | |
length = -1; | |
} | |
void LinkedList::push(int n){ | |
if(arr==NULL){ | |
cout<<"Stack not created.\n"; | |
return; | |
} | |
if(this->isFull()){ | |
cout<<"Stack Full"; | |
return; | |
} | |
if(n<0){ | |
cout<<"Only Positive integers are allowed.\n"; | |
return; | |
} | |
length = length+1; | |
arr[length] = n; | |
} | |
int LinkedList::pop(){ | |
if(this->isEmpty()){ | |
cout<<"Stack empty. Nothing to pop."; | |
return -1; | |
} | |
length = length-1; | |
return arr[length+1]; | |
} | |
int LinkedList::Top(){ | |
if(this->isEmpty()){ | |
cout<<"Stack empty.\n"; | |
return -1; | |
} | |
return arr[length]; | |
} | |
int LinkedList::Size(){ | |
return length+1; | |
} | |
bool LinkedList::isEmpty(){ | |
if(length<0){ | |
return true; | |
} | |
return false; | |
} | |
bool LinkedList::isFull(){ | |
if(length==siz){ | |
return true; | |
} | |
return false; | |
} | |
void LinkedList::display(){ | |
if(this->isEmpty()){ | |
cout<<"Stack Empty. Nothing to display.\n"; | |
return; | |
} | |
int i; | |
for(i=length;i>=0;i--){ | |
cout<<this->pop()<<" "; | |
} | |
cout<<"\n"; | |
} | |
LinkedList *List = NULL; | |
int main(){ | |
string intro = "Stack Implementation.\nChoose your option\n"; | |
string menu = "\n1.Create Stack\n2.Push into stack\n3.Pop From stack\n4.Show count\n5.Display and Exit.\n6.Show top element.\nEnter your choice: "; | |
int choice; | |
cout<<intro; | |
while(1){ | |
cout<<menu; | |
cin>>choice; | |
switch(choice){ | |
case 1: | |
if(List!=NULL){ | |
cout<<"\nStack already created\n"; | |
break; | |
} | |
int t; | |
cout<<"\nEnter the size of stack: "; | |
cin>>t; | |
List = new LinkedList(t); | |
break; | |
case 2: | |
if(List==NULL){ | |
cout<<"\nStack not created yet.\n"; | |
break; | |
}else if(List->isFull()){ | |
cout<<"\nNo more integers can be pushed\n"; | |
break; | |
} | |
int n; | |
cout<<"\nEnter value to push: "; | |
cin>>n; | |
List->push(n); | |
cout<<"Pushed "<<n<<" into the stack.\n"; | |
break; | |
case 3: | |
if(List==NULL){ | |
cout<< "\nStack Not created\n"; | |
break; | |
}else if(List->isEmpty()){ | |
cout<<"Stack Empty.\n"; | |
break; | |
} | |
cout<<"\nPopped element: "<<List->pop()<<"\n"; | |
break; | |
case 4: | |
if(List==NULL){ | |
cout<<"\nStack Not created\n"; | |
break; | |
} | |
cout<<"\nCurrent stack count: "<<List->Size()<<"\n"; | |
break; | |
case 5: | |
cout<<"\nStack elements:\n"; | |
List->display(); | |
return 0; | |
case 6: | |
if(List==NULL){ | |
cout<<"\nStack Not created\n"; | |
break; | |
} | |
cout<<"\nStack top: "<<List->Top()<<"\n"; | |
break; | |
default: break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment