Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Linked List

Linked implementation of list


To avoid the linear cost of insertion and deletion
operations, we need to ensure that the elements
of the list are not stored contiguously.
Linked list basically consists of series of structures,
which are not necessarily adjacent in memory.
Each structure contains an element of the list and
a pointer to structure containing its successor.
Linked implementation allow traverse and search
operations to be carried out in linear time.
Insertion and deletion operations can be
implemented efficiently as it requires only
rearrangement of pointers.

Department of CSE 2
Definition of Linked List
A linked list is a linear collection
of data elements, called nodes,
the linear order is given by
pointers. Each node is divided
into two or more parts. Linked list
can be of following types:
Linear linked list or one way list
Doubly linked list or two way list
Circular linked list

Department of CSE 3
Linear Linked List
In a linear linked list or singly linked list, each node is
divided into two parts:
First part contain the information of the element.
Second part called the linked field or next pointer field,
contains the address of next node in the list.

Head is used to store the address of the first element of the


list
Last element of the list have NULL value in the next pointer
field to mark the end of list.

Department of CSE 4
Representation of Linear
linked list
Suppose we want to store the list of integer
numbers, then the linear linked list can be
represented in memory with the following
declarations.
typedef struct nodetype
{
int info;
struct nodetype *next;
}node;
node *head;
The above declaration define a new data type,
whose each element is of type nodetype and
gives it a name node.
Operation on Linear linked lists
Creating an empty list
Traversing a list
Searching an element
Inserting an element
Deleting an element
Creating a Linked List Program
int main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp = 0;
int count = 0;
int choice = 1;
first = 0;
while (choice)

Department of CSE 7
Contd
{
head = (NODE *)malloc(sizeof(NODE));
printf("Enter the data item\n");
scanf("%d", &head-> num);
if (first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)?\n");
scanf("%d", &choice);

Department of CSE 8
Contd..
}
temp->ptr = 0;
/* reset temp to the beginning */
temp = first;
printf("\n status of the linked list is\n");
while (temp != 0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULL\n");
printf("No. of nodes in the list = %d\n", count);
}

Department of CSE 9
Traversing a list
Algorithm for traversing a list
1.PTR=START.[initialize pointer PTR]
2. Repeat steps 3 and 4 while PTR!= NULL.
3.Apply Process to INFO[PTR].
4.Set PTR=LINK[PTR].[PTR now pointes to next
node.]
End of step2 loop
5. Exit.

Here LIST is a linked list in memory. This list is traversed by this


algo and a PROCESS is applied to each node
PTR is a pointer which points to node currently being processed.
INFO[PTR] is the information part of node.

Department of CSE 10
Count the number of elements in
a list
COUNT(INFO,LINK,START,NUM)
1. Set NUM=0
2.PTR=START.[initialize pointer PTR]
3. Repeat steps 4 and 5 while PTR!=
NULL.
4.Set NUM=NUM+1.
5.Set PTR=LINK[PTR].[PTR now pointes
to next node.]
End of step 3 loop
6. Exit.
Department of CSE 11
Searching in Unsorted List
SEARCH(INFO,LINK,START,ITEM,LOC)
1. Set PTR=START.
2. Repeat Step 3 while PTR!=NULL.
3. If ITEM=INFO[PTR], then:
Set LOC=PTR & Exit.
Else
Set PTR=LINK[PTR]
4. Set LOC=NULL[search is
unsuccessful]
5. Exit.

Department of CSE 12
Searching in Sorted List
SRCHSL(INFO,LINK,START,ITEM,LOC)
1. Set PTR=START.
2. Repeat Step 3 while PTR!=NULL.
3. If ITEM>INFO[PTR], then:
Set PTR=LINK[PTR]
Else If ITEM==INFO[PTR], then
Set LOC=PTR & Exit.
Else
Set LOC=NULL & Exit.
4. Set LOC=NULL
5. Exit.
Department of CSE 13
Inserting an element
To insert an element in the list, the first
task is to get a free node, assign the
element to be inserted to the info field
of the node, and then new node is
placed at the appropriate position by
adjusting the appropriate pointer. The
insertion in the list can take place at
the following positions:
At the beginning of the list
After a given node
Into a sorted linked list
At the beginning of list
Step 1. Create a new node and assign the
address to any node say ptr.
Step 2. OVERFLOW,IF(PTR == NULL)
write : OVERFLOW and EXIT.
Step 3. ASSIGN INFO[PTR] = ITEM
Step 4. IF(START = NULL)
ASSIGN NEXT[PTR] = NULL
ELSE
ASSIGN NEXT[PTR] = START
Step 5. ASSIGN START = PTR
Step 6. EXIT

