Chapter 18 Lab –
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.
Your program should include the main program to test and demonstrate one function for each Stack operations including:
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
#include<iostream>
using namespace std;
//creating list node
struct node{
int data;
node* next;
};
//InitStack – Initialize LIFO Stack
void InitStack(node *& start, node *&end)
{
start=NULL;
end=NULL;
}
//PushStack – Add an item to top of Stack
void PushStack(node *& start, node *&end, int data)
{
node* temp= new node;
temp->data=data;
temp->next=NULL;
//pushing element in empty stack
if (start==NULL)
{
start=temp;
end=temp;
}
//pushing element in existing stack
else if (start!=NULL)
{
temp->next=start;
start=temp;
}
}
//PrintStack – Print out the Stack items
void PrintStack(node *& start)
{
node* temp=new node;
temp=start;
if (temp==NULL)
{
cout<<"Stack is empty"<<endl;
}
else
{
cout<<"Stack items are:
"<<endl;
while(temp!=NULL)
{
cout<<temp->data<<" --> "<<endl;
temp=temp->next; }
}
}
//PopStack – Remove and return top item from Stack
void PopStack(node *&start, node *& end)
{
if (start==NULL)
{
cout<<"Stack is
empty"<<endl;
}
else if (start==end)
{ cout<<"Element popped from the stack:
"<<start->data<<endl;
delete start;
start=NULL;
end=NULL;
}
else
{
node* temp=new node;
temp=start;
start=start->next;
cout<<"Element popped from
the stack: "<<temp->data<<endl;
delete temp;
}
}
//TopStack – Returns the first item on Stack without deleting
it
void TopStack(node *& start)
{
node* temp=new node;
temp=start;
if (temp==NULL)
{
cout<<"Stack is empty"<<endl;
}
else
{
cout<<"Element in the top of the stack:
"<<temp->data<<endl;
}
}
//EmptyStack -Checks for empty Stack condition
(TRUE/FALSE)"
bool EmptyStack(node *&start)
{
if(start==NULL)
return true;
else
return false;
}
//ClearStack – Delete all items from Stack
void ClearStack(node *& start)
{
node* temp=new node;
if (start==NULL)
{
cout<<"Stack is
empty"<<endl;
}
else
{
while(start!=NULL)
{
temp=start;
start=start->next;
delete temp;
}
cout<<"Stack is deleted"<<endl;
}
}
int main()
{
node* start;
node* end;
int choice;
while(1){
cout<<"1. InitStack – Initialize LIFO
Stack"<<endl;
cout<<"2. PushStack – Add an item to top of
Stack at"<<endl;
cout<<"3. PrintStack – Print out the Stack
items"<<endl;
cout<<"4. PopStack – Remove and return top item
from Stack"<<endl;
cout<<"5. TopStack – Returns the first item on
Stack without deleting it"<<endl;
cout<<"6. EmptyStack – Checks for empty Stack
condition (TRUE/FALSE)"<<endl;
cout<<"7. ClearStack – Delete all items from
Stack"<<endl;
cout<<"8. Exit."<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
InitStack(start,end);
break;
case 2:
int element;
cout<<"Enter an element:
";
cin>>element;
PushStack(start,end,element);
break;
case 3:
PrintStack(start);
break;
case 4:
PopStack(start,end);
break;
case 5:
TopStack(start);
break;
case 6:
if(EmptyStack(start))
cout<<endl<<"Yes. Stack is
empty";
else
cout<<endl<<"No. Stack is not
empty";
break;
case 7:
ClearStack(start);
break;
case 8:
exit(0);
}
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.