#include "list.h"
#include <iostream>
using
namespace std;
// Qustion#5:
// What is the purpose of list :: list()?
In what situation would it be called?
// If we remove the only line of
the code in its implementation below from it,
// what would go wrong when we
use list objects?
list ::
list()
{ head = NULL;
}
// Qustion#6:
// What is the purpose of list :: ~list()? In what situation would it be called?
// If we remove all lines of the
code in its implementation below from it,
// what would go wrong when we
use list objects?
list ::
~list()
{ listNode * temp;
while (head)
{ temp=head->next;
delete head;
head=temp;
}
}
// Qustion#7:
// What
is the value of temp when the while loop in the code stops?
void
list::display()
{ listNode * temp=head;
while(temp)
{ cout<<temp->value<<endl;
temp=temp->next;
}
}
// Qustion#8:
// If Val equals to none of the
values stored in the value field of the nodes
// of the linked list, what
would be the value of temp when the while loop in the code stops?
// If Val equals to the value
stored in the value field of one of the nodes
// of the linked list, what
would be the value of temp when the while loop of code stop?
bool
list:: search(int Val)
{ listNode * temp=head;
while(temp && temp->value != Val)
temp=temp->next;
if (temp)
return true;
else
return false;
}
// Qustion#9:
// What
would go wrong if we replace the first line of the code simply as
// listNode * temp;
// ?
void
list :: insert(int Val)
{ listNode * temp = new listNode;
temp->value = Val;
temp->next = head;
head = temp;
}
// Qustion#10:
// We
know that list::insertInOrder should be used for insertion
// if we intend to maintain a
linked list
// with nodes storing values in
ascending order.
// Given
a sorted linked list,
// if Val is less than the value stored in
the value field of the first node
// of the linked list, what
nodes would Prev and Curr point to respectively
// immediately after the while loop in the code
terminates?
// if Val is greater than the value stored in
the value field of the last node
// of the linked list, what
nodes would Prev and Curr point to respectively
// immediately after the while loop in the code
terminates?
void
list::insertInOrder(int
Val)
{ listNode * Curr
= head;
listNode * Prev
= NULL;
listNode * temp = new listNode;
temp->value = Val;
//
Loop through list until end or item is found
while((Curr) && (Curr->value < Val))
{ Prev = Curr; Curr = Curr->next;
}
//Perform
the actual insert
temp->next = Curr;
if (Prev) //Inserting in the middle of the list
Prev->next = temp;
else //Inserting into
the front of the list
head = temp;
}
// Qustion#11:
// What
would happen if more than one nodes in the linked list store a value equal to Val?
// Would all these nodes be
removed by list::remove(int Val)?
// If not, show how you may
modify the code to achieve it.
void
list::remove(int Val)
{ listNode * Curr
= head;
listNode * Prev
= NULL;
while(Curr)
{
if (Curr->value == Val)
{ if (Prev == NULL) //Removing first node;
head = Curr->next;
else //Removing a normal node;
Prev->next = Curr->next;
delete Curr; //In either case
return;
}
else //move to the next item in the list
{ Prev = Curr;
Curr = Curr->next;
}
}
}