Write a program for Stack using Doubly Linked-List, and implement the following
ID: 3769372 • Letter: W
Question
Write a program for Stack using Doubly Linked-List, and implement the following functionalities:
1. Push 2. Pop 3. IsEmpty 4. MakeEmpty 5. Display forward (from the most recent to the first president) 6. Display Backward (from the first president to the most recent one)
Write a main program (C++) which reads presidents.txt containing all presidents of the US.
1. Your program should push the president names into the stack as you read from the file.
2. Display forward
3. Insert your name as the last president into the list. (your become the last or the most recent president)
4. Display backward
5. Then remove your name from the list.
6. Display forward
7. Empty all the list
8. Verify that your list becomes empty
Explanation / Answer
<code>
#include<bits/stdc++.h>
using namespace std;
void displayForward(list<string>name_list){
list<string>::reverse_iterator iit;
cout<<"Display forward:";
for(iit = name_list.rbegin(); iit != name_list.rend();iit++){
cout<< *iit<<" ";
}
cout<<endl;
}
void displayBackward(list<string>name_list){
list<string>::iterator it;
cout<<"Display backward:";
for(it = name_list.begin(); it != name_list.end();it++){
cout<< *it<<" ";
}
cout<<endl;
}
int main()
{
string name;
ifstream infile;
list<string>name_list;
infile.open ("presidents.txt");
cout<<"Pushing the president names in list..."<<endl;
while(infile >> name){
name_list.push_back(name);
}
displayForward(name_list);
cout<<"Adding my name as last president"<<endl;
name_list.push_back("shivam"); // adding myname as last president
displayBackward(name_list);
cout<<"Removing my name from president list."<<endl;
name_list.remove("shivam");
displayForward(name_list);
cout<<"Deleting all element from list"<<endl;
while (!name_list.empty()){
name_list.pop_front();
}
if (name_list.empty()){
cout<<"List is now empty"<<endl;
}
infile.close();
return 0;
}
</code>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.