Question

In: Computer Science

C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static...

C++

  1. Static Stack Template In this chapter you studied IntStack, a class that implements a static stack of integers. Write a template that will create a static stack of any data type. Demonstrate the class with a driver program.
  2. Dynamic Stack Template In this chapter you studied DynIntStack, a class that implements a dynamic stack of integers. Write a template that will create a dynamic stack of any data type. Demonstrate the class with a driver program.
  3. Static Queue Template In this chapter you studied IntQueue, a class that implements a static queue of integers. Write a template that will create a static queue of any data type. Demonstrate the class with a driver program.
  4. Dynamic Queue Template In this chapter you studied DynIntQueue, a class that implements a dynamic queue of integers. Write a template that will create a dynamic queue of any data type. Demonstrate the class with a driver program.
  5. Error Testing The DynIntStack and DynIntQueue classes shown in this chapter are abstract data types using a dynamic stack and dynamic queue, respectively. The classes do not currently test for memory allocaton errors. Modify the classes so they determine if new nodes cannot be created, and handle the error condition in an appropriate way. (You will need to catch the predefined exception bad_alloc.)

Note: If you have already done Programming Challenges 2 and 4, modify the templates you created.

6. Dynamic String Queue Design a class that stores strings on a dynamic queue. The strings should not be fixed in length. Demonstrate the class with a driver program.

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU

// StaticStack.h

#define MAX 20
using namespace std;
class StaticStack
{
   public:  
       int top=-1;
       int arr[MAX];
       int count=0;
      
   public:                                   // constructor to initialise the stack
   StaticStack()
   {
       top=-1;
       count=0;
       for(int i=0;i<MAX;i++)
           arr[i]=0;
   }
      
   public:
   void push(int ch)                       // method that pushes element into stack
   {
       if(top==MAX)
       {
           cout << "Overflow" << endl;
           return;
       }
       arr[++top]=ch;
       count++;
   }
  
   public:
   int pop()                               // method that pops the top element from stack
   {
       if(top==-1)
       {
           cout << "Underflow" << endl;
           return -1;
       }
       int value=arr[top];
       top--;
       return value;
   }
  
   public:
   int getTop()                           // method that returns top most element of stack
   {
       if(top==-1)
           return -1;
       return arr[top];
   }
  
   public:
   int size()                               // method that returns no.of elements in stack
   {
       return count;
   }
  
   public:
   bool isEmpty()                           // method that says whether the stack is empty or not
   {
       return top==-1;
   }
};

// DynamicStack.h

class DynamicStack
{
   class Node                               // nested class of a node of linked list
   {
       public:
           int data;
           Node *next;
      
       public:
       Node(int d)
       {
           data=d;
           next=NULL;
       }
   };
  
   public:
       Node *head=NULL;
       int count=0;
      
   public:                                   // constructor to initialise the stack
   DynamicStack()
   {
       head=NULL;
       count=0;
   }
      
   public:
   void push(int ch)                       // method that pushes element into stack
   {
       Node *node=new Node(ch);
       if(head==NULL)
       {
           head=node;
       }
       else
       {
           node->next=head;
           head=node;
       }
       count++;
   }
  
   public:
   int pop()                               // method that pops the top element from stack
   {
       if(head==NULL)
           return '\0';
       Node *node=head;
       head=head->next;
       count--;
       return node->data;
   }
  
   public:
   int top()                               // method that returns top most element of stack
   {
       if(head==NULL)
           return '\0';
       return head->data;
   }
  
   public:
   int size()                               // method that returns no.of elements in stack
   {
       return count;
   }
  
   public:
   bool isEmpty()                           // method that says whether the stack is empty or not
   {
       return head==NULL;
   }
};

// StaticQueue.h

#define MAX 20
using namespace std;
class StaticQueue
{
   public:  
       int front=-1,rear=-1;
       int arr[MAX];
       int count=0;
      
   public:                                       // constructor to initialise the queue
   StaticQueue()
   {
       front=-1;
       rear=-1;
       count=0;
       for(int i=0;i<MAX;i++)
           arr[i]=0;
   }
      
