Q.1. Implementation of Singly Linked List
Q.1. Implementation of Singly Linked List
- 0801CA181026
Name- Shivani Alya
#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
}
#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
{
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;
}