#include "list.h"
#include <iostream>
using
namespace std;
// Qustion#5:
// What is the purpose of list :: list()?
On what occasions would it be called?
// If we remove the only line of
the code in its body from it,
// what would be the problem to
the usage of list objects?
list ::
list()
{ head = NULL;
}
// Qustion#6:
// What is the purpose of list :: ~list()? On what occasions would it be called?
// If we remove all lines of the
code in its body from it,
// what would be the problem to
the usage of list objects?
list ::
~list()
{ listNodePointer 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()
{ listNodePointer 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)
{ listNodePointer temp=head;
while(temp && temp->value != Val)
temp=temp->next;
if (temp)
return true;
else
return false;
}
// Qustion#9:
// What
is wrong if we replace the first line of the code simply as
// listNodePointer temp;
// ?
void
list :: insert(int Val)
{ listNodePointer 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.
// 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)
{ listNodePointer Curr = head;
listNodePointer Prev = NULL;
listNodePointer 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 or not by list::remove(int Val)?
// If not, show how you may
modify the code to achieve it.
void
list::remove(int Val)
{ listNodePointer Curr = head;
listNodePointer 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;
}
}
}