Department of CSE 15
Insertion after a given node
INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM)
1. Set INFO[NEW]=ITEM [ copies new data into
new node]
2. If LOC=NULL, then [Insert as first node]
Set LINK[NEW]=START &START=NEW.
Else
Set LINK[NEW]=LINK[LOC] & LINK[LOC]
=NEW
3. Exit.

Department of CSE 16
Insertion into sorted linked list
Suppose ITEM is to be inserted in a sorted
linked list. Then ITEM must be inserted in
such a way that
INFO[A]<ITEM<INFO[B]

Ifwe can find the location of node A then we


can call the previous algo of inserting a node
after a given location.

Department of CSE 17
Finding the location of last node in a
sorted list such that INFO[LOC]<ITEM or
LOC=NULL
FINDA(INFO,LINK,START,ITEM,LOC)
1. If START=NULL then Set LOC=NULL & Return.
[Empty list]
2. If ITEM<INFO[START] then Set LOC=NULL & Return.
3. Set SAVE= START & PTR=LINK[START]
4. Repeat 5 & 6 while PTR!= NULL.
5. If ITEM<INFO[PTR] then
Set LOC=SAVE & Return
6. Set SAVE =PTR & PTR=LINK[PTR] [Update
pointer]
7. Set LOC=SAVE
8. Return.

Department of CSE 18
Deletion in Linked List
After deleting the node in Linked
List the memory space of deleted
node is returned to the beginning
of the available nodes list.
We will discuss two algorithms of
deletion:
Deleting the node following a
given node
Deleting the node with a given
item of information.

Department of CSE 19
Deleting the node following a given node

Let LIST be a Linked List & we


are given the location LOC of
Node N.
Also we are given location LOCP
of node preceding N.
In case N is first node the
LOCP=NULL.
This algo deletes the node N.

Department of CSE 20
Algorithm (contd..)
DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
1. If LOCP=NULL then
Set START=LINK[START]. [deletes first
node]
Else
Set LINK[LOCP]=LINK[LOC] [deletes node
N]
2. Exit

Department of CSE 21
Deleting the node with a given
Item of Information
For deleting the node with a
given Item of Information we
require two steps:
Find the location LOC of the first
node N which contains the Item and
location LOCP of the node preceding
N.
Then delete the Item present at
location LOC.
So we will discuss two algorithms.
Department of CSE 22
For finding LOC and LOCP
FINDB(INFO,LINK,START,ITEM,LOC,LOCP)
1. If START=NULL then
LOC=NULL & LOCP=NULL & Return [Empty list]
2. If INFO[START]=ITEM then [item in first node]
Set LOC=START & LOCP=NULL & Return.
3. Set SAVE=START & PTR=LINK[START].
4. Repeat steps 5 & 6 while PTR!= NULL
5. If INFO[PTR]=ITEM then
Set LOC=PTR & LOCP=SAVE & Return
6. Set SAVE=PTR & PTR=LINK[PTR].
7. Set LOC=NULL [Search Unsuccessful]
8. Return

Department of CSE 23
Deleting the node N containing given ITEM of
information

DELETE(INFO,LINK,START,AVAIL,ITEM)
1. Call
FINDB(INFO,LINK,START,ITEM,LOC,LOCP)
2. If LOC=NULL then write ITEM not in the list
& Exit.
3. If LOCP=NULL then
Set START = LINK[START] [deletes first
node]
Else
Set LINK[LOCP]=LINK[LOC].
4. Exit.

Department of CSE 24

You might also like