unit-iv_srb
unit-iv_srb
Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting
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;
IF(HEAD==NULL)
PRINTF(“List is empty”);
printf("\n which no u want to search");
scanf("%d",&serno);
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; // //10->20->30
printf("\n which no u want to update");
scanf("%d",&serno); //20
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 It is updated successfully");
}
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 p
q q
r NULL
NULL
} //while end
head=old;
display();
COMPLEXITY OF VARIOUS OPERATIONS
IN ARRAYS AND SLL
A B C
NULL 11 786 200 656 400 786 777 NULL
Advantages: Disadvantages:
Can be traversed in either Requires more space
direction (may be List manipulations are
essential for some slower (because more
programs) links must be changed)
Some operations, such as Greater chance of having
deletion and inserting bugs (because more links
before a node, become must be manipulated)
easier
Structure of DLL
struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};
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
Adjusting the next and previous pointers of the nodes b/w which
the new node accordingly
void insert_after()
{
struct node *newnode=createnode();
struct node *temp,*temp1;
int serno;
printf(" after which node u want insert");
scanf("%d",&serno);
temp=head;
while(temp->data!=serno)
{
temp=temp->next;
}
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)
6 2 3 8 -3 18 0 0 23
0 2 0 2 4
Index
represents
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:
P1 23 9 18 7 41 6 18 7 3 0
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 3 x 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