   public:
   void enqueue(int ch)                       // method that enqueues element into queue
   {
       if(rear>=MAX)
       {
           cout << "Overflow" << endl;
           return;
       }
       arr[++rear]=ch;
   }
  
   public:
   int dequeue()                               // method that dequeues the element from queue using FIFO rule
   {
       if(front>=MAX)
       {
           cout << "Underflow" << endl;
           return -1;
       }
       int value=arr[++front];
   }
  
   public:
   int top()                                   // method that returns top most element of queue
   {
       if(front==-1 || front>=MAX || rear>=MAX)
       {
           cout << "Underflow" << endl;
           return -1;
       }
       return arr[front];
   }
  
   public:
   int size()                                   // method that returns no.of elements in queue
   {
       return count;
   }
  
   public:
   bool isEmpty()                               // method that says whether the queue is empty or not
   {
       return front>=rear;
   }
};

// DynamicQueue.h

class DynamicQueue
{
   class Node                                   // nested class of a node of linked list
   {
       public:
           int data;
           Node *next;
      
       public:
       Node(int d)
       {
           data=d;
           next=NULL;
       }
   };
  
   public:
       Node *head=NULL,*tail=NULL;
       int count=0;
      
   public:                                       // constructor to initialise the queue
   DynamicQueue()
   {
       head=NULL;
       count=0;
   }
      
   public:
   void enqueue(int ch)                       // method that enqueues element into queue
   {
       Node *node=new Node(ch);
       if(head==NULL)
       {
           head=node;
           tail=node;
       }
       else
       {
           tail->next=node;
           tail=node;
       }
       count++;
   }
  
   public:
   int dequeue()                               // method that dequeues the element from queue using FIFO rule
   {
       if(head==NULL)
           return '\0';
       Node *node=head;
       head=head->next;
       if(head==NULL)
           tail=NULL;
       count--;
       return node->data;
   }
  
   public:
   int top()                                   // method that returns top most element of queue
   {
       if(head==NULL)
           return '\0';
       return head->data;
   }
  
   public:
   int size()                                   // method that returns no.of elements in queue
   {
       return count;
   }
  
   public:
   bool isEmpty()                               // method that says whether the queue is empty or not
   {
       return head==NULL;
   }
};

// StringQueue.h

using namespace std;
class StringQueue
{
   class Node                                           // nested class of a node of linked list
   {
       public:
           string data;
           Node *next;
      
       public:
       Node(string d)
       {
           data=d;
           next=NULL;
       }
   };
  
   public:
       Node *head=NULL,*tail=NULL;
       int count=0;
      
   public:                                               // constructor to initialise the queue
   StringQueue()
   {
       head=NULL;
       count=0;
   }
      
   public:
   void enqueue(string ch)                               // method that enqueues element into queue
   {
       Node *node=new Node(ch);
       if(head==NULL)
       {
           head=node;
           tail=node;
       }
       else
       {
           tail->next=node;
           tail=node;
       }
       count++;
   }
  
   public:
   string dequeue()                                   // method that dequeues the element from queue using FIFO rule
   {
       if(head==NULL)
           return "";
       Node *node=head;
       head=head->next;
       if(head==NULL)
           tail=NULL;
       count--;
       return node->data;
   }
  
   public:
   string top()                                       // method that returns top most element of queue
   {
       if(head==NULL)
           return "";
       return head->data;
   }
  
   public:
   int size()                                           // method that returns no.of elements in queue
   {
       return count;
   }
  
   public:
   bool isEmpty()                                       // method that says whether the queue is empty or not
   {
       return head==NULL;
   }
};

// driver method

#include <bits/stdc++.h>
#include"StaticStack.h"
#include"DynamicStack.h"
#include"StaticQueue.h"
#include"DynamicQueue.h"
#include"StringQueue.h"
using namespace std;

