Sie sind auf Seite 1von 17

Roll no.

- 0801CA181026
Name- Shivani Alya

Q.1. Implementation of Singly Linked List

#include<stdio.h>
#include<stdlib.h>
void
insert_beg(),insert_end(),insert_spec(),display(),delete_beg(),delete_end(),delete_spec(),search_num(
int),search_pos(int);
int count();
struct Node
{
int data;
struct Node *address;
};
struct Node *temp,*p,*q;
struct Node *start=NULL;
struct Node *newnode()
{
p=(struct Node *)malloc(sizeof(struct Node));
printf("\n enter number to insert \n ");
scanf("%d",&p->data);
p->address=NULL;
return(p);
}
void main()
{
int ch,ch1,num,pos;
char choice;
while(1)
{
printf("\n enter your choice \n");
printf("\n enter do you want to insert or delete the node \n");
printf("\n 1 for insert \n 2 for delete \n 3 for search by number \n 4 for search by
position \n 5 for count \n 0 for exit \n ");
scanf("%d",&ch1);
if(ch1==1)
{
printf("\n 1 for insert node at beginning \n 2 for insert node at the end of list \n
3 for insert node at the specified position \n 0 for exit \n ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_beg();
display();
break;
case 2:
insert_end();
display();
break;
case 3:
Roll no.- 0801CA181026
Name- Shivani Alya

insert_spec();
display();
break;
case 0:
exit(1);
default:
printf("\n you entered a wrong choice");
}
}
if(ch1==2)
{
printf("\n 1 for delete node at beginning \n 2 for delete node at the end of list \n
3 for delete node at the specified position \n 0 for exit \n ");
scanf("%d",&ch);
switch(ch)
{
case 1:
delete_beg();
display();
break;
case 2:
delete_end();
display();
break;
case 3:
delete_spec();
display();
break;
case 0:
exit(1);
default:
printf("\n you entered a wrong choice");
}
}
if(ch1==3)
{
printf("\n enter number to search \n");
scanf("%d",&num);
search_num(num);
}
if(ch1==4)
{
printf("\n enter position to search which number present on that\n");
scanf("%d",&pos);
search_pos(pos);
}
if(ch1==5)
{
printf("\n Number of nodes in linked list are- %d",count());
}
Roll no.- 0801CA181026
Name- Shivani Alya

if(ch1==0)
exit(1);
printf("\n do you want to continue ");
scanf(" %c",&choice);
if(choice=='Y' || choice=='y')
continue;
else
exit(1);
}
}
void insert_beg()
{
temp=newnode();
if(start==NULL)
start=temp;
else
{
temp->address=start;
start=temp;
}
}
void insert_end()
{
temp=newnode();
p=start;
if(start==NULL)
start=temp;
else
{
p=start;
while(p->address!=NULL)
{
p=p->address;
}
p->address=temp;
}
}
void insert_spec()
{
int pos,c,c1=0;
temp=newnode();
c=count();
printf("\n insert position to which you have insert a node \n ");
scanf("%d",&pos);
p=start;
if(pos>c || pos<0)
printf("\n not a valid position");
else
{
while(p!=NULL)
Roll no.- 0801CA181026
Name- Shivani Alya

{
if(c1==pos)
{
temp->address=p->address;
p->address=temp;
printf("\n node is inserted");
break;
}
else
{
c1++;
p=p->address;
}
}
}
}
void delete_beg()
{
p=start;
if(start==NULL)
printf("\n linked list is empty");
else
{
start=p->address;
p->address=NULL;
free(p);
printf("\n node is deleted");
}
}
void delete_end()
{
p=start;
if(start==NULL)
printf("\n linked list is empty");
if(p->address==NULL)
{
start=p->address;
free(p);
}
else
{
while(p->address!=NULL)
{
q=p;
p=p->address;
}
q->address=NULL;
free(p);
printf("\n node is deleted");
}
Roll no.- 0801CA181026
Name- Shivani Alya

}
void delete_spec()
{
int pos,c,c1=0;
c=count();
printf("\n insert position to which you have delete a node \n ");
scanf("%d",&pos);
p=start;
if(pos>c || pos<0)
printf("\n not a valid position");
else
{
if(start==NULL)
printf("\n linked list is empty");
if(p->address==NULL)
{
start=p->address;
free(p);
}
else
{
while(p!=NULL)
{
if(c1==pos)
{
q->address=p->address;
p->address=NULL;
free(p);
printf("\n node is deleted");
break;
}
else
{
c1++;
q=p;
p=p->address;
}
}
}
}
}
void display()
{
p=start;
if(start==NULL)
printf("\n linked list is empty");
else
{
printf("\n linked list is--------\n ");
while(p!=NULL)
Roll no.- 0801CA181026
Name- Shivani Alya

{
printf("%d \t----->\t",p->data);
p=p->address;
}
}
}
void search_num(int n)
{
int loc=0,flag=0;
p=start;
if(start==NULL)
printf("\n linked list is empty");
else
{
while(p!=NULL)
{
if(p->data==n)
{
flag=1;
printf("\n number found at %d location",loc+1);
}
p=p->address;
loc++;
}
if(flag==0)
printf("\n number not found");
}
}
void search_pos(int n)
{
int loc=0,c;
c=count();
p=start;
if(n>c || n<0)
printf("\n not a valid position");
else
{
if(start==NULL)
printf("\n linked list is empty");
else
{

while(p!=NULL)
{
if(loc==n)
{
printf("\n number found at %d location is %d",loc,p->data);
break;
}
else
Roll no.- 0801CA181026
Name- Shivani Alya

{
p=p->address;
loc++;
printf("\n hello");
}
}
}
}
}
int count()
{
int c=0;
p=start;
while(p!=NULL)
{
c++;
p=p->address;
}
return c
}

Complexity of each operations are as follows-


a) Insertion at beginning- O(1) because insertion of element doesn’t depend on number of
elements that are already in connected to linked list. So it takes constant cycle to execute.
b) Insertion at end- O(n) because it depends on number of elements already are connected to
linked list, first traverse all elements then insert at the last.
c) Insertion at specific position- O(n) because it depends on number of elements already are
connected to linked list, first traverse all elements to that particular position then insert at that
position.
d) Deletion from beginning- O(1) because deletion of element doesn’t depend on number of
elements that are already connected to linked list. So it takes constant cycle to execute.
e) Deletion from end- O(n) because it depends on number of elements already are connected to
linked list, first traverse all elements then delete from the last.
f) Deletion at specific position- O(n) because it depends on number of elements already are
connected to linked list, first traverse all elements to that particular position then delete from
that position.
g) Display- O(n) because it depends on number of elements are connected to linked list.
h) Search by position- O(n) because in worst case the position can be the last one.
i) Search by number- O(n) because in worst case the number can be found at the last position.
j) Count- O(n) because it depends on number of elements connected to linked list.
Roll no.- 0801CA181026
Name- Shivani Alya

