4 Linked List
4 Linked List
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Suggested Learning Resources :
Balgurusamy, E.
01 Data Structures using ‘C’
●
● McGraw Hill Education, New Delhi
ISRD Group
02 Data Structures using ‘C’
●
● McGraw Hill Education, New Delhi
Steve Qualline
04 Practical ‘C’ Programming
●
● O’Reilly Media
05 Data Structures
●
●
Dr. Rajendra Kawale
Devraj Publicatiions
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Suggested Learning Websites:
❏ http://nptel.ac.in/courses/106102064/1
❏ www.oopweb.com/algorithms
❏ www.studytonight.com/data-structures/
❏ www.cs.utexas.edu/users
❏ liscs.wssu.edu
❏ http://www.academictutorials.com/data-structure/
❏ http://www.sitesbay.com/data-structure/c-data-structure
❏ http://www.indiabix.com
❏ http://www.khanacademy.org/
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Unit : IV
Linked List
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
typedef struct node
Node
{
int data;
struct node *next;
Data/info Link / address of
next node }node;
Node :- A linked list is a data structure which is used to implement a list of elements(nodes)
that are arranged in some order. Each element in the list is represented as a single cell or node,
each of which is made up two portions: data field – information relevant to the node link field
– address of the next node in the list.
NULL Pointer :- A linked field of the last node contains Null rather than a valid address it
indicates end of the list.
Next Pointer :- Which hold the Address of the next node, which in turn links the nodes
together.
Empty List :- An empty list is a list that contains no data records. It contains no nodes.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Dynamic Memory Allocation : The concept of dynamic memory allocation in c language enables the C
programmer to allocate memory at runtime. Dynamic memory allocation/deallocation in c language is possible by
4 functions of stdlib.h header file.
1. malloc()
static memory allocation dynamic memory allocation
2. calloc()
3. realloc()
memory is allocated at compile time. memory is allocated at run time.
4. free()
memory can't be increased while executing program. memory can be increased while executing program.
Now let's have a quick look at the methods used for dynamic memory allocation / deallocation.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Algorithm to create a singly linked list :-
step1:- SET START =NULL
SET PTR =NULL
step2:- Accept the total number of nodes to be created (N).
step3:- FOR I=0 to N-1 loop
a] [create a new node]
newnode= (struct node *) malloc(sizeof (struct node));
b] [Assign the data to the data field and NULL to the address field]
SET newnode->DATA= val
SET newnode->NEXT= NULL
c] If (START == NULL) Then
i) START =newnode
ii) PTR = newnode
[ Above subpoints means to assign the start pointer to first node & assign the ptr to the 1st node i.e. newnode].
ELSE
i) PTR->NEXT=newnode
ii) PTR= newnode
[Assign newnode to the next of the last node of the list].
[End of if]
[End of For].
step4:- Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
while(PTR!=NULL)
step3:-print PTR->DATA
step5:-Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
[either print data (print PTR->DATA), compare data (VALUE == PTR->DATA) etc]
step4:-SET PTR=PTR->NEXT
step5:-Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
step6:- Exit
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
* Algorithm to insert a node at beginning in singly linked list:-
step1:- [create a new node]
newnode=(struct node * ) malloc (sizeof(struct node ));
step2:- [check overflow condition ; if overflow then exit]
If (newnode= = NULL) Then
write: Node creation failed and exit.
ELSE go to step 3
[end of if]
step3:- [Assign the data to the data field and START to the address field]
SET newnode ->DATA = val
SET newnode ->NEXT= START
SET START = nrenode
step4:- Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
* Algorithm to insert a node at end in singly linked list :-
Linked List :
step1:- [create a new node]
newnode=(struct node *) malloc (sizeof(struct node ));
Step2:- If (newnode = = NULL) Then
write: Node creation failed and exit
ELSE go to step 3
[end of if]
step3:- [Assign the data to the data field of node and NULL to address field].
SET newnode ->DATA = data
SET newnode -> NEXT= NULL
step4:- SET PTR=START
[Traverse the pointer ptr to the end node of the linked list].
while (PTR->NEXT!= NULL)
SET PTR=PTR->NEXT
step5:- SET PTR->NEXT= newnode
step6:- Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List : [Accept the key]
* Algorithm to insert a node at mid_positon in SET PTR =START
singly linked list :- while (PTR->NEXT!= NULL)
step1:- [create a new node] If (PTR->DATA==KEY) Then
newnode=(struct node * ) malloc (sizeof(struct node )); SET FLAG=1 and break
If (newnode = = NULL) Then [End of lf]
write: Node creation failed and exit SET PTR=PTR->NEXT
ELSE go to step 3 [End of while loop].
[ end of if] If (FLAG==1) Then
step3:- [assign the data to the data field of node and assign SET newnode ->NEXT= PTR->NEXT
NULL to next field]. SET PTR->NEXT=newnode
SET newnode ->DATA =val ELSE
SET newnode -> NEXT= NULL Display:- key not found hence new node cannot
step4:-[Search for the node after which new node has to be be inserted .
inserted. Set ptr to the start node and traverse the ptr till ptr [End of if].
reaches to last node, if you get the key, insert a new node step5:- Exit.
after it.]
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List : * Algorithm to delete the node at end:-
step1:- [check whether the list exist, if not then exit]
If (START ==NULL) Then
Display :- list is empty and Exit.
ELSE
go to step 2
[End of if ].
step2:-SET PTR1=START
SET PTR2=START->NEXT
step3:-[Traverse the list till PTR2 reaches to the last node & PTR1
reaches to the second last node ].
while(PTR2->NEXT ! =NULL)
SET PTR2=PTR2->NEXT
SET PTR1= PTR1->NEXT
[End of while]
step4:- SET PTR1->NEXT =NULL
step5:- free(PTR2)
step6:- Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Accept Key
Linked List : while (PTR2->NEXT!= NULL)
{
* Algorithm to delete the node at mid_position :- If (PTR2->DATA==KEY) Then
step1:- [check whether the list is exist ,if not then exit] {
If (START ==NULL) Then SET FLAG=1;
Display :- list is empty Exit break;
ELSE }
go to step 2 [End of lf]
[End of if ]. SET PTR2= PTR2->NEXT
step2:-SET PTR1=START SET PTR1=PTR1->NEXT
SET PTR2=START->NEXT }
step3:- [Accept the key to delete the node and traverse [End of while loop].
the list till PTR2 reaches to last node ; if key found, If (FLAG==1) Then
delete that node ]. SET PTR1 ->NEXT =PTR2->NEXT
SET PTR2->NEXT =NULL
free(PTR2);
[End of if]
ELSE
Display: Key not found
step4:- Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Algorithm to create a circular linked list :-
step1:- SET START =NULL
SET PTR =NULL
step2:- Accept the total number of nodes to be created (N).
step3:- FOR I=0 to N-1 loop
a] [create a new node]
newnode= (struct node *) malloc(sizeof (struct node));
b] [Assign the val to the data field]
SET newnode->DATA= val
c] If (START == NULL) Then
i) START =newnode
ii) PTR = newnode
iii) SET newnode->NEXT= START
[ Above subpoints means to assign the start pointer to first node & assign the ptr to the 1st node i.e. newnode].
ELSE
i) PTR->NEXT=newnode
ii) PTR= newnode
iii) SET newnode->NEXT= START
[Assign newnode to the next of the last node of the list].
[End of if]
[End of For].
step4:- Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
while(PTR->next !=START)
step6:-Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
[either print data (print PTR->DATA), compare data (VALUE == PTR->DATA) etc]
step4:-SET PTR=PTR->NEXT
Step6: Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
step7:- Exit
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
* Algorithm to insert a node at beginning:-
step1:- [create a new node]
newnode=(struct node * ) malloc (sizeof(struct node ));
[check overflow condition ; if overflow then exit]
If (newnode= = NULL) Then
write: Node creation failed and exit.
ELSE go to step 2
[end of if]
step2:- [Assign the val to the data field and START to the address field]
SET newnode ->DATA = val
SET newnode ->NEXT= START
Step 3: Set ptr=START
Step 4: traverse ptr to last node
While(ptr->next!=start)
Set ptr=ptr->next
Step 5: set ptr->next=newnode
Set start=newnode
step 6: exit
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List : [Accept the key]
* Algorithm to insert a node at mid_positon :- SET PTR =START
step1:- [create a new node] while (PTR->NEXT!= START)
newnode=(struct node * ) malloc (sizeof(struct node )); If (PTR->DATA==KEY) Then
If (newnode = = NULL) Then SET FLAG=1;
write: Node creation failed and exit and break;
ELSE go to step 2 [End of lf]
[ end of if] SET PTR=PTR->NEXT
step2:- [assign the value to the data field of node and [End of while loop].
assign NULL to next field]. If (FLAG==1) Then
SET newnode ->DATA = value SET newnode ->NEXT= PTR->NEXT
SET newnode -> NEXT= NULL SET PTR->NEXT=newnode
step3:-[Search for the node after which new node has to be ELSE
inserted. Set ptr to the start node and traverse the ptr last Display:- key not found hence new node cannot
node, if you get the key after which node has to inserted be inserted .
,insert a new node there.] [End of if].
step4:- Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
* Algorithm to delete the node from beginning from circular linked list:-
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :
* Algorithm to delete the node at beginning from circular linked list:-
step1:- [check whether the list is exist ,if not then exit]
If (START ==NULL) Then
Display :- list is empty and Exit
ELSE
go to step 2
[End of if ].
step2:- SET PTR=START
step3:- Repeat step 4 while PTR->NEXT != START
Srep4:- SET PTR = PTR->NEXT
[END OF WHILE LOOP]
step4:- SET PTR->NEXT =START->NEXT
step5:- SET PTR=START
step6:- SET START=START->NEXT
step7:- SET PTR->NEXT=NULL
step8:- free (PTR)
step9:- Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List : * Algorithm to delete the node from end from circular linked list:-
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List : * Algorithm to delete the node at end from circular linked list:-
step1:- [check whether the list is exist ,if not then exit]
If (START ==NULL) Then
Display :- list is empty and Exit
ELSE
go to step 2
[End of if ].
step2:-SET PTR1=START
SET PTR2=START->NEXT
step3:-[Traverse the list PTR2 reaches to the last node & PTR
reaches to the second last node ].
while(PTR2->NEXT ! =START)
SET PTR2=PTR2->NEXT
SET PTR1= PTR1->NEXT
[End of while]
step4:- SET PTR1->NEXT = START
step5:- set PTR2->NEXT=NULL
step6:- free(PTR2)
step7:- Exit.
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Accept Key
Linked List : while (PTR2->NEXT!= START)
Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)