DSA DAY 2 - Linked Lists
DSA DAY 2 - Linked Lists
Twitter: @NaikVritika
DSA DAY 2
LinkedIn: Vritika Naik LINKED LISTS
• Single Linked List
• Doubly Linked List
Topics to be • Circular Linked List
covered • Linked list with header node
• Operations on a Linked List
WHAT IS A Like arrays, Linked List is a linear data structure. Unlike arrays, linked list
elements are not stored at a contiguous location; the elements are linked using
LINKED LIST? pointers.
Arrays can be used to store linear data of similar types, but arrays have the
following limitations.
WHY LINKED 1) The size of the arrays is fixed. Linked Lists have dynamic size.
LIST? 2) Inserting a new element in an array of elements is expensive because the
room has to be created for the new elements and to create room existing
elements have to be shifted.
TYPES OF LINKED LISTS
• First is info part that contains the actual data of the list and the second one is the
link part that points to the next node of the list (address of the next node).
SINGLE LINKED • Beginning is marked with a special pointer called head or start which points to
LIST the first node in the list.
• Link of last node is NULL.
Syntax:
struct Node {
type member1;
….
AND };
EXAMPLE Example:
struct Node {
int data;
struct Node* next;
};
TRAVERSA
L
void display(struct node *start)
{
struct node *p;
if(start==NULL)
{
printf("List is empty\n");
return;
DISPLAY }
p=start;
printf("List is :\n");
while(p!=NULL)
{
printf("%d ",p->info);
p=p->link;
}
printf("\n\n");
}/*End of display() */
void count(struct node *start)
{
struct node *p;
int cnt=0;
p=start;
COUNT while(p!=NULL)
{
p=p->link;
cnt++;
}
printf("Number of elements are %d\n",cnt);
}/*End of count() */
void search(struct node *start,int item)
{
struct node *p=start;
int pos=1;
while(p!=NULL)
{
SEARCH if(p->info==item)
{
printf("Item %d found at position %d\n",item,pos);
return;
}
p=p->link;
pos++;
}
printf("Item %d not found in list\n",item);
}/*End of search()*/
ADD AT
BEGINNIN
G
struct node *addatbeg(struct node *start,int data)
ADD AT {
ADD AFTER
{
if(p->info==item)
{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=p->link;
p->link=tmp;
return start;
}
p=p->link;
}
printf("%d not present in the list\n",item);
return start;
}/*End of addafter()*/
struct node *addbefore(struct node *start,int data,int item)
{
struct node *tmp,*p;
if(start==NULL )
ADD {
printf("List is empty\n");
BEFORE }
return start;
BEFORE {
tmp=(struct node *)malloc(sizeof(struct node));
(continued…) tmp->info=data;
tmp->link=p->link;
p->link=tmp;
return start;
}
p=p->link;
}
printf("%d not present in the list\n",item);
return start;
}/*End of addbefore()*/
struct node *addatpos(struct node *start,int data,int pos)
{ struct node *tmp,*p;
int i;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
ADD AT
if(pos==1)
{ tmp->link=start;
start=tmp;
(continued…) while(p->link!=NULL)
{
if(p->link->info==data)
{
tmp=p->link;
p->link=tmp->link;
free(tmp);
return start;
}
p=p->link;
}
printf("Element %d not found\n",data);
return start;
}/*End of del()*/
REVERSAL
OF LINKED
LIST
struct node *reverse(struct node *start)
{
struct node *prev, *ptr, *next;
prev=NULL;
ptr=start;
while(ptr!=NULL)
{
REVERSE next=ptr->link;
ptr->link=prev;
prev=ptr;
ptr=next;
}
start=prev;
return start;
}/*End of reverse()*/
DOUBLY LINKED LIST
Nodes have 2 links, one for previous and one for the next node.
NODE IN DOUBLY LINKED LIST
struct node
{
struct node *prev;
int info;
struct node *next;’
}
CIRCULAR Last node’s link is connected to first node
LINKED LIST
HEADER-LINKED
LIST
• A header node - special node found at the
beginning of the list
• This type of list is useful when information
other than that found in each node is
needed.
SORTED LINKED LIST
SORTING A LINKED LIST
1) By exchanging the data
2) By rearranging the links
POLYNOMIAL
ARITHMETIC WITH
LINKED LIST
struct node{
float coefficient;
int exponent;
struct node *link;
}
Dynamically allocated memory –
Helps prevent wastage of space.
ADVANTAGE
S OF LINKED Easier insertion and deletion
LIST
DISADVANTAGE
S OF LINKED Extra space required for pointer.
LIST
Complexity of program
increases.
Thank You!
VRITIKA NAIK
Twitter: @NaikVritika
LinkedIn: Vritika Naik