Stack implemented with SLL Design and code your own LIFO Stack using single like
ID: 3808286 • Letter: S
Question
Stack implemented with SLL
Design and code your own LIFO Stack using single liked list to hold a list of integers. Use the C++ “struct” structure to create your SLL nodes. Node position numbering should start with one. The program should include the following options. The program shoudl ask the user to choose an option from the main list below and proceed with the operations
InitStack – Initialize LIFO Stack
PushStack – Add an item to top of Stack at
PrintStack – Print out the Stack items
PopStack – Remove and return top item from Stack
TopStack – Returns the first item on Stack without deleting it
EmptyStack – Checks for empty Stack condition (TRUE/FALLS)
ClearStack – Delete all items from Stack
Explanation / Answer
#include <iostream>
using namespace std;
class Stack
{
struct Node
{
int data;
Node* link;
};
Node* head;
public:
Stack();
void InitStack();
int PushStack(int element);
void PrintStack();
int PopStack();
int TopStack();
bool EmptyStack();
void ClearStack();
};
Stack::Stack(){
head = NULL;
}
void Stack::InitStack(){
head = NULL;
}
int Stack::PushStack(int element){
Node* node = new Node;
node->data = element;
node->link = NULL;
if(head==NULL){
head = node;
}
else{
node->link = head;
head = node;
}
}
void Stack::PrintStack(){
Node* temp = head;
while(temp != NULL){
cout<<" "<<temp->data<<" ";
temp = temp->link;
}
cout<<endl;
}
int Stack::PopStack(){
int data = -1;
if(head==NULL){
cout <<"stack is empty"<<endl;
}
else{
Node* temp = head;
data = head->data;
head = head->link;
delete temp;
}
return data;
}
int Stack::TopStack(){
int data = -1;
if(head==NULL){
cout <<"stack is empty"<<endl;
}
else{
data = head->data;
}
return data;
}
bool Stack::EmptyStack(){
if(head==NULL)
return true;
return false;
}
void Stack::ClearStack(){
Node* temp = head;
while(head!=NULL){
temp = head;
head = head->link;
delete temp;
}
}
int printMenu();
int main(){
Stack * stack = new Stack();
bool quit = false;
while(!quit){
int choice = printMenu();
switch(choice){
case 1:
cout<<"Enter integer :";
int num;
cin >> num;
stack->PushStack(num);
cout<<"Pushed ";
break;
case 2:
cout<<" "<<stack->PopStack()<<" is poped from stack ";
break;
case 3:
cout<<" "<<stack->TopStack()<<" is on top of stack ";
break;
case 4:
cout<<"Stack is : ";
stack->PrintStack();
break;
case 5:
stack->ClearStack();
cout<<"Stack is cleared ";
break;
case 6:
if(stack->EmptyStack()){
cout<<"Stack is Empty ";
}
else{
cout<<"Stack is Not Empty ";
}
break;
case 7:
quit = true;
break;
};
}
return 0;
}
int printMenu(){
int choice;
while(true){
cout << " [1] Push in Stack "<< endl;
cout << " [2] Pop from Stack "<< endl;
cout << " [3] Top of Stack "<< endl;
cout << " [4] Print Stack "<< endl;
cout << " [5] Clear Stack "<< endl;
cout << " [6] Empty Stack? "<< endl;
cout << " [7] Quit"<< endl;
cout << "Choose an option :";
cin >> choice;
if(choice>0 && choice <8){
break;
}
else{
cout<<"Invailid Input ";
continue;
}
}
return choice;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.