-MUST use linked lists in functions, no libraries -comments above code is encour
ID: 3881628 • Letter: #
Question
-MUST use linked lists in functions, no libraries -comments above code is encouraged so I can follow Write a C+program to implement a Stack and a Queue. The program will be menu driven. Main Menu 1) Stack 2) Queue 3) Quit Enter option: Stack option. 1 Enter filename: data_file Read each word from the file and push it onto the stack until EOF. At EOF the program will pop each word from the stack (and free space) and write each word to a new file "report_ out". Do-in include numbers or punctuation marks in output. Data_file The quick brown fox. Report_out fox brown not quick TheExplanation / Answer
here is your program : -------------------????????????????????
#include<iostream>
#include<fstream>
#include<list>
using namespace std;
//this for stack implementation
void stack(){
ifstream fi;
ofstream of;
string data;
string file;
list<string> ll;
cout<<" Enter filename : ";
cin>>file;
fi.open(file.c_str());
if(fi.is_open()){
fi>>data;
while(!fi.eof()){
cout<<data;
ll.push_front(data);
fi>>data;
}
fi.close();
of.open("report_out.txt");
if(of.is_open()){
for(int i = 0;i<=ll.size();i++){
of<<ll.front()<<" ";
ll.pop_front();
}
of<<ll.front();
ll.clear();
of.close();
cout<<" Data is written to report_out.txt go and check file";
}else{
cout<<" Output File creating error";
}
}else{
cout<<" File opening error please check the filename;";
}
}
//this is for queue implementation
void queue(){
ifstream fi;
ofstream of;
string data;
string file;
list<string> ll;
system("cls");
cout<<" Enter filename : ";
cin>>file;
fi.open(file.c_str());
if(fi.is_open()){
while(!fi.eof()){
fi>>data;
ll.push_back(data);
}
fi.close();
of.open("report_out.txt");
if(of.is_open()){
for(int i = 0;i<=ll.size();i++){
of<<ll.front()<<" ";
ll.pop_front();
}
of<<ll.front();
ll.clear();
of.close();
cout<<" Data is written to report_out.txt go and check file";
}else{
cout<<" Output File creating error";
}
}else{
cout<<" File opening error please check the filename;";
}
}
void Menu(){
system("cls");
cout<<" 1. Stack";
cout<<" 2. Queue";
cout<<" 3. Quit";
cout<<" Choose Option : ";
}
int main(){
char choice = '0';
while(choice != '3'){
Menu();
cin>>choice;
switch(choice){
case '1':stack();break;
case '2':queue();break;
case '3':break;
default:
cout<<" Wrong Choice ";
}
cin.get();
cin.ignore();
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.