0% found this document useful (0 votes)
9 views35 pages

4 Linked List

Uploaded by

Shrawani Dongre
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)
9 views35 pages

4 Linked List

Uploaded by

Shrawani Dongre
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/ 35

PIMPRI CHINCHWAD EDUCATION TRUST’S

PIMPRI CHINCHWAD POLYTECHNIC

NBA Accredited Institute


I.S.O. Certified Organization, Approved by A.I.C.T.E.
Affiliated to M.S.B.T.E. Mumbai

Address : Pimpri Chinchwad Polytechnic


Sector No. 26, Pradhikaran, Nigdi,
Pune - 411 044
Phone: 020 - 2765 4156 / 2765 8797
Fax: 27654156
Email: principal@pcpolytechnic.com
Website: www.pcpolytechnic.com
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad Polytechnic


Sector No. 26, Pradhikaran, Nigdi, Pune – 411044
(NBA Accredited / An ISO 9001:2015 Certified)
Phone: 020-27654156 / 27658797 Fax: 27654156
Website : www.pcpolytechnic.com Email:
principal@pcpolytechnic.com

Department : Computer Engineering


Course : Data Structures Using ‘C’
Course Code : 22317

Course Outcomes (COs) :


a. Perform basic operations on arrays
b. Apply different searching and sorting techniques.
c. Implement basic operations on stack and queue using array
representation.
d. Implement basic operations on Linked List.
e. Implement program to create and traverse tree to solve problems.

Pimpri Chinchwad Polytechnic, Computer Department


Course : Data Structures Using ‘C’
Course Code : 22317

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

03 Data Structures with ‘C’ (SIE)


(Schaum’s Outline Series)


Lipschutz
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

4.1 Introduction to Linked List Terminologies: node, Address, Pointer, Information


field / Data field, Next pointer, Null Pointer, Empty list.
4.2 Type of lists:
· Linear list
· Circular list
4.3 Operations on a singly linked list
· Traversing a singly linked list
· Searching a linked list
· Inserting a new node in a linked list
· Deleting a node from a 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;

Some Linked List Terminologies:

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 :

The basic operations that can be performed on a list are:


1. Creation: This operation is used to create a node in the linked list.
2. Insertion: This operation is used to insert a new node in the linked list.
At the beginning of the list, At a certain position and At the end.
3. Deletion: This operation is used to delete node from the list.
At the beginning of the list / To delete first element, At a certain position, At the end.
4. Traversing: It is a process of going through all the nodes of a linked list from one
end to the other end.
5. Display: This operation is used to print all nodes information field.
6. Searching: To search a specific element in given linked list.
7. Count: To count number of nodes present in list.
8. Erase all: To free/delete all elements from the list.
9. Reverse: This operation is used to print node‟s information field in reverse order i.e. from last to first.
10. Sorting: To arrange the entire node‟s information field in ascending or descending order.

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.

used in array. used in linked list.

Now let's have a quick look at the methods used for dynamic memory allocation / deallocation.

malloc() allocates single block of requested memory.

calloc() allocates multiple block of requested memory.

realloc() reallocates the memory occupied by malloc() or calloc() functions.

free() frees the dynamically allocated memory.

Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :

The malloc( ) function :


The malloc() function allocates single block of requested memory.
It doesn't initialize memory at execution time, so it has garbage value initially.
It returns NULL if memory is not sufficient.
The syntax of malloc() function is given below:
ptr=(cast-type*)malloc(byte-size)
Ex.
temp = (struct node*)malloc(sizeof(struct node));

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 :

* Algorithm to print each node of singly linked list:-

[Assuming that linked list is created ]

step1:-[initialize] SET PTR = START

step2:-Repeat steps 3 & 4

while(PTR!=NULL)

step3:-print PTR->DATA

step4:-SET PTR = PTR->NEXT

[End of while loop]

step5:-Exit.

Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :

* Algorithm to traverse a singly linked list :-

[assuming that the linked list is created]

step1:-[initialize] SET PTR=START

step2:-Repeat steps 3 &4

while (PTR !=NULL)

step3:- apply the process [to PTR->DATA]

[either print data (print PTR->DATA), compare data (VALUE == PTR->DATA) etc]

step4:-SET PTR=PTR->NEXT

[End of while loop]

step5:-Exit.

Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :

* Algorithm to count the number of nodes in a singly linked list:-

step1:-[initialize] SET PTR =START, SET count=0

step2:-Repeat steps 3 & 4 while (PTR!=NULL)

step3:- count =count+1

step4:- SET PTR =PTR-> NEXT

[End of while loop]

step5:- display count

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 :

* Algorithm to delete the node at beginning from singly 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:- SET START=START->NEXT
step4:- SET PTR->NEXT =NULL
step5:- free (PTR)
step6:- Exit.

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 :

* Algorithm to print each node of list:-

[Assuming that linked list is created ]

step1:-[initialize] SET PTR = START

step2:-Repeat steps 3 & 4

while(PTR->next !=START)

Step3:- print PTR->DATA

step4:-SET PTR = PTR->NEXT

[End of while loop]

Step5: print PTR->DATA

step6:-Exit.

Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :

* Algorithm to traverse a list :-

[assuming that the linked list is created]

step1:-[initialize] SET PTR=START

step2:-Repeat steps 3 &4

while (PTR->next !=START)

step3:- apply the process [to PTR->DATA]

[either print data (print PTR->DATA), compare data (VALUE == PTR->DATA) etc]

step4:-SET PTR=PTR->NEXT

[End of while loop]

Step5:-apply process to PTR->DATA

Step6: Exit.

Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :

* Algorithm to count the number of nodes in a list:-

step1:-[initialize] SET PTR =START, SET count=0

step2:-Repeat steps 3 & 4 while (PTR->next !=START)

step3:- count =count+1

step4:- SET PTR =PTR-> NEXT

[End of while loop]

step5:- count =count+1

step6 :- display count

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 :

Inserting a Node at the End of a Circular Linked List:

Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)
Linked List :

* Algorithm to insert a node at end:-


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 val to the data field of node and START to address field].
SET newnode ->DATA = val
SET newnode -> NEXT= START
step4:- SET PTR=START
[Traverse the pointer ptr to the end node of the linked list].
while (PTR->NEXT!= START)
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 :- 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)

* Algorithm to delete the node at mid_position :- If (PTR2->DATA==KEY) Then


step1:- [check whether the list is exist ,if not then exit] i) SET FLAG=1
If (START ==NULL) Then ii) break
Display :- list is empty AND Exit [End of lf]
ELSE
go to step 2 SET PTR2= PTR2->NEXT
[End of if ]. SET PTR1=PTR1->NEXT
step2:-SET PTR1=START
SET PTR2=START->NEXT [End of while loop].
step3:- [Accept the key to delete the node and traverse If (FLAG==1) Then
the list till PTR2 reaches to last node ; if key found, i) SET PTR1 ->NEXT =PTR2->NEXT
delete that node ]. ii) SET PTR2->NEXT =NULL
iii) free(PTR2);
ELSE
Display : keynode not found
[End of if]
step4:- Exit.

Pimpri Chinchwad Polytechnic, Computer Department Data Structures Using ‘C’ (22317)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy