67 178
Assignments Done
99,3%
Successfully Done
In November 2018

Answer to Question #50604 in C++ for Umer

Question #50604
Revise the additem() member function from the LINKLIST program so that it adds the
item at the end of the list, rather than the beginning. This will cause the first item
inserted to be the first item displayed, so the output of the program will be
25
36
49
64
To add the item, you’ll need to follow the chain of pointers to the end of the list, then
change the last link to point to the new link.
Expert's answer
#include <stdlib.h>
#include <iostream>  
#include <stdio.h>
#include <cstdlib> 
struct Data                  
{
    int data;
Data *Next,*Prev; 
};
class List                                   
 {
 public:
     Data *Head,*Tail;     
 public:
     List():Head(NULL),Tail(NULL){}; 
     ~List();   //Destructor                              
     void DisplayList(void);  
//adds the item at the end of the list
     void AddItem(int _data);     
 };
using namespace std;
int main(){
List lst;
   
int menu_state = 0;
do
{
system&#40;"cls"&#41;;
cout<<"1.Add element to the list"<<endl;
cout<<"2.Display List"<<endl;
cout<<"3.Exit"<<endl;
cin>>menu_state;
switch (menu_state)
{
case 1:
system&#40;"cls"&#41;;
int data;
cout<<"Enter number: ";
cin>>data;
lst.AddItem(data);
cout<<"Number was added.."<<endl;
system&#40;"pause"&#41;;
menu_state = 4;
break;
case 2:
system&#40;"cls"&#41;;
lst.DisplayList();
system&#40;"pause"&#41;;
menu_state = 4;
break;
default:
break;
}
} while (menu_state != 3);
system&#40;"pause"&#41;;
return 0;
}
 void List::AddItem(int _data)
 {
   Data *temp=new Data;    
   temp->Next=NULL;       
  
   temp->data = _data;
   if (Head!=NULL)         
   {
       temp->Prev=Tail; 
       Tail->Next=temp; 
       Tail=temp;       
   }
   else 
   {
       temp->Prev=NULL; 
       Head=Tail=temp; 
   }
 }
 void List::DisplayList()
 {
Data *temp = Head;
while (temp)            
     {
cout<<temp->data<<endl;
         temp = temp->Next;               
     }
 }
 List::~List()           
 {    
Data *temp = Head;
     while (Head)            
     {
         Tail=Head->Next;  
         delete Head;       
         Head=Tail;       
     }
 }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions