Question

Chapter 18 Lab – Design and code your own LIFO Stack using single liked list to...

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

Homework Answers

Answer #1

#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;  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
Code in JAVA The requirements are as follows: The input will be in a text file...
Code in JAVA The requirements are as follows: The input will be in a text file whose name is given by arg[0] of main(). It will contain a fully-parenthesized infix expression containing only: "(", ")", "+", "-" and integers. Need help on the main and fixing the Queue. //Input: ( ( 1 + 2 ) - ( ( 3 - 4 ) + ( 7 - 2 ) ) ) ( ( 1 + 2 ) - ( 3 -...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT