0% found this document useful (0 votes)
56 views41 pages

Linked List

A linked list is a linear data structure consisting of nodes that are connected by pointers. Each node contains data and a pointer to the next node. Linked lists allow for dynamic memory allocation and can be traversed in either direction. There are different types of linked lists including singly linked, doubly linked, circular linked lists. Common operations on linked lists include insertion and deletion at the beginning, end, or middle of the list.
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)
56 views41 pages

Linked List

A linked list is a linear data structure consisting of nodes that are connected by pointers. Each node contains data and a pointer to the next node. Linked lists allow for dynamic memory allocation and can be traversed in either direction. There are different types of linked lists including singly linked, doubly linked, circular linked lists. Common operations on linked lists include insertion and deletion at the beginning, end, or middle of the list.
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/ 41

LINKED LIST

(Basics & Singly Linked List)


What is Linked List
• A Linked List consists of set of nodes related to
each other through pointers.

• Each node in a Linked List consists of data and


address of next data.
data

Address of
Next Data
Define Linked list
• A linked list is a linear collection of data
elements whose order is not given by their
physical placement in memory.
• Instead, each element points to the next. It is
a data structure consisting of a collection of
nodes which together represent a sequence
Structure of linked list
• A linked list is a way to store a collection of
elements.
• Like an array these can be character or
integers.
• Each element in a linked list is stored in the
form of a node.
What is Node

• A node is a collection of two sub-elements or


parts.
• A data part that stores the element and
• a next part that stores the link to the next
node.
Linked List
• A linked list is formed when many such nodes
are linked together to form a chain.
• Each node points to the next node present in
the order.
• The first node is always used as a reference to
traverse the list and is called HEAD.
• The last node points to NULL.
Why Linked List?

• Dynamism
–Dynamic List
• Running Time Complexity
–Reducing Running Time
Array Vs Linked List
Data Structure

struct node
{
int data;
struct node *next;
};

Self referential structure!?


self-referential
• A structure having references to itself is called
a self-referential structure.
• Self Referential structures are those
structures that have one or more pointers
which point to the same type of structure, as
their member.
• In other words, structures pointing to the
same type of structures are self-referential in
nature.
Types of Linked List

•Linked List
•Singly
•Doubly

•Circular
•Circular Singly
•Circular Doubly
1. Singly Linked list
• It is the commonly used linked list in programs.
• If we are talking about the linked list, it means it is
a singly linked list.
• The singly linked list is a data structure that
contains two parts, i.e., one is the data part, and
the other one is the address part, which contains
the address of the next or the successor node.
• The address part in a node is also known as
a pointer.
• Suppose we have three nodes, and the
addresses of these three nodes are 100, 200
and 300 respectively.
• The representation of three nodes as a linked
list is shown in the below figure:
• The first node contains the address of the next
node, i.e., 200, the second node contains the
address of the last node, i.e., 300, and the
third node contains the NULL value in its
address part as it does not point to any node.
• The pointer that holds the address of the
initial node is known as a head pointer.
• Above diagram, is known as a singly linked list
as it contains only a single link. In this list, only
forward traversal is possible; we cannot
traverse in the backward direction as it has
only one link in the list.
Representation of the node in a singly
linked list
struct node
{
int data;
struct node *next;
}
2. Doubly linked list

• As the name suggests, the doubly linked list


contains two pointers.
• We can define the doubly linked list as a
linear data structure with three parts: the data
part and the other two address part.
• In other words, a doubly linked list is a list that
has three parts in a single node, includes one
data part, a pointer to its previous node, and a
pointer to the next node.
• Suppose we have three nodes, and the
address of these nodes are 100, 200 and 300,
respectively.
• The representation of these nodes in a
doubly-linked list is shown below:
• The node in a doubly-linked list has two
address parts; one part stores the address of
the next while the other part of the node
stores the previous node's address.
• The initial node in the doubly linked list has
the NULL value in the address part, which
provides the address of the previous node.
Representation of the node in a doubly linked
list

• In the above representation, we have defined a


