In: Computer Science
C++
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.
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;
}