Professional Documents
Culture Documents
the inserted values. Also display the values after deleting a value
from the list.
Answer::
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct node
{
int item;
struct node *next;
};
static int i = 0;
struct node *head = NULL;
void create(struct node *);
void display(struct node *);
void insert_after(int,int);
void delete_val(int val);
/* Create node */
void create(struct node *p)
{
printf("\n Input The %d Node (Enter 0 To Terminate) ",i+1);
scanf("%d",&p->item);
if (p->item = = 0)
p->next = NULL;
else
{
i = i+1;
p->next = (struct node *)malloc(sizeof(struct node));
p = p->next;
create(p);
}
}
/* Display Node */
void display(struct node *p)
{
printf("Nodes are :\n ");
while(p->next != NULL)
1
{
printf(" %d ",p->item);
p = p->next;
}
}
/* Insert After */
void insert_after(int data,int val)
{
struct node *p,*newnode;
p = head;
while((p->item != val) && (p->next != NULL))
{
p = p->next;
}
if (p->next = = NULL)
printf("\n Node Not Found");
else
{
newnode = (struct node*)malloc(sizeof(struct node));
newnode->item = data;
newnode->next = p->next;
p->next = newnode;
}
}
2
free(p);
}
}
}
int main()
{
int choice = 0,val,data;
struct node *node = (struct node*)malloc(sizeof (struct node));
head = node;
while (choice != 5)
{
printf("\n Menu ");
printf("\n 1. Create List ");
printf("\n 2. Display List ");
printf("\n 3. Insert After An Existing Value ");
printf("\n 4. Delete A Particular Value ");
printf("\n 5. Exit \n");
printf("\nEnter your choice (1 to 5): ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
create(head);
i = 0;
break;
}
case 2:
{
display(head);
break;
}
case 3:
{
printf("\n Original ");
display(head);
printf("\n Enter The Value After Which U Want To Insert: ");
scanf("%d", &val);
printf("\n Enter The Value U Want To Insert: ");
scanf("%d", &data);
insert_after(data,val);
printf("\n");
display(head);
break;
}
case 4:
{
3
printf("\n Original ");
display(head);
printf("\n Enter The Value U Want To Delete: ");
scanf("%d", &data);
delete_val(data);
printf("\n");
display(head);
break;
}
case 5:
break;
default :
printf("\n End");
}
}
return 0;
}
/* End Of Program */
Output ::
Menu
1. Create List
2. Display List
3. Insert After An Existing Value
4. Delete A Particular Value
5. Exit
4
Menu
1. Create List
2. Display List
3. Insert After An Existing Value
4. Delete A Particular Value
5. Exit
Menu
1. Create List
2. Display List
3. Insert After An Existing Value
4. Delete A Particular Value
5. Exit
Nodes are :
10 20 25 30
Menu
1. Create List
2. Display List
3. Insert After An Existing Value
4. Delete A Particular Value
5. Exit
Nodes are :
10 25 30
5
Menu
1. Create List
2. Display List
3. Insert After An Existing Value
4. Delete A Particular Value
5. Exit