Document Sample

```					                                          Linked List
Demerits of static Array and dynamic Array
   In an array the elements are stored in contiguous memory locations. It is impossible that one
element of the array is located at a memory location while the other element is located
somewhere far from it in the memory. Moreover, when we have declared the size of the
array, it is not possible to increase or decrease it during the execution of the program.
Whenever we need more elements to store in the array, we have to stop its execution, change
its size in the declaration, recompile the program and then execute it again.
   The solution of above mentioned problem is in Dynamic Memory Allocation. Suppose we
create an array of 100 elements with the use of new operator. In case we need to grow size of
array to 200 elements, we will release this array and allocate a new array of 200 elements.
During this process we have to copy all the elements in the old array to the new array so that
it does not lose any information. Now this new array is in ‘ready for use’ position. Thus the
procedure of creating a new array is not an easy task.

To avoid above mentioned problems we need some linked memory in which the various
cells of memory are not located continuously. In this process, each cell of the memory not only
contains the value of the element but also the information where the next element of the list is
residing in the memory. It is not necessary that the next element is at the next location in the
memory. It may be anywhere in the memory. Thus the first element must explicitly have the
information about the location of the second element. Similarly, the second element must know
where the third element is located and the third should know the position of the fourth element
and so on. Thus, each cell (space) of the list will provide the value of the element along with the
information about where the next element is in the memory. This information of the next element
is accomplished by holding the memory address of the next element. Such an element is called a

Node
To form a linked list, at first, we define a node. A node comprises two fields. i.e. the object field
that holds the actual list element and the next that holds the starting location of the next node.

A chain of these nodes forms a linked list. Consider the following list of elements stored in the
form of a linked list 2, 6, 8, 7, 1. Following is the figure which represents the list stored as a
In this linked list, the head points to 2, 2 points to 6, 6 points to 8, 8 points to 7 and 7 points to 1.
Moreover we have the current position at element 8.

We knew that an array always starts from index 0 that means the first element of the list is at
index 0. Similarly in the linked list we need to know the starting point of the list. For this
purpose, we have a pointer head that points to the first node of the list. If we don’t use head, it
will not be possible to know the starting position of the list. We also have a pointer current to
point to the current node of the list. We need this pointer to add or remove current node from the
list. The next field of the last node points to nothing. It is the end of the list. We place the
memory address NULL in the last node. NULL is an invalid address and is inaccessible.

The diagram above just represents the linked list. In the memory, different nodes may occur at
different locations but the next part of each node contains the address of the next node. The
following diagram depicts the process through which this linked list might be stored in the
memory. Note that the figure below just represents one possible configuration. The actual
memory allocation is done by operating system at runtime.
In the figure, we see that head is the name of the memory location 1062 and the name current is
used for the memory address 1053. The head holds the address 1054 which in turn contains the
element 2, the first one in the list. Similarly current holds the address 1063 where the element 8
is stored which is the current position in the list. In the diagram, two memory locations comprise
a node. So the location 1054 holds the element 2 while the next location 1055 holds the address
of the memory location (1051) where the next element of the list (i.e. 6) is stored. Similarly the
next part of the node that has value 6 holds the memory address of the location occupied by the
next element (i.e. 8) of the list. The other nodes are structured in a similar fashion. Thus, by
knowing the address of the next element we can traverse the whole list.

1.     Inserting a new node:

In a linked list a new node will always be inserted after the node where the current pointer
points. To insert a new node with some value we will call the following function.

To insert a new node we first have to create it using the following statement.

Node * newNode = new Node(9);
This statement will create the Node shown in the figure below.

9       /

The code of Node class is as follows.

class Node
{
public:
Node(int val) { object = val; nextNode = NULL; }
int get() { return object; }
void set(int object) { this->object = object; }
Node * getNext() { return nextNode; }
void setNext(Node * nextNode) { this->nextNode = nextNode; }
private:
int object;
Node * nextNode;
};

Now suppose that we want to add this node in the linked list described above. The linked list
contains five nodes with data elements as 2, 6, 8, 7, 1. The current pointer is pointing to the node
with element as 8. The insertion operation is performed in a step by step fashion.

   The first step is to point next pointer of the new node (with data element as 9) to the node
with data element as 7.
   The second step is to point the next pointer of the node with data element 8 to the node
the new node with data element 9.
   The third step is to change the current pointer to point to the new node.
Now, the updated linked list has nodes with data elements as 2, 6, 8, 9, 7 and 1. The list size has
become 6. These operations are shown in the following figure.
The data members in the node class are actually two parts of a linked list’s node. First variable is
object of type int, present there to store the data part of the node. The second variable is
nextNode, which is a pointer to an object of type Node. It has the address of the next element of
The first public function is the constructor of the class which receives an integer value and
generates the given at the top is get( ). We have written its code within the class Node. It returns
back the value of the variable object i.e. of the type of int.
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
struct Person
{
float age;
string name;
};
class Node
{
private:
Person value;
Node * next;
public:
void setValue(Person p)
{
value=p;
}
Person getValue()
{
return value;
}
void setNextNode(Node * temp)
{
next=temp;
}
Node * getNextNode()
{
return next;
}
};
{
private:
Node* firstNode;
Node* lastNode;
int no_of_nodes;
public:
{
firstNode=new Node();
firstNode->setValue(p);
firstNode->setNextNode(NULL);
no_of_nodes = 1;
lastNode=firstNode;
}
void insertNode(Person t,int index)
{
Node* newp=new Node();
newp->setValue(t);
Node* temp;
//************ CASE 1--- INSERTION AT START *************
if(index == 1)
{
newp->setNextNode(firstNode);
firstNode = newp;
}
//************ CASE 2--- INSERTION WITH IN LINK LIST
*************
else if(index > 1 && index <= no_of_nodes)
{
temp=firstNode;
for(int i=2; i< index;i++)
{
temp = temp->getNextNode();
}
newp -> setNextNode( temp -> getNextNode() );
temp -> setNextNode( newp );
}
//************ CASE 3--- INSERTION AT END *************
else if(index == no_of_nodes+1)
{
temp=firstNode;
while(temp->getNextNode() != NULL)
temp = temp->getNextNode();

newp -> setNextNode( NULL );
temp -> setNextNode( newp );

}
no_of_nodes++;
}
{
Node* temp = firstNode;
Person p;
for(int i=1; i <= no_of_nodes; i++)
{
p = temp->getValue();
cout <<endl<<"NAME OF PERSON = "<<p.name;
cout<<endl<<"AGE = "<<p.age;
temp = temp ->getNextNode();
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int opt,ind;
Person p;
cout<<"Enter the name of first person ";
getline(cin,p.name);
cout<<endl<<"Enter the age of first person ";
cin>>p.age;
do{
cout<<endl<<"2. Delete Some Person from Link List";
cout<<endl<<"3. Display Persons in the List";
cout<<endl<<"4. Exit Program";
cout<<endl<<"         ENTER THE OPTION (1-4) ";
cin>>opt;
switch(opt)
{
case 1:
system("cls");
cout<<"Enter the name of new person ";
cin.ignore();
getline(cin,p.name);
cout<<endl<<"Enter the age of new person ";
cin>>p.age;
cout<<endl<<"Enter the location for new node ";
cin>>ind;
l1.insertNode(p,ind);
break;
case 3:
system("cls");
getch();
break;
}
}while(opt!=4);
return 0;

}

```
DOCUMENT INFO
Categories:
Tags:
Stats:
 views: 3 posted: 8/1/2012 language: English pages: 8
How are you planning on using Docstoc?