Finallinked Listppt
Finallinked Listppt
Insertions and Deletions are inefficient: Elements Insertions and Deletions are efficient: No shifting
are usually shifted
No memory waste if the array is full or almost Since memory is allocated dynamically(acc. to
full; otherwise may result in much memory our need) there is no waste of memory.
waste.
Sequential access is faster [Reason: Elements in Sequential access is slow [Reason: Elements not
contiguous memory locations] in contiguous memory locations]
Types of lists
There are two basic types of linked list
a b c d
• Creating a List
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
• Reversing a list
Creating a node
struct node
{ // A simple node of a linked list
int data;
struct node *next; head points at the first
node
}*head;
struct node *createnode()
{
struct node *newnode =(struct
node*)malloc (sizeof(struct node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=NULL;
return(newnode);
}
void accept()
{
int n;
int i;
struct node *temp;
head=NULL;
printf("how many node u want to enter");
scanf("%d",&n);
for(i=0;i<n;i++)
{
struct node *newnode = createnode();
if(head==NULL)
{
head=newnode;
}
else
{ temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
Display()
void display()
{
struct node *temp=head;
while(temp!=NULL)
{
printf("|%d\t|->",temp->data);
temp=temp->next;
}
printf("NULL");
Searching a SLL
Searching involves finding the required
element in the list
We can use various techniques of searching
like linear search or binary search where
binary search is more efficient in case of
Arrays
But in case of linked list since random access
is not available it would become complex to do
binary search in it
We can perform simple linear search traversal
Search()
void search()
{
struct node *temp;
int serno; int flag; flag=0;
temp=head;
while(temp!=NULL)
{
if(serno==temp->data)
{
flag=1;
break;
}
temp=temp->next;
}
if(flag==0)
{
printf("number not found");
}
else
{
printf("number found");
}
display();
}
Inserting the node in a SLL
}
newnode->next=temp->next;
temp->next=newnode;
display();
}
Insert end
void insert_end()
{
struct node *newnode=createnode();
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
display();
}
Inserting at the end
Here we simply need to make the next
pointer
of the last node point to the new node
void insert_end(node* p)
{
node *q=start;
if(start==NULL)
{
start=p;
cout<<”\nNode inserted successfully at the
end…!!!\n”;
}
else{
while(q->link!=NULL)
q=q->link;
q->next=p;
}
}
Deleting a node in SLL
Here also we have three cases:-
start
start
} //while end
old->next=NULL;
free(temp);
display();
}
Deleting a particular node
Here we make the next pointer of the node
previous to the node being deleted ,point to the
successor node of the node to be deleted and
then delete the node using delete keyword
To be deleted
void delete_after()
{
struct node *temp; struct node *temp1; struct node
*temp2;
int serno;
printf(" after which node u want delete");
scanf("%d",&serno);
temp=head;
while(temp->data!=serno)
{
temp=temp->next;
}
temp1=temp->next;
temp2=temp1->next;
temp->next=temp2;
temp1->next=NULL;
free(temp1);
display();
}
void update()
{
//printf("\n update function");
int serno;
int flag;
struct node *temp;
flag=0;
temp=head;
printf("\n which no u want to update");
scanf("%d",&serno);
while(temp!=NULL) //while start
{
if(serno==temp->data) //if start
{ printf("enter new data");
scanf("%d",&temp->data);
flag=1;
break;
} //if end
temp=temp->next;
} //while end
if(flag==0)
{
printf("\n number not found");
}
else
{
printf("number is found");
}
display();
}
void sort()
{
for(temp=head;temp!=NULL;temp=temp->next)
{
for(temp1=temp->next;temp1!=NULL;temp1=temp1-
>next)
{
if(temp->data>temp1->data)
{
temp2->data=temp->data;
temp->data=temp1->data;
temp1->data=temp2->data;
}
}
}
display();
}
void concatination()
{
struct node *temp,*temp1,*head1,*head2,*temp2;
// temp=head;
head1=head;
accept();
head2=head;
temp=head1;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=head2;
head=head1;
// display();
}
Reversing a linked list
• We can reverse a linked list by reversing
the direction of the links between 2 nodes
We make use of 3 structure pointers say p,q,r
Head P q
p rq NULL
NULL
} //while end
head=old;
COMPLEXITY OF VARIOUS
OPERATIONS IN ARRAYS AND
SLL
Operation ID-Array Complexity Singly-linked list Complexity
Insert at beginning O(n) O(1)
Insert at end O(1) O(1) if the list has tail reference
O(n) if the list has no tail reference
A B
C 11 786 200 656 400 777
NULL 786 NULL
200 786 400
Advantages: Disadvantages:
Can be traversed in Requires more space
either direction (may List manipulations
be essential for some are slower (because
programs) more links must be
Some operations, changed)
such as deletion and Greater chance of
inserting before a having bugs (because
node, become easier more links must be
manipulated)
Structure of DLL
struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};
}*head;
struct node *createnode()
{
struct node *newnode =(struct node*)malloc
(sizeof(struct node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
return(newnode);
}
void display()
{
struct node *temp=head;
while(temp!=NULL)
{
printf("|%d\t|-<-->",temp->data);
temp=temp->next;
}
printf("NULL");
}
void search()
{
int flag;
flag=0;
temp=head;
printf("\n search function");
while(temp!=NULL)
{
if(serno==temp->data)
{
flag=1;
break;
}
temp=temp->next;
}
if(flag==0)
{
printf("number not found");
}
else
{
printf("number found");
}
display();
}
Inserting at beginning
void insert_begning()
{
struct node *newnode=createnode();
newnode->next=head;
head->prev=newnode;
head=newnode;
display();
}
Inserting at the end
void insert_end()
{
struct node *newnode=createnode();
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
newnode->next=NULL;
display();
}
Inserting after a node
}
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
newnode->prev=temp;
display();
}
Deleting a node
• Node deletion from a DLL involves changing two
links
• In this example,we will delete node b
myDLL
a b c
} //while end
old->next=NULL;
temp->prev=NULL;
free(temp);
display();
}
void delete_after()
{
// struct node *newnode=createnode();
struct node *temp;
struct node *temp1;
struct node *temp2;
int serno;
printf(" after which node u want delete");
scanf("%d",&serno);
temp=head;
while(temp->data!=serno)
{
temp=temp->next;
}
temp1=temp->next;
temp2=temp1->next;
temp->next=temp2;
temp2->prev=temp->prev;
temp1->next=NULL;
free(temp1);
display();
}
void reverse()
{
struct node *temp,*old,*oldold;
temp=head;
temp=temp->next;
}
while(temp!=NULL)
{
printf("|%d\t|-<-->",temp->data);
temp=temp->prev;
} printf("NULL");
}
APPLICATIONS OF LINKED LIST
1. Applications that have an Most Recently Used(a
linked list of file names)
Index
represent
s
exponents
•This is why arrays aren’t good to
represent polynomials:
6 2 0 0 -3 0 ………… 0 16
WASTE OF SPACE!
• Advantages of using an Array:
P2 4 6 10 4 12 1 8 0
Representation
struct polynode {
int coef;
int exp;
struct polynode * next;
};
typedef struct polynode *polyptr;
a
3 14 2 8 1 0 null
b 8 x 14 3x 10 10 x 6
b
8 14 -3 10 10 6 null
Adding Polynomials
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 a->expon == b->expon
d
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 a->expon < b->expon
3 14 2 8 1 0
a
8 14 -3 10 10 6
b
11 14 -3 10 2 8
d
a->expon > b->expon
THANK YOU