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 -...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
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...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Write a program that will read the information from a file into a list and then...
Write a program that will read the information from a file into a list and then display the list to the screen. Remove the fifth item in the list and display the list again. Ask the program user for an entry into the list and add it to the list. Display the list one last time. disneyin.txt file daisy   123 donald   345 goofy   654 mickey   593 minnie   489 daffy   432 pluto   765 huey   321 dewey   987 lewey   554 porky   333...
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