Professional Documents
Culture Documents
Definition of List:
Implementation:
NODE:
A node consists of two fields, an information field called DATA field which contains the
actual data to be stored in the list and a LINK field which contains the address of the next
node in the list
Initialization:
The list is initialized with head as NULL
Head = NULL
Head is a pointer of type node which always stores the address of first node of the
list.
1. Creation of List : This operation creates ‘n’ nodes in the list where n is the
number of nodes to be created in the list and the data is provided during run time
by the user.
2. Insertion: This function inserts a new node either in the first position, last position
or in the intermediate position. The position at which the data should be inserted
is provided as input during run time by the user.
3. Deletion: Deletion is performed by providing an element as input. The given
element is searched in the list and if the element is found then it is deleted. The
element is searched for multi occurrences and it is deleted.
Condition:
1. The condition to be checked is List underflow. (ie) List is empty
2. The element may not be available in the list
cin>>element
P = new node
P->data = element
P->link = NULL
Where
Element – new element to be inserted in the list
P – new node with which the data is inserted in the list
Get ‘n’ (ie) number of nodes to be created. Repeat the following steps till n
INSERTION ( BY POSITION )
cin>> pos
cin>>element
P = new node
P->data = element
P->link = NULL
Where
Element – new element to be inserted in the list
P – new node with which the data is inserted in the list
1. If the position to be inserted is first
a. If pos = = 1
b. Then assign the address of first node which is available in the head node to
P->link and assign the address of new node to head
i. P->link = head
ii. head = P
2. If position is > 1
a. Assign the head address to a temporary ‘q’ pointer
i. Q=head
b. Travel the list till the (position-1) is reached using q pointer
c. If the position exceeds the last node of the list then the new node cannot
be inserted
d. If the node’s link is NULL then the new node to be inserted is in the last
position
i. Assign the address of new node to be inserted as the link address
of the last node of the list
DELETION ( BY ELEMENT )
1. Assign the first node address to the temporary pointer ‘Q’ to travel the list
a. head = Q
2. If Q -> data = = element then first element is to be deleted
a. Then Assign address to the temporary variable
b. Make Head point to the next address
c. Delete the first node
ELSE
3. Repeat the following steps throughout the list
4. Travel the list till the element to be deleted is found (TEMP)
5. keep track of the previous node of the element to be deleted (TEMP1)
6. Assign the address of the node to be deleted to the temporary variable.
7. Assign TEMP1’s link part address as TEMP’s link address
8. Delete the node
a. While ( Q -> link != NULL)
{
If ( Q->data = = element )
{
Temp = Q
Head = Q -> link
delete Temp
}
Else
TEMP1 =Q
TEMP=Q -> link
If ( TEMP -> data = = element)
{
Temp1 -> link = Temp -> link
delete Temp
}
}
TRAVERSAL
Q = Head
While ( Q != NULL )
{
Display Q -> data
Q = Q ->link
}