Q.2(i). Implementation of Doubly Linked List

#include<stdio.h>
#include<stdlib.h>
void
insert_beg(),insert_end(),insert_spec(),display(),delete_beg(),delete_end(),delete_spec(),search_num(
int),search_pos(int);
int count();
struct Node
{
struct Node *prev;
int data;
struct Node *next;
};
struct Node *temp,*p,*q;
struct Node *start=NULL;
struct Node *newnode()
{
p=(struct Node *)malloc(sizeof(struct Node));
printf("\n enter number to insert \n ");
scanf("%d",&p->data);
p->prev=NULL;
p->next=NULL;
return(p);
}
void main()
{
int ch,ch1,num,pos;
char choice;
while(1)
{
printf("\n enter your choice \n");
printf("\n enter do you want to insert or delete the node \n");
printf("\n 1 for insert \n 2 for delete \n 3 for search by number \n 4 for search by
position \n 5 for count \n 0 for exit \n ");
scanf("%d",&ch1);
if(ch1==1)
{
printf("\n 1 for insert node at beginning \n 2 for insert node at the end of list \n
3 for insert node at the specified position \n 0 for exit \n ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_beg();
display();
break;
case 2:
insert_end();
display();
Roll no.- 0801CA181026
Name- Shivani Alya

break;
case 3:
insert_spec();
display();
break;
case 0:
exit(1);
default:
printf("\n you entered a wrong choice");
}
}
if(ch1==2)
{
printf("\n 1 for delete node at beginning \n 2 for delete node at the end of list \n
3 for delete node at the specified position \n 0 for exit \n ");
scanf("%d",&ch);
switch(ch)
{
case 1:
delete_beg();
display();
break;
case 2:
delete_end();
display();
break;
case 3:
delete_spec();
display();
break;
case 0:
exit(1);
default:
printf("\n you entered a wrong choice");
}
}
if(ch1==3)
{
printf("\n enter number to search ");
scanf("%d",&num);
search_num(num);
}
if(ch1==4)
{
printf("\n enter position to search which number present on that ");
scanf("%d",&pos);
search_pos(pos);
}
if(ch1==5)
{
Roll no.- 0801CA181026
Name- Shivani Alya

printf("\n Number of nodes in linked list are- %d",count());


}
if(ch1==0)
exit(1);
printf("\n do you want to continue ");
scanf(" %c",&choice);
if(choice=='Y' || choice=='y')
continue;
else
exit(1);
}
}
void insert_beg()
{
temp=newnode();
if(start==NULL)
start=temp;
else
{
temp->next=start;
start->prev=temp;
start=temp;
}
}
void insert_end()
{
temp=newnode();
p=start;
if(start==NULL)
start=temp;
else
{
p=start;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
temp->prev=p;
}
}
void insert_spec()
{
int pos,c,c1=0;
temp=newnode();
c=count();
printf("\n insert position to which you have insert a node \n ");
scanf("%d",&pos);
p=start;
if(pos>c || pos<0)
Roll no.- 0801CA181026
Name- Shivani Alya

printf("\n not a valid position");


else
{
while(p->next!=NULL)
{
if(c1==pos)
{
q->next=temp;
temp->prev=q;
p->prev=temp;
temp->next=p;
printf("\n node is inserted");
break;
}
else
{
c1++;
q=p;
p=p->next;
}
}
}
}
void delete_beg()
{
p=start;
if(start==NULL)
printf("\n linked list is empty");
else
{
start=p->next;
p->next->prev=start;
p->next=NULL;
free(p);
printf("\n node is deleted");
}
}
void delete_end()
{
p=start;
if(start==NULL)
printf("\n linked list is empty");
if(p->next==NULL)
{
start=p->next;
free(p);
}
else
{
while(p->next!=NULL)
Roll no.- 0801CA181026
Name- Shivani Alya

{
q=p;
p=p->next;
}
p->prev->next=NULL;
p->prev=NULL;
p->next=NULL;
free(p);
printf("\n node is deleted");
}
}
void delete_spec()
{
int pos,c,c1=0;
c=count();
printf("\n insert position to which you have delete a node \n ");
scanf("%d",&pos);
p=start;
if(pos>c || pos<0)
printf("\n not a valid position");
else
{
if(start==NULL)
printf("\n linked list is empty");
if(p->next==NULL)
{
start=p->next;
free(p);
}
else
{
while(p!=NULL)
{
if(c1==pos)
{
q->next=p->next;
p->next->prev=start;
p->next=NULL;
free(p);
printf("\n node is deleted");
break;
}
else
{
c1++;
q=p;
p=p->next;
}
}
}
Roll no.- 0801CA181026
Name- Shivani Alya

}
}
void display()
{
p=start;
if(start==NULL)
printf("\n linked list is empty");
else
{
printf("\n linked list is--------\n ");
while(p!=NULL)
{
printf("%d \t----->\t",p->data);
p=p->next;
}
}
}
void search_num(int n)
{
int loc=0,flag=0;
p=start;
if(start==NULL)
printf("\n linked list is empty");
else
{
while(p!=NULL)
{
if(p->data==n)
{
flag=1;
printf("\n number found at %d location",loc);
}
p=p->next;
loc++;
}
if(flag==0)
printf("\n number not found");
}
}
void search_pos(int n)
{
int loc=0,c;
c=count();
p=start;
if(n>c || n<0)
printf("\n not a valid position");
else
{
if(start==NULL)
printf("\n linked list is empty");
Roll no.- 0801CA181026
Name- Shivani Alya

else
{
while(p!=NULL)
{
if(loc==n)
{
printf("\n number found at %d location is %d",loc,p->data);
}
p=p->next;
loc++;
}
}
}
}
int count()
{
int c=0;
p=start;
while(p!=NULL)
{
c++;
p=p->next;
}
return c;
}

Complexity of each operations are as follows-


a) Insertion at beginning- O(1) because insertion of element doesn’t depend on number of
elements that are already in connected to linked list. So it takes constant cycle to execute.
b) Insertion at end- O(1) because insertion of element doesn’t depend on number of elements
that are already in connected to linked list because we use one extra pointer to traverse it in
reverse order. So it takes constant cycle to execute.
c) Insertion at specific position- O(n) because it depends on number of elements already are
connected to linked list, first traverse all elements to that particular position then insert at that
position.
d) Deletion from beginning- O(1) because deletion of element doesn’t depend on number of
elements that are already connected to linked list. So it takes constant cycle to execute.
e) Deletion from end- O(1) because deletion of element doesn’t depend on number of elements
that are already connected to linked list because we use one extra pointer to traverse it in reverse
order. So it takes constant cycle to execute.
f) Deletion at specific position- O(n) because it depends on number of elements already are
connected to linked list, first traverse all elements to that particular position then delete from
that position.
g) Display- O(n) because it depends on number of elements are connected to linked list.
h) Search by position- O(n) because in worst case the position can be the last one.
i) Search by number- O(n) because in worst case the number can be found at the last position.
j) Count- O(n) because it depends on number of elements connected to linked list.
Roll no.- 0801CA181026
Name- Shivani Alya

