0% found this document useful (0 votes)
37 views19 pages

Link List

DSA

Uploaded by

Debasish Vishal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views19 pages

Link List

DSA

Uploaded by

Debasish Vishal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Data Structure

Link List

Prepared by
Dalton Meitei Thounaojam
Link List

 It is a linear collection of data elements call nodes.


 It does not store its elements in consecutive
memory locations.
 It is a data structure which in turn can be used to
implement other data structure such as stack and
queue.

Node

data *next
Fig.: A Link Node.
Link List vs Array

 a linked list does not store its nodes in consecutive


memory locations.
 a linked list does not allow random access of data.
 we can add any number of elements in the list.
Link List

struct node
{
int node;
struct node *next;
};
Types of Link List

 Singly Link List

 Circular Link List

 Doubly Link List

 Circular Doubly Link List


Example Function to create a Link List
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf(“\n Enter -1 to end”);
printf(“\n Enter the data : “);
scanf(“%d”, &num);
while(num!=-1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data=num;
if(start==NULL)
{
new_node -> next = NULL;
start = new_node;
}
Example Function to create a Link List

else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next = new_node;
new_node->next=NULL;
}
}
return start;
}
Operations on a Link List

 Traversing a Linked List


 Searching for a Value in a Linked List
 Inserting a New Node in a Linked List
 Inserting a Node at the Beginning of a Linked List
 Inserting a Node at the End of a Linked List
 Inserting a Node After a Given Node in a Linked List
 Inserting a Node Before a Given Node in a Linked List

 Deleting a Node from a Linked List


 Deleting the First Node from a Linked List
 Deleting the Last Node from a Linked List
 Deleting the Node After a Given Node in a Linked List
 Deleting the Node Before a Given Node in a Linked List
Traversing a Link List

Step 1: [INITIALIZE] SET PTR = START

Step 2: Repeat Steps 3 and 4 while PTR != NULL

Step 3: Apply Process to PTR -> DATA

Step 4: SET PTR = PTR -> NEXT

[END OF LOOP]

Step 5: EXIT
Counting the nodes in a Link List

Step 1: [INITIALIZE] SET COUNT = 0


Step 2: [INITIALIZE] SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR != NULL
Step 4: SET COUNT= COUNT+1
Step 5: SET PTR = PTR ->NEXT
[END OF LOOP]
Step 6: Write COUNT
Step 7: EXIT
Searching in a Link List

Step 1: [INITIALIZE] SET PTR = START


Step 2: Repeat Step 3 while PTR != NULL
Step 3: IF VAL = = PTR -> DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR -> NEXT
[END OF IF]
[END OF LOOP]
Step 4: SET POS = NULL
Step 5: EXIT
Inserting at the beginning in a Link List

Step 1: IF AVAIL = NULL


Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL ->NEXT
Step 4: SET NEW_NODE->DATA = VAL
Step 5: SET NEW_NODE ->NEXT = START
Step 6: SET START = NEW_NODE
Step 7: EXIT
Inserting at the end in a Link List

Step 1: IF AVAIL = NULL


Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE ->DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR -> NEXT != NULL
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR NEXT = NEW_NODE
Step 10: EXIT
Inserting after a given node in a Link List

Step 1: IF AVAIL = NULL


Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PREPTR -> DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 1 : PREPTR -> NEXT = NEW_NODE
Step 11: SET NEW_NODE -> NEXT = PTR
Step 12: EXIT
Inserting before a given node in a Link List

Step 1: IF AVAIL = NULL


Write OVERFLOW
Go to Step 12
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET PTR = START
Step 6: SET PREPTR = PTR
Step 7: Repeat Steps 8 and 9 while PTR ->DATA != NUM
Step 8: SET PREPTR = PTR
Step 9: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 1 : PREPTR -> NEXT = NEW_NODE
Step 11: SET NEW_NODE -> NEXT = PTR
Step 12: EXIT
Deleting first node in a Link List

Step 1: IF START = NULL


Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START -> NEXT
Step 4: FREE PTR
Step 5: EXIT
Deleting last node in a Link List

Step 1: IF START = NULL


Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
Deleting after a node in a Link List

Step 1: IF START = NULL


Write UNDERFLOW
Go to Step 1
[END OF IF]
Step 2: SET PTR = START
Step 3: SET PREPTR = PTR
Step 4: Repeat Steps 5 and 6 while PREPTR -> DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 7: SET TEMP = PTR
Step 8: SET PREPTR -> NEXT = PTR -> NEXT
Step 9: FREE TEMP
Step 1 : EXIT
Thank you

You might also like