user-defined structure named a node with three
members, one is data of integer type, and the other
two are the pointers, i.e., next and prev of the node
type.
• The next pointer variable holds the address of the
next node, and the prev pointer holds the address of
the previous node.
• The type of both the pointers, i.e., next and
prev is struct node as both the pointers are storing the
address of the node of the struct node type.
3. Circular linked list
• A circular linked list is a variation of a singly linked
list. The only difference between the singly linked
list and a circular linked list is that the last node
does not point to any node in a singly linked list,
so its link part contains a NULL value.
• On the other hand, the circular linked list is a list
in which the last node connects to the first node,
so the link part of the last node holds the first
node's address.
• The circular linked list has no starting and ending
node. We can traverse in any direction, i.e., either
backward or forward.
• Suppose we have three nodes, and the
address of these nodes are 100, 200 and 300,
respectively.
• The representation of these nodes in a
Circular-linked list is shown below:
Representation of the node in a
Circular linked list

• A circular linked list is a sequence of elements


in which each node has a link to the next
node, and the last node is having a link to the
first node.
Operations
•Operations
•Insert
•Begin
•End
•Middle

•Delete
•Begin
•End

•Middle

•Search
Operations
(Insertion at end)
Operations
(Insertion at Position)
Operations
(Deletion at Position)
Operations
(Insertion and Deletion)
Examples
Examples(Continued)
Create
struct node* createlist() if(r==NULL)
{ {
struct node *r,*n; r=n;
int i,k; current=n;
r=NULL; }
printf("Enter the size of list"); else
scanf("%d",&k); {
for(i=0;i<k;i++) current->next=n;
{ current=n;
n=(struct }
node*)malloc(sizeof(struct } // End of For Loop
node)); return r;
scanf("%d",&n->data); } // End of function
n->next=NULL;
Insert Begin
struct node *insert_begin(struct node *r,int x)
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
n->data=x;
n->next=NULL;
if(r==NULL)
{
r=n;
} Running Time!?
else
{
n->next=r;
r=n;
}
return r;
}
Insert End
struct node *insert_end(struct node *r,int x)
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node));
n->data=x;
n->next=NULL;
if(r==NULL)
{
r=n;
current=n;
Running Time!?
}
else
{
current->next=n;
current=n;
}
return r;
}
Insert Middle
struct node *insert(struct node *r,int else
element,int pos)
{
{
k=2;
Running Time!?
struct node *n,*p; for(p=r;p!=NULL;p=p->next)
int k=0; {
n=(struct node*)malloc(sizeof(struct if(k==pos)
node)); {
n->data=element; n->next=p->next;
n->next=NULL; p->next=n;
if(r==NULL) return r;
{ }
printf("Empty List"); k=k+1;
return NULL; }
} } printf("Position not available");
else if(pos==1) return r;
{ }
n->next=r;
r=n;
return r;
}
Display
void display(struct node *r)
{
while(r!=NULL)
{ Running Time!?
printf(" %d",r->data);
r=r->next;
}
printf(" null");
}
Delete
• Delete Begin
• Delete End
• Delete Middle
Delete Begin
struct node *delete_begin(struct node *r)
{
if(r==NULL)
printf(“No element to delete”);
else
return r->next;
}
Delete End
struct node *delete_end(struct node *r)
{
struct node *p;
if( r==NULL)
{
printf("No Element to Delete");
return NULL;
}
else
{
p=r;
while(p!=NULL)
{
if(p->next->next==NULL)
{
p->next=NULL;
current=p;
return r;
}
p=p->next;
}
}
}
Delete Middle
struct node *delete_middle(struct else
node *r, int element) {
{ p=r;
struct node *p; while(p!=NULL)
if(r==NULL) {
{ if(p->next->data==element)
printf("no element found"); {
return NULL; p->next=p->next->next;
} }
if(r->data==element) p=p->next;
{ }
return NULL; return r;
} }
}
Search
struct node *search(struct node *r,int element)
{
struct node *p;
p=r;
while(p!=NULL)
{
if(p->data==element)
return p;
p=p->next;
}
return NULL;
}
THANK YOU!!!

You might also like