#include <iostream> #include <string> #include \"superQueue\" using namespace st
ID: 3722734 • Letter: #
Question
#include <iostream> #include <string> #include "superQueue" using namespace std; /* Implement a "superQueue" using a doubly linked list. Create a class superQueue with methods that work as specified by the test program below. Your superQueue must be a doubly linked list with a head and tail pointer. */ int main() { //It's a stack!!! superQueue<string> pancakes; pancakes.push("mustard"); pancakes.push("captaincrunchy"); pancakes.push("pokemon"); pancakes.push("beef"); pancakes.push("pita"); pancakes.push("smurf"); pancakes.push("mightcore"); pancakes.push("egg"); pancakes.push("rotten"); pancakes.push("tomato"); pancakes.push("pizza"); pancakes.push("carrot"); pancakes.push("cheese"); pancakes.push("cinamon"); pancakes.push("chocochip"); pancakes.push("blueberry"); pancakes.push("strawberry"); pancakes.push("original"); cout << pancakes.pop() << endl; //original cout << pancakes.pop() << endl; //strawberry pancakes.push("banananana"); cout << pancakes.pop() << endl; //bananananana cout << pancakes.pop() << endl; //blueberry //Review overloading operators: cout << pancakes[5] << endl; //smurf cout << pancakes[2] << endl; //pokemon while (!pancakes.empty()) { cout << "eating: " << pancakes.pop() << endl; } //It's a queue!!!! superQueue<int> numberQueue; for (int i = 0; i<50; i++) numberQueue.enqueue(i); for (int i = 0; i<20; i++) cout << "Dequeing: " << numberQueue.dequeue() << endl; for (int i = 50; i<60; i++) numberQueue.enqueue(i); while (!numberQueue.empty()) cout << "Dequeing: " << numberQueue.dequeue() << endl; //It's even a priorityQueue!!!! superQueue<double> pq; pq.insert(57); pq.insert(32); pq.insert(105); pq.insert(17); cout << pq.extractMin() << endl; //17 cout << pq.extractMin() << endl; //32 cout << endl; pq.insert(68); pq.insert(5); pq.insert(43); cout << pq.extractMin() << endl; //5 cout << pq.extractMin() << endl; //43 cout << pq.extractMin() << endl; //57 cout << endl; pq.insert(120); pq.insert(500); pq.insert(3); pq.insert(73); pq.insert(29); //3 29 68 73 105 120 500 while (!pq.empty()) { cout << pq.extractMin() << endl; } cout << endl; return 0; }
Explanation / Answer
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
struct node *prev;
string info;
struct node *next;
}*start;
struct queue
{
struct queue *prev;
int value;
struct queue *next;
}*start1;
class super_queue
{
public:
void push_stack(string value);
void pop_stack();
void index_pop(int val);
void insert_queue(int value);
void delete_queue();
void pri_insert(int val);
void pri_delete();
void sort();
super_queue()
{
start=NULL;
start1=NULL;
}
};
int main()
{
super_queue s;
int num;
s.push_stack("mustard");
s.push_stack("captaincrucnchy");
s.push_stack("pokemon");
s.push_stack("beef");
s.push_stack("pita");
s.push_stack("smurf");
s.push_stack("mightcore");
s.push_stack("egg");
s.push_stack("rotten");
s.push_stack("tomato");
s.push_stack("pizza");
s.push_stack("carrot");
s.push_stack("cheese");
s.push_stack("cinamon");
s.push_stack("chocochip");
s.push_stack("blueberry");
s.push_stack("strawberry");
s.push_stack("original");
s.pop_stack();
s.pop_stack();
s.push_stack("bananananana");
s.pop_stack();
s.pop_stack();
s.index_pop(5);
s.index_pop(2);
struct node *eat;
eat=start;
while(eat!=NULL)
{
s.pop_stack();
eat=eat->next;
}
int i=0;
for(i=0;i<50;i++)
{
s.insert_queue(i);
}
for(i=0;i<20;i++)
{
s.delete_queue();
}
for(i=50;i<60;i++)
{
s.insert_queue(i);
}
struct queue *t;
t=start1;
while(t->next!=NULL)
{
s.delete_queue();
t=t->next;
}
start1=NULL;
s.insert_queue(57);
s.insert_queue(32);
s.insert_queue(105);
s.insert_queue(17);
s.sort();
s.delete_queue();
s.delete_queue();
s.insert_queue(68);
s.insert_queue(5);
s.insert_queue(43);
s.sort();
s.delete_queue();
s.delete_queue();
s.delete_queue();
s.insert_queue(120);
s.insert_queue(500);
s.insert_queue(3);
s.insert_queue(73);
s.insert_queue(29);
s.sort();
t=start1;
while(t->next!=NULL)
{
s.delete_queue();
t=t->next;
}
}
void super_queue::push_stack(string value)
{
struct node *s,*temp;
temp=new(struct node);
temp->info=value;
temp->next=NULL;
if(start==NULL)
{
temp->prev=NULL;
start=temp;
}
else
{
s=start;
while(s->next!=NULL)
s=s->next;
s->next=temp;
temp->prev=s;
}
cout<<"********";
}
void super_queue::pop_stack()
{
struct node *temp,*temp1;
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
cout<<temp->info;
cout<<endl;
temp1=temp->prev;
free(temp);
temp1->next=NULL;
}
void super_queue::index_pop(int val)
{
struct node *temp;
temp=start;
int i=0;
while(i!=val)
{
temp=temp->next;
i++;
}
cout<<temp->info;
cout<<endl;
}
void super_queue::insert_queue(int value)
{
struct queue *s,*temp;
temp=new(struct queue);
temp->value=value;
temp->next=NULL;
if(start1==NULL)
{
temp->prev=NULL;
start1=temp;
}
else
{
s=start1;
while(s->next!=NULL)
s=s->next;
s->next=temp;
temp->prev=s;
}
}
void super_queue::delete_queue()
{
struct queue *temp;
temp=start1;
cout<<temp->value;
cout<<endl;
temp=temp->next;
temp->prev=NULL;
start1=temp;
free(temp);
}
void super_queue::sort()
{
struct queue *temp,*temp2;
temp2=temp=start1;
int value,min,t;
temp=start1->next;
value=temp2->value;
min=value;
while(temp2->next!=NULL)
{
while(temp->next!=NULL)
{
if(value>temp->value)
min=temp->value;
temp=temp->next;
}
t=min;
temp2->value=min;
min=t;
temp2=temp2->next;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.