Linked List

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

Link List

Module 3: Linked List


Topics Hours Weightage Books

Introduction, Representation of Linked


List, Linked List v/s Array, Types of
Linked List - Singly Linked List, Circular
T2, T3
Linked List, Doubly Linked List,
and T4,
Operations on Singly Linked List and 10 26%
R1 . R2 ,
Doubly Linked List, Stack and Queue
R5
using Singly Linked List, Singly Linked
List Application-Polynomial
Representation and Addition

2
● Linked list is a very common data structure used to store similar data
in memory
● The elements of the linked list are not stored in contiguous memory
location.
● They are scattered all over the memory but these elements are still
bounded to each other.
● This order and bonding between the elements is maintained by
explicit links between them called as linear linked list.
● i.e. if the memory is allocated before the execution of a program, it is
fixed can not be changed. We have to adopt an alternative strategy to
allocated memory only when it is required. There is a special data
structure called as “ LINKED LIST”.
● A LINK LIST is a non sequential data structure because elements of
linked list are not store in sequence like array.
● Linked list is a collection of nodes, where each node is divided into 2
fields, the first field is called as INFORMATION field and second field
called as NEXT/ LINK field.
● The INFORMATION fields holds the actual data element/ or data values.
● The NEXT/ LINK fields holds the address of next node in the list. Such an
address , which is used to access a particular / next node , is known as
pointer.
● The entire linked list is accessed by an external pointer called ad START
that points to the address of first node in the list.
● The NEXT/ LINK field of the last node contains a special value called as
“NULL” , This NULL pointer is used to indicate the end of the list.
Structure of LINKED LIST
NODE
INFORMATION NEXT

start
200 300
100
10 200 20 300 30 400
400

40 NULL
Difference between array and linked list
Linked list Array
The linked list is a collection of nodes and each The array is a collection of similar types of data
node is having information and next link field elements. In array the data is always stored at
some index of the array

Physically data can be deleted Only logical deletion of the data is possible

Insertion and deletion of data is easy. Insertion and deletion of data is difficult.

Memory allocation is dynamic. Hence Memory allocation is static. Hence once the
developer allocate as well as deallocate fixed amount of size is declared then that
memory and so no wastage of memory is memory is allocated. Therefore there is a
there. chance of memory wastage
Memory allocation and deallocation
● Dynamic memory and pointers:-
● In array, the memory allocation for variable and other data is done at
“compilation time”, it is also called as “STATIC ALLOCATION”
● Hence there are situation in programming language where a data
structure needs memory allocation to be done at RUN TIME. These run
time memory allocation for variables called as “ DYNAMIC MEMORY
ALLOCATION”.
● This techniques allows to allocate additional memory space or
release unwanted memory space at run time or optimize the use
of storage space.
The dynamic memory allocation and reallocation used following
memory management functions:-

1. malloc() function : allocate a block of memory.


p=malloc(); { p=malloc(sizeof(struct node)); }
2. free() function: frees (releases )the memory space.
free(p);
3. realloc() function: when the previously allocated memory is not sufficient or it is much
more than necessary, we can change the memory size that was already allocated by some
malloc function. Ex: p1=malloc(100); p2=realloc(p1,500);
4. calloc(): is used to allocate multiple blocks of memory of same size.
Types Of Linked List (LL)

1. SLL(Single Link List)

2. DLL (Doubly Link List)

3. CLL(Circular Link List)


Operation on LL(SLL)
1. Create Link List
2. Add node at beginning
3. Add at after any node
4. Delete node
5. Search node
6. Count number of nodes
7. Display link list
Linked list as an ADT (Abstract Data type)
● Create Link List
● Add node at begining
● Add at after any node
● Delete node
● Search node
● Count number of nodes
● Display node on reverse order
● Display link list
Create node

struct node
{
int data;
struct node *next; (This statement declares a variable named next as type of pointer to
struct node.)

} *start=null,*p,*q;

For allocation
t=(struct node *)malloc(sizeof(struct node *));
Create LL
Initially start=null;
void createLL() else
{ { q=start;
p=malloc(sizeof(struct node)); while(q->next!=null)
scan element/data say X; q=q->next;
p->data=x; q->next=p;
p->next=null }
if(start==null) }
{
start=p;
}
Add node / element at begining

