0% found this document useful (0 votes)
8 views24 pages

CHP 3

Uploaded by

Rutuja Jadhav
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)
8 views24 pages

CHP 3

Uploaded by

Rutuja Jadhav
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/ 24

Linked List

Linear data structures

sequence of data elements connected together via links.


Every element is called as node

Nodes are structures made up of data and pointer to


next node
data next

Pointer is called as next. node

It is the second most-used data structure after array.


Linked List contd....
important terms to understand the concept of
Linked List.

⚫ Link − Each link of a linked list can store a data


called an element.
⚫ Next − Each link of a linked list contains a link to
the next link called Next.
Linked List of marks obtained by a
student
80 500 75 200 79 1200 88 NULL

4000 500 200 1200

Fig shows example of marks obtained by student in


different subject

NULL indicates end of linked list


Linked List contd....
A linked structure is introduced to overcome limitations
of arrays and allow easy insertion and deletion

⚫ A collection of nodes storing data items and links to


other nodes

⚫ If each node has a data field and a reference field to


another node called next or successor, the sequence of
nodes is referred to as a singly linked list

⚫ Nodes can be located anywhere in the memory

⚫ The first node is called head and the last node is called
tail
Linked List contd....
⚫ are dynamic data structures that grow and
shrink one element at a time
⚫ is a series of connected nodes
400 1200 memory
⚫ Head
Data Next Data Next Data Null

20 400 30 1200 40
Linked List contd....
⚫ Array allocates memory for all its elements in
one block(consecutive)
Eg int a[6];
200 202 204 206 208 210

0 1 2 3 4 6

Linked List (data not consecutive):


Data is scattered in memory and connected by
link
Data next data null data null
Why Linked List
⚫ Array is static

⚫ Once an array is created, its size is fixed. It


cannot be changed.

⚫ Although Fast element access

⚫ While many applications require resizing, static


arrays are impossible to resize

⚫ Required size is not always immediately available


Why Linked List contd....
⚫ Storing data items in arrays has at least two
limitations
⚫ The array size is fixed once it is created: Changing
the size of the array requires creating a new array
and then copying all data from the old array to the
new array

⚫ The data items in the array are next to each other in


memory: Inserting an item inside the array requires
shifting other items

⚫ A linked structure is introduced to overcome


limitations of arrays and allow easy insertion and
deletion
Advantages of Linked List
⚫ Dynamic

⚫ Efficient memory utilization

⚫ Memory can be deallocated

⚫ Insertion and deletion easy

⚫ Used for more complex application


Disadvantages of Linked List
⚫ Complex

⚫ Uses additional memory to store pointer

⚫ Searching of element is time consuming


Difference between Array and
Linked List
Array Linked List
Fixed Variable
Static Dynamic
Less memory than linked list Extra memory for pointer
Inserting element in between is Easy
complex
Access any element directly Access sequentially starting
from first node
Deleting is also complex Easy
Easy to understand Complex
Sorting is easy Not easy
Difference between Array and
Linked List contd.....
Array Linked List
Memory cannot be freed Can be freed using free
function
Size is defined in advance Size is not defined
Basic Operations
⚫ Insertion − Adds an element at the
beginning of the list.
⚫ Deletion − Deletes an element at the
beginning of the list.
⚫ Display − Displays the complete list.
⚫ Search − Searches an element using the
given key.
⚫ Traverse- Print all elements
Applications of linked list
Computer science –
⚫ Implementation of stacks and queues
⚫ Implementation of graphs : Adjacency list
representation of Graphs is most popular which is
uses linked list to store adjacent vertices.
⚫ Dynamic memory allocation : We use linked list of
free blocks.
⚫ Maintaining directory of names
⚫ Performing arithmetic operations on long integers
⚫ Manipulation of polynomials by storing constants in
the node of linked list
⚫ representing sparse matrices
Applications of linked list
Real world-
⚫ Image viewer – Previous and next images are
linked, hence can be accessed by next and
previous button.
⚫ Previous and next page in web browser –
We can access previous and next url
searched in web browser by pressing back
and next button since, they are linked as
linked list.
⚫ Music Player – Songs in music player are
linked to previous and next song. you can
play songs either from starting or ending of
the list.
Types of Linked List
⚫ Simple(Single) Linked List − Item navigation is
forward only.

⚫ Doubly Linked List − Items can be navigated


forward and backward.

⚫ Circular Linked List − Last item contains link of


the first element as next and the first element has
a link to the last element as previous.
Functions in Linked list

malloc() used to allocate memory

Free() used to deallocate the memory


Creation of Node
struct linked_list
{
int data;
struct linked_list *next;
};

node
Data next
C program
struct linked_list
{
int data;
struct linked_list *next; data next
}; new 20
NULL
void main
{
struct linked_list *new=NULL;
new=(struct linked_list*) malloc(sizeof(struct linked_list));
printf(“Enter data\n”);
scanf(“%d”, &new->data);
new->next=NULL;
}
typedef ...
struct linked_list
{
int data;
struct linked_list *next;
} typedef struct linked_list node;
void main
{
node *new=NULL;
new=(node *) malloc(sizeof(node));
printf(“Enter data\n”);
scanf(“%d”, &new->data);
new->next=NULL;
}
Creating 3 nodes
struct node
{
int data;
struct node * next;
};
void main() 2500
{ first 10 1500
struct node *first,*second,*third;
first=(struct node*)malloc(sizeof(node));
second=(struct node*)malloc(sizeof(node));
third=(struct node*)malloc(sizeof(node)); 1500
first->data=10;
first->next=second;
20 4000
second->data=20; second
second->next=third;
third->data=30; 4000
third->next=NULL;
} third 30 NULL
Printing Elements
struct node
{
int data;
struct node * next;
};
void main()
{ struct node *first,*second,*third,*head;
first=(struct node*)malloc(sizeof(node));
second=(struct node*)malloc(sizeof(node));
third=(struct node*)malloc(sizeof(node));
first->data=10;
first->next=second;
second->data=20;
second->next=third;
third->data=30;
third->next=NULL;
printf(“Data\n”);
head=first;
while(head!=NULL)
{ printf(“%d\n”,head->data);
head=head->next;
}
}
Linked list to store student rollno and name
struct node
{ roll name ptr
int roll;
char name[30];
struct 2bytes 30bytes 2bytes
};
void main()
{
struct node *s1,*s2,*s3;
s1=(struct node*)malloc(sizeof(node));
s2=(struct node*)malloc(sizeof(node)); s1 1 ana 1500
s3=(struct node*)malloc(sizeof(node));
s1->roll=1 ;
strcpy( s1->name,’ana’);
s1->next=s2;
s2->roll=2;
strcpy( s2->name,’sam’); s2 2 sam 4000
s2->next=s3;
s3>roll=3; 1500
strcpy(s3->name,’john’);
s3->next=NULL ;

} s3 30 john NULL

4000

You might also like