#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct
vehicle
{
int
veh_no;
vehicle
*next;
}*end;
void
main()
{
void
add_node();
void
add_beg();
void
add_after();
void
traverse();
void
del();
int
ch;
end=NULL;
while(ch!=6)
{
printf("\n1.
Add Node");
printf("\n2.
Add a node at the begining");
printf("\n3.
Add after a node");
printf("\n4.
Display all nodes");
printf("\n5.
Delete a node");
printf("\n6.
Quit");
printf("\nSelect
your choice : ");
scanf("%d",&ch);
switch(ch)
{
case
1: add_node();
break;
case
2: add_beg();
break;
case
3: add_after();
break;
case
4: traverse();
break;
case
5: if(end == NULL)
{
printf("The
vehicle list is empty");
}
else
del();
break;
case
6:
exit(0);
default:
printf("Wrong
choice\n");
}
}
}
void
add_node()
{
vehicle
*q,*ptr;
int
num;
ptr=
(vehicle *)malloc(sizeof(vehicle));
printf("Enter
the vehicle no : ");
scanf("%d",&num);
ptr->veh_no
= num;
if(end
== NULL)
{
end
= ptr;
ptr->next
= end;
}
else
{
ptr->next
= end->next; end->next = ptr;
end
= ptr;
}
}
void
add_beg()
{
vehicle
*ptr;
int
num;
printf("Enter
the vehicle no : ");
scanf("%d",&num);
ptr
=(vehicle *) malloc(sizeof(struct vehicle));
ptr->veh_no
= num;
ptr->next
= end->next;
end->next
= ptr;
}
void
add_after()
{
vehicle
*ptr,*q;
int
i,num,idx;
printf("Enter
the vehicle no : ");
scanf("%d",&num);
printf("Enter
the index no. after which you want to add the vehicle : ");
scanf("%d",&idx);
q
= end->next;
for(i=0;
i < idx-1; i++)
{
q
= q->next;
if(q
== end->next)
{
printf("There
are less than %d elements\n",idx);
return;
}
}
ptr
= (vehicle *)malloc(sizeof(vehicle) );
ptr->next
= q->next;
ptr->veh_no
= num;
q->next
= ptr;
if(q==end)
end=ptr;
}
void
del()
{
int
num;
vehicle
*ptr,*q;
printf("Enter
the vehicle number that you want to delete : ");
scanf("%d",&num);
if(
end->next == end && end->veh_no == num)
{
ptr
= end;
end
= NULL;
free(ptr);
return;
}
q
= end->next;
if(q->veh_no
== num)
{
ptr
= q;
end->next
= q->next;
free(ptr);
return;
}
while(q->next
!= end)
{
if(q->next->veh_no
== num)
{
ptr
= q->next;
q->next
= ptr->next;
free(ptr);
printf("%d
Vehicle Record deleted\n",num);
return;
}
q
= q->next;
}
if(q->next->veh_no
== num)
{
ptr
= q->next;
q->next
= end->next;
free(ptr);
end
= q;
return;
}
printf("\nVehicle
no %d does not exists",num);
}
void
traverse()
{
vehicle
*q;
if(end
== NULL)
{
printf("No
Vehicles Found");
return;
}
q
= end->next;
printf("List
is :\n");
while(q
!= end)
{
printf("%d
", q->veh_no);
q
= q->next;
}
printf("%d\n",end->veh_no);
}
output:
1.
Add Node
2.
Add a node at the begining
3.
Add after a node
4.
Display all nodes
5.
Delete a node
6.
Quit
Select
your choice : 1
Enter
the vehicle no : 456
1.
Add Node
2.
Add a node at the begining
3.
Add after a node
4.
Display all nodes
5.
Delete a node
6.
Quit
Select
your choice : 2
Enter
the vehicle no : 678
1.
Add Node
2.
Add a node at the begining
3.
Add after a node
4.
Display all nodes
5.
Delete a node
6.
Quit
Select
your choice : 3
Enter
the vehicle no : 89
Enter
the index no. after which you want to add the vehicle : 89
There
are less than 89 elements
1.
Add Node
2.
Add a node at the begining
3.
Add after a node
4.
Display all nodes
5.
Delete a node
6.
Quit
Select
your choice : 4
List
is :
678
456
1.
Add Node
2.
Add a node at the begining
3.
Add after a node
4.
Display all nodes
5.
Delete a node
6.
Quit
Select
your choice : 5
Enter
the vehicle number that you want to delete : 678
1.
Add Node
2.
Add a node at the begining
3.
Add after a node
4.
Display all nodes
5.
Delete a node
6.
Quit
Select
your choice : 4
List
is :
456
1.
Add Node
2.
Add a node at the begining
3.
Add after a node
4.
Display all nodes
5.
Delete a node
6.
Quit
Select
your choice :
No comments:
Post a Comment