AddAtBegining()
{
p=malloc(sizeof(struct node));
scan element/data say X;
p->data=x;
p->next=null;
p->next=start;
start=p;
}
Add node after
AddAfter()
{ p=malloc(sizeof(struct node));
q=start; p->next=q->next;
scan element/data say X; p->data=x;
Scan the position after which node you want to
q->next=p;
insert new node i.e. pos
}
for(i=0;i<pos-1;i++)
{
q=q->next;
/ * if(q==null)
{
printf(“\n Unable add new node”);
Return
} */ }
Display
display()
{
if(start==null)
printf(“\n Link list is empty”);
else
{
q=start;
printf(“\n The elements are”);
while(q!=null)
{
printf(“\n %d”,q->data);
q=q->next;
}}}
Search()
{
int pos1=1;
printf(“\n which element you want to search”) i.e. scan n;
ser=start;
while(ser!=null)
{
if(ser->data==n)
{
printf(“\n %d found at position %d”,n,pos1);
Return;
}
ser=ser->next;
pos1=pos1+1;
}
if(ser==null)
printf(“\n Number %d not found in LL”,n);
}
Count
count()
{
int cnt=0;
q=start;
while(q!=null)
{
q=q->next;
cnt=cnt+1;
}
printf(“\n number of node in a given list is %d”,cnt);
}
Delete()
Delete element from list
{
/* Initially check whether the Link list is empty or not */
if( start==null)
printf(“\n Link lIst is empty”);
Otherwise
/* delete first element*/
Enter the element which you want to delete i.e.Say n;
If( start->data==n)
{ p=start;
start=start->next;
free(p);
return; }
q= start;
/* for in between element */
while(q->next->next!=null)
{
if(q->next->data==n)
{
p=q->next;
q->next=p->next;
free(p);
Return;
}
q=q->next;
}
/* for last node */
if(q->next->data==n)
{
p=q->next;
free(p);
q->next=null;
Return;
}
printf(“\n % not found”,n);
}
Stack using LL
Operation of stack:
1. Push
2. Pop
3. Display
4. Exit
Stack using LL
Stack using LL
Stack using LL
POP

Before pop
Stack using LL
Stack using LL
Stack using LL
Stack using LL
Stack using LL
Stack using LL
Stack using LL
Stack using LL
Output
Queue using Linked List

Fig. Linked list representation of Queue

