Answer to Question #4525 in C++ for ime

Question #4525
Write a C++ program using dynamic variables and pointers to construct a doubly linked list consisting of the following information in each node: student id (integer), student name (character string), and semester (integer). The operations to be supported are:

(a) Create a doubly linked list by adding each node at the front.

(b) Deleting a node based on student id. If the specified node is not present in the list an error message should be displayed. Both the options must be demonstrated.

(c) Searching a node based on student id and updates the information content. If the specified node is not present in the list an error message should be displayed. Both the options must be demonstrated.

(d) Display the contents of the list either from the first node to the last or from the last to the first node.
1
Expert's answer
2011-10-14T08:12:24-0400
// LinkedList.cpp : Defines the entry point for the console application.
//

#include "IntList.h"
#include <string>
#include <sstream>


using namespace std;

IntList::IntList(const IntList& src) & //add new list
{
ItemsCount = 0;
first=last=NULL;
AddLast(src);
}

IntList::~IntList() // destructor. deleted added list
{
ListItem*& current = NULL;
ListItem* next = first;
while(next)
{
& current = next;
& next=next->next;
& delete current;
}

}

void IntList::AddLast(const IntList& src) &
{
for(ListItem *cur=src.first; cur; cur = cur->next )
& AddLast(cur->item);
}

void IntList::AddFirst(int item)
{
ListItem *newItem = new ListItem(item, first);
first = newItem;
if(!first)
{
& last = newItem;
}
ItemsCount++;
}

void IntList::AddLast(int item)
{
ListItem *newItem = new ListItem(item);
if(!last)
{
& first = newItem;
}
else
{
& last ->next = newItem;
}
last = newItem;
ItemsCount++;
}

int IntList::RemoveFirst()
{
int res = first ->item;
first = first ->next;
ItemsCount--;
return res;
}

bool IntList::Remove(int value)
{
ListItem *prev=0;
ListItem *current = first;

while (current)
{
& if(current ->item == value)
& {
& if(prev)
& {
& prev->next=current->next;
& }
& if(current==last)
& {
& last=prev;
& }
& delete current;
& ItemsCount--;
& return true;
& }
& else
& {
& prev = current;
& current = current ->next;
& }
}
return false;
}

void IntList::Insert(int value)
{
ListItem *prev=NULL;
ListItem *succ=first;

while (succ !=NULL && succ->item<value)
{
& prev = succ;
& succ = succ ->next;
}

ListItem *newItem = new ListItem (value, succ);&
if(succ == NULL)
{
& last = newItem;
}

if(prev == NULL)
{
& first = newItem;
}
else
{
& prev->next=newItem;
}
ItemsCount++;
}

string IntList::GetAllItemsInfo()
{
stringstream stream;
ListItem *current = first;
while(current)
{
& stream<<current->item<<" ";
& current = current->next;
}
stream<<endl;
return stream.str();
}


#include<iostream>
#include"IntList.h"

using namespace std;

int main()
{
IntList list;
list.AddFirst(2);
list.AddLast(5);
list.AddFirst(7);
list.Insert(3);

cout<<list.GetAllItemsInfo();

IntList list1(list);
list1.AddLast(4);
list1.Remove(1);
list1.RemoveFirst();
list1.AddFirst(7);
&
cout<<list1.GetAllItemsInfo();
system("pause");
return 0;
}

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 the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS