Print out the contents of the stack and queue by calling the Print() functions d
ID: 3552870 • Letter: P
Question
Print out the contents of the stack and queue by calling the Print() functions dened
for the stack and queue, which call the pop function and Dequeue function.
Students open the door but you must walk through it yourself."
Example for reading from le:
ifstream inFile;
string word;
inFile.open("inData.txt");
inFile >> word;
while (inFile) {
// print and insert words
inFile >> word;
}
I need to change my print fuction to >>>>>> recrusive print fuction
Here is my code
main.cpp
#include <iostream>
#include <fstream>
#include<string>
#include "Stack_Queue.h"
using namespace std;
int main()
{
stack * st = new stack();
queue * qe = new queue();
ifstream inFile;
string word;
inFile.open("inData.txt");
inFile >> word;
while (inFile)
{
st->push(word);
qe->enqueue(word);
inFile >> word;
}
cout<<"Stack Contents : " <<endl;
st->Print();
cout << endl;
cout<<"Queue Contents :"<<endl;
qe->Print();
cout << endl;
return 0;
}
header.h
#include <iostream>
#include <fstream>
using namespace std;
struct node
{
string info;
node* next;
};
class stack
{
public:
stack();
void push(string s);
void pop();
void Print();
private:
node* head;
node* tail;
};
class queue
{
public:
queue();
void enqueue(string s);
void dequeue();
void Print();
private:
node* head;
node* tail;
};
Stack_Queue.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "Stack_Queue.h"
using namespace std;
stack::stack()
{
head=NULL;
}
void stack::push(string s)
{
if(head == NULL)
{
head = new node;
head->info = s;
head->next = NULL;
}
else
{
node* temp = new node;
temp->info = s;
temp->next = head;
head = temp;
}
}
void stack::pop()
{
node* temp = head;
if(head!=NULL)
{
head = head->next;
delete temp;
}
}
void stack::Print()
{
while(head!= NULL)
{
cout<<head->info<<endl;
pop();
}
}
queue::queue()
{
head = NULL;
tail = NULL;
}
void queue::enqueue(string s)
{
if(head == NULL)
{
head = new node;
head->info = s;
head->next = NULL;
tail = head;
}
else
{
tail->next = new node;
tail = tail->next;
tail->info = s;
tail->next = NULL;
}
}
void queue::dequeue()
{
node* temp = head;
if(head!=NULL)
{
head = head->next;
delete temp;
}
}
void queue::Print()
{
while(head!= NULL)
{
cout<<head->info<<endl;
dequeue();
}
}
Explanation / Answer
Done changed function to recursive
//Stack_Queue.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "Stack_Queue.h"
using namespace std;
stack::stack()
{
head=NULL;
}
void stack::push(string s)
{
if(head == NULL)
{
head = new node;
head->info = s;
head->next = NULL;
}
else
{
node* temp = new node;
temp->info = s;
temp->next = head;
head = temp;
}
}
void stack::pop()
{
node* temp = head;
if(head!=NULL)
{
head = head->next;
delete temp;
}
}
void stack::Print()
{
if(head == NULL)
return ;
cout<<head->info<<endl;
pop();
Print();
}
queue::queue()
{
head = NULL;
tail = NULL;
}
void queue::enqueue(string s)
{
if(head == NULL)
{
head = new node;
head->info = s;
head->next = NULL;
tail = head;
}
else
{
tail->next = new node;
tail = tail->next;
tail->info = s;
tail->next = NULL;
}
}
void queue::dequeue()
{
node* temp = head;
if(head!=NULL)
{
head = head->next;
delete temp;
}
}
void queue::Print()
{
if(head == NULL)
return;
cout<<head->info<<endl;
dequeue();
Print();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.