0% found this document useful (0 votes)
37 views

Program Linked List Lab

The document provides steps to create a linked list program in C that allows the user to insert and delete nodes. It includes functions to create a list by adding nodes, display the list, insert a new node after a specified value, and delete a node with a given value. The main function runs a menu loop calling these functions as selected by the user to demonstrate creating a list, displaying it, inserting a new value, displaying the modified list, deleting a value, and displaying the list again.

Uploaded by

ank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Program Linked List Lab

The document provides steps to create a linked list program in C that allows the user to insert and delete nodes. It includes functions to create a list by adding nodes, display the list, insert a new node after a specified value, and delete a node with a given value. The main function runs a menu loop calling these functions as selected by the user to demonstrate creating a list, displaying it, inserting a new value, displaying the modified list, deleting a value, and displaying the list again.

Uploaded by

ank
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Question:: Write a program in C to create a linked list and display

the inserted values. Also display the values after deleting a value
from the list.

Answer::

Step-1 Open the compiler:- Double-click on the Turbo C++ icon on


the Desktop.

Step-2 C Program Code::


/* Program on Linked List */

#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;
}
}

/* Delete A Particular Value */


void delete_val(int val)
{
struct node *p,*q;
p = head;
while ((p->item != val) && (p->next != NULL))
{
q = p;
p = p->next;
}
if (p->next = = NULL)
printf("\n Item Not Found");
else
{
if (p= =head)
{
head = p->next ;
free(p);
}
else
{
q->next = p->next ;

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 */

Step-3 Save the C program file ::


Save As > LinkedList.C > Ok

Step-4 Compile the C program file ::


Compile > Compile

Step-5 Execute the C program file ::


Run > Run

Output ::

Menu
1. Create List
2. Display List
3. Insert After An Existing Value
4. Delete A Particular Value
5. Exit

Enter your choice (1 to 5): 1

Input The 1 Node (Enter 0 To Terminate) : 10

Input The 2 Node (Enter 0 To Terminate) : 20

Input The 3 Node (Enter 0 To Terminate) : 30

Input The 4 Node (Enter 0 To Terminate) : 0

4
Menu
1. Create List
2. Display List
3. Insert After An Existing Value
4. Delete A Particular Value
5. Exit

Enter your choice (1 to 5): 2


Nodes are :
10 20 30

Menu
1. Create List
2. Display List
3. Insert After An Existing Value
4. Delete A Particular Value
5. Exit

Enter your choice (1 to 5): 3

Original Nodes are :


10 20 30
Enter The Value After Which U Want To Insert: 20

Enter The Value U Want To Insert : 25

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

Enter your choice (1 to 5): 4

Original Nodes are :


10 20 25 30
Enter The Value U Want To Delete : 20

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

Enter your choice (1 to 5): 5

You might also like