The program will use separate functions to do the following tasks:
1. Add a node at the end of the Linked List.
2. Add a node at the start of the Linked List.
3. Delete a node at the end of the Linked List.
4. Delete a node at the start of the Linked List.
5. Display the values in the linked list.
6. Enter a node at any position.
7. Delete a node from any position of the linked list.
8. Search a value in the linklist and delete it
#include
#include
#include
typedef struct node
{
int d;
struct node *link;
}Node;
Node *head=NULL;
void append()
{
Node *p, *q;
int x;
p = (struct node *)malloc(sizeof(struct node));
p->link = NULL;
printf("\nEnter a value to add at the end:");
scanf("%d", &p->d);
if(head==NULL)
{
head=p;
return;
}
q = head;
while(q->link!=NULL)
{
q = q->link;
}
q->link = p;
}
void add_beg()
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
printf("\nEnter a value to add at the beginning: ");
scanf("%d", &p->d);
if(head==NULL)
{
p->link=NULL;
head = p;
return;
}
p->link=head;
head=p;
}
void del_beg()
{
Node *q;
if(head==NULL)
{
printf("\nThere is no node to delete.");
return;
}
printf("\nDeleted Node is: %d", head->d);
head = head->link;
free(q);
}
void del_end()
{
Node *q, *r;
if(head==NULL)
{
printf("\nCannot delete. Linked List is empty.");
return;
}
if(head->link==NULL)
{
printf("\nDeleted value: %d", head->d);
q = head;
head=NULL;
free(q);
return;
}
r = head;
q = head->link;
while(q->link!=NULL)
{
r = q;
q = q->link;
}
printf("\nDeleted value: %d", q->d);
r->link=NULL;
free(q);
}
void display()
{
Node *q;
q = head;
printf("\nThe values in the linked list are: \n");
while(q!=NULL)
{
printf("%d\n", q->d);
q = q->link;
}
}
int count()
{
Node *q=head;
int c=0;
while(q!=NULL)
{
c=c+1;
q=q->link;
}
return c;
}
void add_any_pos()
{
Node *q, *p;
int pos, i;
printf("\nEnter the position where you want to insert the node: ");
scanf("%d", &pos);
if(pos<1||pos>count()+1)
{
printf("\nInvalid position.");
return;
}
if(pos==1)
{
add_beg();
return;
}
if(pos==count()+1)
{
append();
return;
}
p = (Node *)malloc(sizeof(Node));
printf("Enter a value: ");
scanf("%d", &p->d);
q = head;
for(i=2;i<pos;i++) { q = q->link;
}
p->link=q->link;
q->link=p;
}
void del_any_pos()
{
Node *q, *r;
int pos, i;
if(head==NULL)
{
printf("\nThere is no node to delete.");
return;
}
printf("\nEnter the position of the node you want to delete: ");
scanf("%d", &pos);
if(pos<1||pos>count())
{
printf("\nInvalid position.");
return;
}
if(pos==1)
{
del_beg();
return;
}
if(pos==count())
{
del_end();
return;
}
r = head;
q = head->link;
for(i=2;i<pos;i++) { r = q; q = q->link;
}
printf("\nDeleted Node: %d", q->d);
r->link=q->link;
free(q);
}
void searchNdel()
{
Node *q, *r;
int pos=0, val, i=0;
if(head==NULL)
{
printf("\nThere is no node to delete.");
return;
}
printf("\nEnter the value you want to delete: ");
scanf("%d", &val);
q=head;
while(q!=NULL)
{
i++;
if(q->d==val)
{
pos=i;
break;
}
q = q->link;
}
if(pos<1) { printf("\nInvalid Value."); return; } if(pos==1) { del_beg(); return; } if(pos==count()) { del_end(); return; } r = head; q = head->link;
for(i=2;i<pos;i++) { r = q; q = q->link;
}
printf("\nDeleted Node: %d", q->d);
r->link=q->link;
free(q);
}
void main()
{
int ch;
clrscr();
do
{
printf("\nMENU");
printf("\n1. Add a node at the end of the Linked List.");
printf("\n2. Add a node at the start of the Linked List.");
printf("\n3. Delete a node at the end of the Linked List.");
printf("\n4. Delete a node at the start of the Linked List.");
printf("\n5. Display the values in the linked list.");
printf("\n6. Enter a node at any position.");
printf("\n7. Delete a node from any position of the linked list.");
printf("\n8. Search a value in the linklist and delete it.");
printf("\n0. Terminate the program.");
printf("\nEnter your choice:");
ch = getche()-48;
switch(ch)
{
case 1:
append();
break;
case 2:
add_beg();
break;
case 3:
del_end();
break;
case 4:
del_beg();
break;
case 5:
display();
break;
case 0:
printf("\nThank you for using the program.");
break;
case 6:
add_any_pos();
break;
case 7:
del_any_pos();
break;
case 8:
searchNdel();
break;
default:
printf("\nInvalid Input Enter Again.");
}//End of Switch-Case
getch();
}while(ch!=0);
getch();
}