Q.2(ii). Implementation of Circular Linked List

Complexity of each operations are as follows-


a) Insertion at beginning- O(1) because insertion of element doesn’t depend on number of
elements that are already in connected to linked list. So it takes constant cycle to execute.
b) Insertion at end- O(n) because it depends on number of elements already are connected to
linked list, first traverse all elements then insert at the last.
c) Insertion at specific position- O(n) because it depends on number of elements already are
connected to linked list, first traverse all elements to that particular position then insert at that
position.
d) Deletion from beginning- O(1) because deletion of element doesn’t depend on number of
elements that are already connected to linked list. So it takes constant cycle to execute.
e) Deletion from end- O(n) because it depends on number of elements already are connected to
linked list, first traverse all elements then delete from the last.
f) Deletion at specific position- O(n) because it depends on number of elements already are
connected to linked list, first traverse all elements to that particular position then delete from
that position.
g) Display- O(n) because it depends on number of elements are connected to linked list.
h) Search by position- O(n) because in worst case the position can be the last one.
i) Search by number- O(n) because in worst case the number can be found at the last position.
j) Count- O(n) because it depends on number of elements connected to linked list.
Roll no.- 0801CA181026
Name- Shivani Alya

Q.3(i). Algorithm of implementation of Stack using single link list.


