2. Linked list
• Each node in a linked list contains one or more members
that represent data.
• In addition to the data, each node contains a pointer,
which can point to another node.
Example
Data / Info link / next
3.
4. Advantages of Linked list over arrays:
• Efficient memory utilization
• Operations such as insertion and deletion are easy and
efficient.
• Size can be changed.
• Arbitrary memory locations
5. Disadvantages of linked lists:
• Each node requires an extra pointer in addition to
information, requiring more space.
• Insertion or deletion of a node takes a bit longer
because it involves more pointer operations.
• Linked lists do not allow random access.
• Traversing and changing of pointers consumes a lot of
time.
• Programming is typically trickier with pointers.
6. Memory Representation of Linear Linked List:
It needs two linear arrays for memory representation.
Let these linear arrays are INFO and LINK.
INFO[K] contains the information part and LINK[K]
contains the next pointer field of node K.
A variable START is used to store the location of the
beginning of the LIST
NULL is used as next pointer sentinel which indicates
the end of LIST.
7.
8.
9.
10.
11. TYPES OF LINKED LIST
Singly-linked list
• The simplest kind of linked list is a singly-linked
list which has one link per node. This link points to the
next node in the list, or to a null value or empty list if it
is the last node.
Doubly-linked list
• A more sophisticated kind of linked list is a doubly-
linked list. Each node has two links, one to the previous
node and one to the next node.
12. Doubly-linked list
• A more sophisticated kind of linked list is a doubly-
linked list. Each node has two links, one to the previous
node and one to the next node.
13. Info − 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.
Prev − Each link of a linked list contains a link to
the previous link called Prev.
Advantages
1) It can be traversed in forward or backward
direction.
2) Given a particular nodes address, one can have
both successor nodes and predecessor nodes
address that makes inserting and deleting much
easier
14. 3) It is used to represent the trees effectively.
Disadvantage
Extra memory is required to store the back pointer.
15. Circularly-linked list
• In a circularly-linked list, the first and last nodes
are linked together. This can be done for both
singly and doubly linked lists. To traverse a
circular linked list, you begin at any node and
follow the list in either direction until you return to
the original node. Viewed another way, circularly-
linked lists can be seen as having no beginning or
end.
16.
17. Singly –circularly linked list
• A singly linked circular list is a linked list where the last
node in the list points to the first node in the list. A
circular list does not contain NULL pointers
18. Doubly Linked List
In this type of liked list each node holds two-
pointer field. Pointers exist between adjacent
nodes in both directions. The list can be traversed
either forward or backward.
29. • Operations on Singly linked list:
– Traversing a linked list
– Creation of linked list
– Displaying elements of linked list
– counting number of nodes
– searching an element
– insertion of node
– deletion of node
30. void create()
{ char choice='y';
q=null; while(choice=='y')
{ p=((struct node*)malloc(sizeof(struct node)));
printf("nEnter the rollno & name :");
scanf("%d %s",&p->rollno,p->name);
p->prev=null;
p->next=null;
if(start==null) { start=p; q=p; }
else
{ q->next=p; p->prev=q; p->next=null; q=p; }
printf("nDo you want to create another node y/n :");
choice=getche();
} }
31. void display()
{
printf("n The nodes in the list arenSTART->");
p=start;
while(p!=null)
{
printf("%d %s ->",p->rollno,p->name);
p=p->next;
}
printf("NULL");
return;
}
34. void del(int regno)
{ p=start; prev=null;
if(start==null) {
printf("nLinked list is emptyn"); return; }
if(start->rollno==regno) {
start =start->link; free(p); return; }
while((p->rollno!=regno)&&(p!=null))
{ prev=p; p=p->link; }
if(p==null)
{ printf("n Reg number %d is not found in the LLn", regno); }
else { prev->link=p->link;
free(p);
}
}
35. void search(int regno)
{
int i=0; p=start;
while(p!=null)
{ if(regno==p->rollno)
{ i++;
printf("nRoll no %d is found at %d",p->rollno,i);
printf("n Name:%s",p->name);
return; }
else
{ p=p->link; i++; }
}
printf("nNode with register number %d does not exit",regno);
}
36. Stack Implementation using linked list:
/*Program to implement a Stack using Linked List */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define NULL 0
void push(int);
int pop();
void display();
struct node
{
int info;
struct node *link;
}*top,*p;
void main()
{
int ch,i,item,pos;
clrscr();
40. Circular Linked list
Circular Linked List is a variation of Linked list in which the
first element points to the last element and the last element
points to the first element. Both Singly Linked List and
Doubly Linked List can be made into a circular linked list.