You are on page 1of 4

LIST

Definition of List:

List is an ordered collection of elements in which the elements can be inserted or


deleted at any position.

Implementation:

List can be implemented using array or pointer

LINKED LINEAR LIST

• Implementation of List using pointers is known as Linked Lists


• Each element in the list is referred to a node.

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.

Basic operations in a Single Linked 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

4. Traversal : Display the data in the list


CREATION:

Get the data to be inserted in the new node


i. Allocate memory for the new node pointer
ii. Insert data

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

1. If Head = = NULL then the element to be inserted is the first element


a. Assign the address of P to head
Head = P
2. else the node to be added is at the last of the list
b. Assign the address of first node for the temporary pointer ‘Q”
c. travel the list till the end of the list using this temporary pointer and assign
the address of P to the last node of the list
i. head = Q
ii. while ( Q -> link !=NULL )
{
Q = Q -> temp
}

iii. Q -> link = P


iv. P -> link = NULL

INSERTION ( BY POSITION )

Get the position to be inserted as input (pos)


Get the data to be inserted in the new node
i. Allocate memory for the new node pointer
ii. Insert data

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

for i = 1 to pos-1 and increment i by 1 for each iteration


{
If ( Q -> link = = NULL ) AND ( i != pos-1)
{
Display position is out of the list
}
Q = Q -> link
}

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

If Q -> link = = NULL


Then
Q -> link =P
P -> link =NULL

e. Else the node to be inserted is in the middle of the list


i. P -> link = Q -> link
ii. Q -> link = P

DELETION ( BY ELEMENT )

Get the input element to be deleted in the variable element


cin >> element
create a temporary node pointer to assign the node which is to be deleted from the list

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

1. Assign the first node address to a temporary pointer ‘Q”


2. Travel the list till the end of the list with ‘Q’ pointer
3. display the data part of the node.

Q = Head
While ( Q != NULL )
{
Display Q -> data
Q = Q ->link
}

You might also like