Operation on queue
1. Insertion(Enqueue)
2. Deletion(Dequeue)
3. Display
Operation on queue
1. Insertion
Operation on queue
1. Deletion
#include<stdio.h>
#include<stdlib.h>
switch(choice)
struct node
{
{
case 1:
int data;
insert();
struct node *next;
break;
};
case 2:
struct node *front;
delete();
struct node *rear;
break;
void insert();
case 3:
void delete();
display();
void display();
break;
void main ()
case 4:
{
exit(0);
int choice;
break;
while(choice != 4)
default:
{
printf("\nEnter valid choice??\
printf("\n*******Main Menu***********\n"); n");
printf("\ }
n===========================\n");
}
printf("\n1.insert an element\n2.Delete an
}
element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
void insert() if(front == NULL)
{ {
struct node *ptr; front = ptr;
int item; rear = ptr;
front -> next =
ptr = (struct node *) malloc (sizeof(struct NULL;
node));
rear -> next =
if(ptr == NULL) NULL;
{
}
printf("\nOVERFLOW\n");
else
return;
{
}
rear -> next = ptr;
else
rear = ptr;
{
printf("\nEnter value?\n");
rear->next = NULL;
scanf("%d",&item);
}
ptr -> data = item;
}
void display()
void delete () {
{ struct node *ptr;
struct node *ptr; ptr = front;
if(front == NULL) if(front == NULL)
{ {
printf("\ printf("\nEmpty queue\
nUNDERFLOW\n"); n");
return; }
} else
else { printf("\nprinting values
{ .....\n");
ptr = front; while(ptr != NULL)
front = front -> {
next; printf("\n%d\n",ptr ->
free(ptr); data);
} ptr = ptr -> next;
} }
}
Doubly Linked List(DLL)
Doubly Linked List is a variation of Linked list in which navigation is possible in both
ways either forward and backward easily as compared to Single Linked List. Following
are important terms to understand the concepts of doubly Linked List

Link − Each Link of a linked list can store a data called an element.
Next − Each Link of a linked list contain a link to next link called Next.
Prev − Each Link of a linked list contain a link to previous link called Prev.

LinkedList − A LinkedList contains the connection link to the first Link called First and to
the last link called Last.
Doubly Linked List Representation
Following are the important points to be considered.

➢ Doubly LinkedList contains a link element called first and last.


➢ Each Link carries a data field(s) and a Link Field called next.
➢ Each Link is linked with its next link using its next link.
➢ Each Link is linked with its previous link using its prev link.
➢ Last Link carries a Link as null to mark the end of the list.
Basic Operations:-
Following are the basic operations supported by an list.
1. Insertion − add an element at the beginning of the list.
2. Deletion − delete an element at the beginning of the list.
3. Insert Last − add an element in the end of the list.
4. Delete Last − delete an element from the end of the list.
5. Insert After − add an element after an item of the list.
6. Delete − delete an element from the list using key.
7. Display forward − displaying complete list in forward manner.
8. Display backward − displaying complete list in backward manner.
Node Declaration

struct node
{
int data;
prev next
struct node* next;
struct node* prev;
} *start=null;
Create DLL

void create() else


{ {
p=(struct node*)malloc(sizeof (struct node)); while(temp->next != null)
scanf(“%d”,&x); {
p->data=x; temp=temp->next;
p->next=null; }
p->prev=null;
temp->next=p;
temp=start;
p->prev=temp;
if(start==null)
{ start=p; temp=p;
temp=start; }
start->next=null; }
start->prev=null;
}
Inserting a New Node in a Doubly Linked List
In this section, we will discuss how a new node is added into an already
existing doubly linked list. We will take four cases and then see how insertion
is done in each case.

Case 1: The new node is inserted at the beginning.


Case 2: The new node is inserted at the end.
Case 3: The new node is inserted after a given node.
Case 4: The new node is inserted before a given node.
Inserting a Node at the Beginning of a Doubly Linked List

Consider the doubly linked list shown in Suppose we want to add a new node with data
9 as the first node of the list. Then the following changes will be done in the linked list.
Insert node at begining

void insertAtBegin()
else
{int x;
{ temp=start;
p=(struct node*)malloc(sizeof(struct node*));
p->next=temp;
printf(“\n Enter the element);
temp->prev=p;
scanf(“%d”,&x);
start=p;
p->next=null;
printf(“\n Element inserted
p->prev=null;
at begining position”);
if(start==null)
} }
start=p;
Inserting a Node at the End end of a Doubly Linked List

Consider the doubly linked list shown in fig. Suppose we want to add a new node with data 9
as the last node of the list. Then the following changes will be done in the linked l ist.
Insert node at last
void insertAtLast()
{int x; else
p=(struct node*)malloc(sizeof(struct node*)); {
while(temp->next != null)
printf(“\n Enter the element); {
scanf(“%d”,&x); temp=temp->next;
}
p->data=x; temp->next=p;
p->next=null; p->prev=temp;
temp=p;
p->prev=null; }
temp=start;
if(start==null) printf(“\n Element inserted at last position”);}

start=p;
Inserting a Node After a Given Node in a Doubly Linked List

we want to
add a new
node with
value 9 after
the node
containing 3
Insert node at after (at intermediate)
void insertAtintermediate()
{ int x,int pos; Scanf(“%d”,&pos)
p=(struct node*)malloc(sizeof(struct node*)); for(i=0;i<pos-1;i++)
printf(“\n Enter the element); {
scanf(“%d”,&x); q=q->next;

p->next=null; }

p->prev=null; temp=start;
p->next=q->next;
if(start==null)
q->next->prev=p;
start=p;
p->prev=q;
else
q->next=p;
{
printf(“\n Element inserted at intermediate position”);
printf(“\n After which element you want to insert”);
} }
Inserting a Node Before a Given Node in a Doubly Linked List

Suppose
we want to
add a new
node with
value 9
before the
node
containing
3.
Deleting a Node from a Doubly Linked List

In this section, we will see how a node is deleted from an already existing
doubly linked list. We will take four cases and then see how deletion is
done in each case.

Case 1: The first node is deleted.


Case 2: The last node is deleted.
Case 3: The node after a given node is deleted.
Case 4: The node before a given node is deleted.
Deleting the First Node from a Doubly Linked List
Consider the doubly linked list shown in Fig. When we want to delete a node from the
beginning of the list, then the following changes will be done in the linked list.
Deleting the Last Node from a Doubly Linked List
Consider the doubly linked list shown in Fig. Suppose we want to delete the last node
from the linked list, then the following changes will be done in the linked list.
Circular Linked List
● Circular Linked List is little more complicated linked data
structure.
● In the circular linked list we can insert elements anywhere in the
list whereas in the array we cannot insert element anywhere in
the list because it is in the contiguous memory.
● In the circular linked list the previous element stores the address
of the next element and the last element stores the address of the
starting element.
Circular Linked List
● The elements points to each other in a circular way which forms a
circular chain.
● The circular linked list has a dynamic size which means the memory
can be allocated when it is required.
Application of Circular Linked List

Multiplayer games
All the Players are kept in a Circular Linked List and
the pointer keeps on moving forward as a player's
chance ends.
Basic Operations on a Circular Linked
List

Insert – Inserts a new element at the end of the list.


Delete – Deletes any node from the list.
Find – Finds any node in the list.
Print – Prints the list.
Applications of Linked List
Polynomial Representation and Addition

A polynomial p(x) is the expression in variable x which is in the form (ax n + bxn-1
+ …. + jx+ k), where a, b, c …., k fall in the category of real numbers and 'n' is
non negative integer, which is called the degree of polynomial.
An essential characteristic of the polynomial is that each term in the polynomial
expression consists of two parts:
● one is the coefficient
● other is the exponent
Applications of Linked List
Polynomial Representation using linked list
Polynomial Addition Example

Input:-
1st number = 5x2+4x+2
2nd number = 5x+5

Output:-
5x2+9x+7

You might also like