Linked List
Linked List
Linked List
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:-
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
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.
struct node
{
int data;
prev next
struct node* next;
struct node* prev;
} *start=null;
Create DLL
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.
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
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