Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) Implement the program, as outlined below in pseudocode, using singly linke

ID: 3877507 • Letter: #

Question

(C++) Implement the program, as outlined below in pseudocode, using singly linked list interface to store the data read from Words.txt (shown below). Demonstrate that the list of words does print in reverse using the 'prev' node (rather than the 'next' node). ---none of the previous answers to this question are correct on this site, so please don't copy/paste---

(Words.txt) is as follows, this code should work even if file is changed to more words:

when

what

there

been

one

could

very

an

who

them

Weekend

we

now

more

out

do

are

up

Explanation / Answer

Just to print in reverse would read data from file soon

#include<iostream>
using namespace std;
class Node{
   public:
   int data;
   Node *pre;
   Node(int x){
       data=x;
       pre=NULL;
   }
};
class Stack{
   Node *top;
   public:
   Stack(){
   top=NULL;
   }
void push(int x);
void pop();
int getTop();
void display();
};
void Stack::push(int x)
{
    Node *nn;
    nn=new Node(x);
    if(top!=NULL)
    nn->pre=top;
    top=nn;
}
void Stack::pop()
{
   Node *temp;
   if(top==NULL)
   {
       cout<<"Stack is empty"<<endl;
       return;
   }
   temp=top;
   top=top->pre;
   cout<<"Popped value is "<<temp->data<<endl;
   delete temp;
}
int Stack::getTop(){
   if(top==NULL)
   return 0;
   else
   return top->data;
}
void Stack::display(){
   Node *temp;
   if(top==NULL)
   {
       cout<<"Stack is empty"<<endl;
       return;
   }
   for(temp=top;temp!=NULL;temp=temp->pre)
   {
       cout<<temp->data<<" ";
       cout<<endl;
   }
}
int main()
{
   Stack s;
   int ch,num;
   while(ch!=5)
   {
       cout<<"1. Push ";
       cout<<"2. Pop ";
       cout<<"3. Get top most element ";
       cout<<"4. Display ";
       cout<<"5. Exit"<<endl;
       cout<<"Enter choice"<<endl;
       cin>>ch;
       switch(ch)
       {
           case 1: cout<<"Enter data ";
                    cin>>num;
                   s.push(num);
                   break;
           case 2:s.pop();
                    break;
           case 3:num=s.getTop();
                if(num==0)
                cout<<"Stack is empty"<<endl;
                else
                cout<<"Top most element is "<<num<<endl;
                   break;
           case 4:s.display();
                    break;
            case 5:cout<<"Exiting";
                    break;
           default:
                cout<<"Wrong choice"<<endl;
       }
   }
   return 0;
}