void printStaticStack(StaticStack stack)                       // prints elements of static int stack
{
   cout << "Static IntStack Elements: ";
   while(!stack.isEmpty())
       cout << stack.pop() << " ";
   cout << endl;
}
void printDynamicStack(DynamicStack stack)                       // prints elements of dynamic int stack
{
   cout << "Dynamic IntStack Elements: ";
   while(!stack.isEmpty())
       cout << stack.pop() << " ";
   cout << endl;
}

void printStaticQueue(StaticQueue queue)                       // prints elements of static int queue
{
   cout << "Static IntQueue Elements: ";
   while(!queue.isEmpty())
       cout << queue.dequeue() << " ";
   cout << endl;
}

void printDynamicQueue(DynamicQueue queue)                       // prints elements of dynamic int queue
{
   cout << "Dynamic IntQueue Elements: ";
   while(!queue.isEmpty())
       cout << queue.dequeue() << " ";
   cout << endl;
}

void printStringQueue(StringQueue queue)                       // prints elements of dynamic string queue
{
   cout << "String Queue Elements: ";
   while(!queue.isEmpty())
       cout << queue.dequeue() << " ";
   cout << endl;
}

int main()                                               // driver method
{
   int n=5;
   StaticStack staticStack;
   for(int i=1;i<=n;i++)
       staticStack.push(i);
   printStaticStack(staticStack);
  
   DynamicStack dynamicstack;
   for(int i=1;i<=n;i++)
       dynamicstack.push(i);
   printDynamicStack(dynamicstack);
  
   StaticQueue staticQueue;
   for(int i=1;i<=n;i++)
       staticQueue.enqueue(i);
   printStaticQueue(staticQueue);
  
   DynamicQueue dynamicQueue;
   for(int i=1;i<=n;i++)
       dynamicQueue.enqueue(i);
   printDynamicQueue(dynamicQueue);
  
   StringQueue strQueue;
   for(int i=1;i<=n;i++)
       strQueue.enqueue("string"+to_string(i));
   printStringQueue(strQueue);
  
    return 0;
}

Related Solutions

Write your own version of a class template that will create a static stack of any...
Write your own version of a class template that will create a static stack of any data type. Demonstrate the class with a driver program. please make a version to copy.
Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
"""stack.py implements stack with a list""" class Stack(object): def __init__(self): #creates an empty stack. O(1) self.top...
"""stack.py implements stack with a list""" class Stack(object): def __init__(self): #creates an empty stack. O(1) self.top = -1 #the index of the top element of the stack. -1: empty stack self.data = [] def push(self, item): # add item to the top of the stack. O(1) self.top += 1 self.data.append(item) def pop(self): # removes and returns the item at the top O(1) self.top -=1 return self.data.pop() def peek(self): # returns the item at the top O(1) return self.data[len(self.data)-1] def isEmpty(self):...
In C++ In this lab we will be creating a stack class and a queue class,...
In C++ In this lab we will be creating a stack class and a queue class, both with a hybrid method combining linked list and arrays in addition to the Stack methods(push, pop, peek, isEmpty, size, print) and Queue methods (enqueue, deque, peek, isEmpty, size, print). DO NOT USE ANY LIBRARY, implement each method from scratch. Both the Stack and Queue classes should be generic classes. Don't forget to comment your code.
You will write Stack class in c++ that will be integrated into a larger software product...
You will write Stack class in c++ that will be integrated into a larger software product provided by the instructor. This Stack class uses a dynamically allocated array to store data values (integers) that have been pushed onto the stack object. When the client code attempts to push another integer onto a full stack, your Push operation should invoke the Resize() function which attempts to double the capacity of the stack and then add the new data value to the...
If a class A implements interface I1 and class C and D are derived from class...
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct 1. A, C, and D 2. A and D
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race condition and is not appropriate for a concurrent environment. Using Pthreads mutex locks, fix the race condition. For reference, see Section 7.3.1 of SGG book.(Section 7.3.1 is about mutex and semaphores it does explain how to implement I'm just having a hard time finding the race condition within the code) /* * Stack containing race conditions */ #include #include #include typedef int value_t; //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT