100% found this document useful (1 vote)
2K views40 pages

DSA DAY 2 - Linked Lists

This ppt is made for the sole purpose of GirlScript Foundation Easy Grad Success DSA Week. It consists of the following topics: • Single Linked List • Doubly Linked List • Circular Linked List • Linked list with header node • Operations on a Linked List

Uploaded by

Vritika Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views40 pages

DSA DAY 2 - Linked Lists

This ppt is made for the sole purpose of GirlScript Foundation Easy Grad Success DSA Week. It consists of the following topics: • Single Linked List • Doubly Linked List • Circular Linked List • Linked list with header node • Operations on a Linked List

Uploaded by

Vritika Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

VRITIKA NAIK

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;
….

SYNTAX type member;


struct Node* next;

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 {

BEGINNING struct node *tmp;


tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}/*End of addatbeg()*/
ADD TO
EMPTY
LIST
struct node *addatbeg(struct node *start,int data)
{
ADD TO struct node *tmp;

EMPTY LIST tmp=(struct node *)malloc(sizeof(struct node));


tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}/*End of addatbeg()*/

//NOTE: here start=NULL


ADD AT
END
struct node *addatend(struct node *start,int data)
{
struct node *p,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
ADD AT END tmp->info=data;
p=start;
while(p->link!=NULL)
p=p->link;
p->link=tmp;
tmp->link=NULL;
return start;
}/*End of addatend()*/
ADD
BETWEEN
THE LIST
struct node *addafter(struct node *start,int data,int item)
{
struct node *tmp,*p;
p=start;
while(p!=NULL)

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;

/*If data to be inserted before first node*/


if(item==start->info)
{
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=start;
start=tmp;
return start;
}
p=start;
while(p->link!=NULL)
{
ADD if(p->link->info==item)

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;

POSITION return start;


}
p=start;
for(i=1; i<pos-1 && p!=NULL; i++)
p=p->link;
if(p==NULL)
printf("There are less than %d elements\n",pos);
else
{ tmp->link=p->link;
p->link=tmp;
}
return start;
}/*End of addatpos()*/
DELETION
OF FIRST
NODE
DELETION
OF ONLY
NODE
DELETION
OF IN-
BETWEEN
NODE
DELETION
OF LAST
NODE
struct node *del(struct node *start,int data)
{
struct node *tmp,*p;
if(start==NULL)
{

DELETION printf("List is empty\n");


return start;
}
/*Deletion of first node*/
if(start->info==data)
{
tmp=start;
start=start->link;
free(tmp);
return start;
}
/*Deletion in between or at the end*/
DELETION p=start;

(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

Not stored in contiguous


locations – no issue due to lack
of space.
Random access is difficult

DISADVANTAGE
S OF LINKED Extra space required for pointer.
LIST

Complexity of program
increases.
Thank You!
VRITIKA NAIK
Twitter: @NaikVritika
LinkedIn: Vritika Naik

You might also like