Ans- Push operation-
1. Initialize *start=NULL,*p, data,*next, num
2. Enter value (num)
p->data=num
p->next=NULL
3. If (start==NULL)
Start=p
4. Else
p->next=start
start=p
5. Exit
Pop operation-
1. p=start
2. If (start==NULL)
Linked list is empty
3. Else
Start=p->next
p->next=NULL
free(p)
4. Exit

Complexity of each operations are as follows-


a) Push operation- O(1) because insertion of element doesn’t depend on number of elements that are
already in connected to linked list. So it takes constant cycle to execute.
b) Pop operation- O(1) because deletion of element doesn’t depend on number of elements that are
already connected to linked list. So it takes constant cycle to execute.
Roll no.- 0801CA181026
Name- Shivani Alya

Q.3(ii). Algorithm of implementation of Queue using single link list.


Ans- Enqueue operation-
1. Initialize *start=NULL,*p, data,*next, num,*temp
2. Enter value (num)
p->data=num
p->next=NULL
3. If (start==NULL)
Start=p
4. Else
temp=start
temp=temp->next
5. Exit
Dequeue operation-
1. p=start
2. If (start==NULL)
Linked list is empty
3. Else
Start=p->next
p->next=NULL
free(p)
4. Exit

Complexity of each operations are as follows-


a) Enqueue operation- O(n) because it depends on number of elements already are connected to
linked list because insertion of node is done from the rear end, first traverse all elements then
insert at the last/rear.
b) Dequeue operation- O(1) because deletion of element doesn’t depend on number of elements
that are already connected to linked list because deletion of node is done from the front end. So
it takes constant cycle to execute.

Das könnte Ihnen auch gefallen