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.
1
Expert's answer
2015-02-12T09:26:02-0500
#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("cls"); 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("cls"); int data; cout<<"Enter number: "; cin>>data; lst.AddItem(data); cout<<"Number was added.."<<endl; system("pause"); menu_state = 4; break; case 2: system("cls"); lst.DisplayList(); system("pause"); menu_state = 4; break; default: break; } } while (menu_state != 3); system("pause"); 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; } }
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!
Comments
Leave a comment