0% found this document useful (0 votes)
9 views323 pages

C Programming: Linked List Operations

The document outlines a series of menu-driven C programming exercises focused on creating and manipulating singly linked lists. Each exercise includes functions for creating a list, inserting or deleting nodes, displaying the list, and counting nodes, along with sample outputs. It serves as a practical guide for students in a computer applications course at Navgujarat College.

Uploaded by

jaydeepleuva2007
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)
9 views323 pages

C Programming: Linked List Operations

The document outlines a series of menu-driven C programming exercises focused on creating and manipulating singly linked lists. Each exercise includes functions for creating a list, inserting or deleting nodes, displaying the list, and counting nodes, along with sample outputs. It serves as a practical guide for students in a computer applications course at Navgujarat College.

Uploaded by

jaydeepleuva2007
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

Navgujarat College Of Computer Applications

Navgujarat Campus, Prin. M.C. Shah Marg, Ashram Road, Ahmedabad

[Link] [Link]

Subject Code:DSC-C-BCA-121 P
Subject Name: DATA Structure Using C Programming Practical

Name : Koshti Harsh S. .


\
Roll No : 1 053

Semester : II

Year:2024-25

Subject-Faculty : Prof. Rutvi vijay .

Signature : Prof. rutvi vijay


[Link] a menu driven program as below:
• Create a singly linked list
• Insert a node at the beginning
• Display
• Exit
#include<stdio.h>
#include<malloc.h>
struct node
{ int info;
struct node *next;
} *start;
void main()
{ int choice, n, m, i;
clrscr();
start=NULL;
while(1)
{
printf("[Link] List\n");
printf("[Link] at begining\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want enter : ");
scanf("%d" ,&n);
for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d" ,&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int data)
{
struct node *ptr,*new_node; new_node=
malloc(sizeof(struct node));
new_node->info=data;
new_node->next=NULL;
if(start==NULL)
start=new_node; else
{
ptr=start;
while(ptr -> next != NULL) ptr=ptr-
>next; ptr->next=new_node;
}
return;
}
addatbeg(int data)
{
struct node *new_node;
new_node=malloc(sizeof(struct node));
new_node->info=data; new_node-
>next=start; start=new_node;
return;
}
display()
{
struct node *ptr;
if(start == NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr != NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}

 Output :-
1 : Create List . 2 : List before adding Element at beginning .

3 : Insert Element at the beginning . 4 : Display . 5 : Exit .


[Link] a menu driven program as below :
• Create a singly linked list
• Insert a node at the end
• Display
• Exit
#include<stdio.h>
#include<malloc.h>
struct node
{ int info;
struct node *next;
} *start;
void main()
{ int choice, n, m, i;
clrscr();
start=NULL;
while (1)
{
printf("[Link] List\n");
printf("[Link] Node at the end\n");
printf("[Link] \n"); printf("[Link]
\n");
printf("Enter your choice : ");
scanf("%d" ,&choice); switch(choice)
{
case 1:
printf("How many nodes you want enter : ");
scanf("%d" ,&n);
for(i=0; i<n; i++)
{
printf("Enter the node : ");
scanf("%d" ,&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatend(m); break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int data)
{
struct node *ptr,*new_node; new_node=
malloc(sizeof(struct node));
new_node->info=data;
new_node->next=NULL;
if(start==NULL)
start=new_node; else
{
ptr=start;
while(ptr->next!=NULL) ptr=ptr->next;
ptr->next=new_node;
}
return;
}
addatend(int data)
{
struct node *new_node, *ptr;
new_node = malloc(sizeof(struct node));
new_node->info = data;
new_node->next = NULL; if
(start == NULL)
{
start = new_node;
return;
}
ptr = start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = new_node;
return;
}
display()
{
struct node *ptr;
if(start == NULL)
{
printf("List is empty\n");
return;
}
ptr=start;
printf("List is :\n");
while(ptr != NULL)
{
printf("%d ", ptr->info);
ptr = ptr->next;
}
printf("\n");
return;
}
 Output :-
Create List:

List before adding node at the end:

Insert node at the end of list:

Display:

Quit:
[Link] a menu driven program as below:
• Create a Singly linked list
• Insert a node at any position
• Display
• Exit
#include<stdio.h>
#include<malloc.h>
struct node
{ int info;
struct node *next;
}*start;
void main()
{
int choice,n,m,position,i;
clrscr();
start=NULL;
while(1)
{
printf("[Link] List\n");
printf("[Link] at any position\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want enter : ");
scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position in which you want to insert node: ");
scanf("%d",&position); addafter(m,position); break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int data)
{
struct node *ptr, *new_node; new_node=
malloc(sizeof(struct node));
new_node->info=data;
new_node->next=NULL;
if(start==NULL)
start=new_node; else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next; ptr-
>next=new_node;
}
return;
}
addafter(int data,int pos)
{
struct node *new_node, *ptr; int
i;
ptr=start; for(i=1;
i<pos; i++)
{
ptr=ptr->next;
if(ptr == NULL)
{
printf("There are less than %d elements\n",pos);
return;
}
}
new_node=malloc(sizeof(struct node) );
new_node->next=ptr->next; new_node-
>info=data; ptr->next=new_node;
return;
}
display()
{
struct node *ptr;
if(start == NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr!=NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}
 Output :-
Create List:

List Before adding element:

Inserting a node at particular posit

Display:

Quit:
[Link] a menu driven program as below:
• Create a Singly linked list
• Count the number of nodes in the list
• Display
• Exit
#include<stdio.h>
#include<malloc.h>
struct node
{ int info;
struct node *next;
}*start;
void main()
{ int choice ,n ,m ,i;
clrscr();
start=NULL;
while(1)
{
printf("[Link] List\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want enter :
"); scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
count();
break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int data)
{
struct node *ptr,*new_node; new_node=
malloc(sizeof(struct node));
new_node->info=data;
new_node->next=NULL;
if(start==NULL)
start=new_node; else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next; ptr-
>next=new_node;
}
return;
}
count()
{
struct node *ptr=start; int
cnt=0; while(ptr!=NULL)
{
ptr=ptr->next; cnt++;
}
printf("Number of elements are %d\n" ,cnt);
return;
}
display()
{
struct node *ptr;
if(start == NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr != NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}

 Output :-
1 : Create List . 2 : Count .

3 : Display : 4 :Quit:
[Link] a menu driven program as below:
• Create a singly linked list
• Delete First node
• Display
• Exit
#include<stdio.h> #include<malloc.h>
struct node
{ int info;
struct node *next;
}*start;
void main()
{ int choice, n, m, i;
clrscr();
start=NULL;
while(1)
{
printf("[Link] List\n");
printf("[Link] first node\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want enter :
"); scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
if(start==NULL)
{
printf("List is empty\n");
continue;
}
del();
break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int data)
{
struct node *ptr,*new_node; new_node=
malloc(sizeof(struct node));
new_node->info=data;
new_node->next=NULL;
if(start==NULL)
start=new_node; else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next; ptr-
>next=new_node;
}
return;
}
del()
{
struct node *ptr = start; if
(start == NULL)
{
printf("List is empty\n"); return;
}
start = start->next; free(ptr);
printf("First node deleted\n"); return;
}
display()
{
struct node *ptr;
if(start == NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr!=NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}
 Output :-
1 : Create List : 2 : List Before deleting node :

3 : Deleting Node : 4 : Display :

5 : Exit :
[Link] a menu driven program as below :
• Create a singly linked list
• Delete Last node
• Display
• Exit
#include<stdio.h>
#include<malloc.h>
struct node
{ int info;
struct node *next;
}*start;
void main()
{ int choice, n, m, i;
clrscr();
start=NULL;
while(1)
{
printf("[Link] List\n");
printf("[Link] Last node\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want enter : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d" ,&m);
create_list(m);
}
break;
case 2:
if(start==NULL)
{
printf("List is empty\n");
continue;
}
del();
break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int data)
{
struct node *ptr,*new_node;
new_node= malloc(sizeof(struct node));
new_node->info=data;
new_node->next=NULL;
if(start==NULL)
start=new_node; else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next; ptr-
>next=new_node;
}
return;
}
del()
{
struct node *ptr, *preptr ;
ptr=start;
preptr = NULL;
if (start == NULL)
{
printf("List is empty\n"); return;
}
while (ptr->next != NULL)
{
preptr = ptr;
ptr = ptr->next;
}
if (preptr == NULL)
{
free(ptr);
start = NULL;
}
else
{
preptr->next = NULL;
free(ptr);
}
printf("Last node deleted\n"); return;
}
display()
{
struct node *ptr;
if(start == NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr != NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}

 Output :-
1 : Create List: 2 : List Before deleting last node:

3 : Delete Last Node: 4 : Display:

5 : Quit:
Q-7. Write a menu driven program as below :
• Create a singly linked list
• Delete a node with particular value of the node
• Display
• Exit
#include<stdio.h>
#include<malloc.h>
struct node
{ int info;
struct node *next;
}*start;
void main()
{ int choice, n, m, i;
clrscr();
start=NULL;
while(1)
{
printf("[Link] List\n");
printf("[Link] Particular Node\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want enter :
"); scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
if(start==NULL)
{
printf("List is empty\n");
continue;
}
printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int data)
{
struct node *ptr,*new_node; new_node=
malloc(sizeof(struct node));
new_node->info=data;
new_node->next=NULL;
if(start==NULL)
start=new_node; else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next; ptr-
>next=new_node;
}
return;
}
del(int data)
{
struct node *ptr, *preptr;
int f = 0;
if (start->info == data)
{
ptr = start; start =
start->next;
free(ptr); f = 1;
return;
}
ptr = start; preptr
= NULL;
while (ptr != NULL && ptr->info != data)
{
preptr = ptr;
ptr = ptr->next;
}
if (ptr == NULL)
{
printf("Element %d not found\n", data); return;
}
if (preptr != NULL)
{
preptr->next = ptr->next;
free(ptr);
f = 1;
}
else
{
start = ptr-
>next; free(ptr);
f = 1;
}
if (f == 0)
{
printf("Element %d not found\n", data);
}
return;
}
display()
{
struct node *ptr;
if(start == NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr != NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}

 Output :-
1 : Create List : 2 : List before deleting any node

3 : Deleting particular node : 4 : Display 5 : Quit


[Link] a menu driven program as below:
• Create a doubly linked list
• Insert a node at the beginning
• Display
• Exit
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *prev; int
info;
struct node *next;
}*start;
main()
{ int choice ,n ,m ,i;
start=NULL;
clrscr();
while(1)
{
printf("[Link] List\n");
printf("[Link] a node at the
beginning\n"); printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int num)
{
struct node *ptr, *new_node;
new_node= malloc(sizeof(struct node));
new_node->info=num;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
start=new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL) ptr=ptr->next;
ptr->next=new_node;
new_node->prev=ptr;
}
return;
}
addatbeg(int num)
{
struct node *new_node;
new_node=malloc(sizeof(struct node));
new_node->prev=NULL; new_node-
>info=num; new_node->next=start;
start->prev=new_node; start=new_node;
return;
}
display()
{
struct node *ptr;
if(start==NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr!=NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}
 Output :-
1 : Create List .

2 : List before adding Element at beginning .

3 : Inserting Element at the beginning .

4 : Display List .

5 : Exit .
Q-9 .Write a menu driven program as below :
• Create a doubly linked list
• Insert a node at the end
• Display
• Exit
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
main()
{ int choice, n, m, i;
start=NULL;
clrscr();
while(1)
{
printf("[Link] List\n");
printf("[Link] at the end\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the Element for Insert at end: ");
scanf("%d",&m); addatend(m); break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int num)
{
struct node *ptr, *new_node;
new_node= malloc(sizeof(struct node));
new_node->info=num;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
start=new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL) ptr=ptr-
>next; ptr->next=new_node;
new_node->prev=ptr;
}
return;
}
addatend(int data)
{
struct node *new_node,*ptr;
new_node=malloc(sizeof(struct node)); new_node->info=data;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
start=new_node;
return;
}
ptr=start;
while(ptr->next!=NULL) ptr=ptr->next;
ptr->next=new_node; new_node->prev=ptr;
return;
}
display()
{
struct node *ptr;
if(start==NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr!=NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}
 Output :-
1 : Create list .

2 : List before adding node at the end .

3 : Insert node at the end of list .

4 : Display .

5 : Quit .
Q-10 : Write a menu driven program as below:
• Create a Doubly linked list
• Insert a node at any position
• Display
• Exit
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
main()
{
int choice, n, m, po, i;
start=NULL;
clrscr();
while(1)
{
printf("[Link] List\n");
printf("[Link] at any position\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position for entering new node:
"); scanf("%d",&po); addafter(m,po); break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int num)
{
struct node *ptr,*new_node; new_node=
malloc(sizeof(struct node));
new_node->info=num;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
start=new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next; ptr-
>next=new_node;
new_node->prev=ptr;
}
return;
}
addafter(int num, int c)
{
struct node *new_node, *ptr; int
i;
ptr=start;
for(i=1;i<c;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("There are less than %d elements\n",c); return;
}
}
new_node=malloc(sizeof(struct node) );
new_node->info=num; ptr->next-
>prev=new_node; new_node->next=ptr-
>next; new_node->prev=ptr; ptr-
>next=new_node;
return;
}
display()
{
struct node *ptr;
if(start==NULL)
{
printf("List is empty\n");
return;
}
ptr=start;
printf("List is :\n");
while(ptr!=NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}
 Output :-
1 : Create List . 2 : List Before adding element .

3 : Inserting a node at particular position . 4 : Display .

5 : Quit .
Q-11 : Write a menu driven program as below:
• Create a Doubly linked list
• Count the number of nodes in the list
• Display
• Exit
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
main()
{ int choice ,n ,m ,i;
start=NULL;
clrscr();
while(1)
{
printf("[Link] List\n");
printf("[Link] the number of nodes\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
count();
break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int num)
{
struct node *ptr,*new_node; new_node=
malloc(sizeof(struct node));
new_node->info=num;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
start=new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next; ptr-
>next=new_node;
new_node->prev=ptr;
}
return;
}
count()
{
struct node *ptr=start; int
cnt=0; while(ptr!=NULL)
{
ptr=ptr->next; cnt++;
}
printf("Number of elements are %d\n",cnt); return;
}
display()
{
struct node *ptr;
if(start==NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr!=NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}

 Output :-
1 : Create List . 2 : Count . 3 : Display .

4 : Quit .
Q-12 : Write a menu driven program as below:
• Create a Doubly linked list
• Delete First node
• Display
• Exit
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
main()
{ int choice ,n ,m ,i;
start=NULL;
clrscr();
while(1)
{
printf("[Link] List\n");
printf("[Link] First Node\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
del();
break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int num)
{
struct node *ptr,*new_node;
new_node= malloc(sizeof(struct node));
new_node->info=num;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
start=new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next; ptr-
>next=new_node;
new_node->prev=ptr;
}
return;
}
del()
{
struct node *new_node, *ptr;
if(start==NULL)
{
printf("\nList is empty\n"); return;
}
ptr=start; start=start->next;
start->prev=NULL;
free(ptr);
printf("First Node deleted\n"); return;
}
display()
{
struct node *ptr;
if(start==NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr!=NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}
 Output :-
1 : Create List .

2 : List Before deleting node .

3 : List after Deleting Node .

4 : Display .

5 : Quit .
Q-13 : Write a menu driven program as below :
• Create a Doubly linked list
• Delete Last node
• Display
• Exit
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
main()
{ int choice ,n ,m ,i;
start=NULL;
clrscr();
while(1)
{
printf("[Link] List\n");
printf("[Link] Last Node\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
del();
break;
case 3:
display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int num)
{
struct node *ptr,*new_node; new_node=
malloc(sizeof(struct node));
new_node->info=num;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
start=new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next; ptr-
>next=new_node;
new_node->prev=ptr;
}
return;
}
del()
{
struct node *new_node,*ptr;
if(start==NULL)
{
printf("\nList is empty\n"); return;
}
ptr=start;
while(ptr->next!=NULL) ptr=ptr->next;
ptr->prev->next=NULL;
free(ptr);
printf("Last Node Deleted\n"); return;
}
display()
{
struct node *ptr;
if(start==NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr!=NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}
 Output :-
1 : Create List .

2 : List Before deleting last node .

3 : After Delete Last Node .

4 : Display .

5 : Quit .
Q-14 : Write a menu driven program as below:
• Create a Doubly linked list
• Delete a node with particular value of the node
• Display
• Exit
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
main()
{ int choice ,n ,m ,i;
start=NULL;
clrscr();
while(1)
{
printf("[Link] List\n");
printf("[Link] Particular Node\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice); switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n); for(i=0; i<n; i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break; case
3: display();
break;
case 4: exit();
default:
printf("Wrong choice\n");
}
}
}
create_list(int num)
{
struct node *ptr,*new_node; new_node=
malloc(sizeof(struct node));
new_node->info=num;
new_node->next=NULL;
if(start==NULL)
{
new_node->prev=NULL;
start=new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL) ptr=ptr-
>next; ptr->next=new_node;
new_node->prev=ptr;
}
return;
}
del(int num)
{
struct node *new_node, *ptr, *temp;
if(start->info==num)
{
ptr=start; start=start->next;
start->prev = NULL;
free(ptr);
return;
}
ptr=start;
while(ptr->next->next!=NULL)
{
if(ptr->next->info==num)
{
temp=ptr->next; ptr->next=temp-
>next; temp->next->prev=ptr;
free(temp);
return;
}
ptr=ptr->next;
}
if(ptr->next != NULL && ptr->next->info == num)
{
ptr=ptr->next; ptr->prev->next=NULL;
free(ptr);
return;
}
printf("Element %d not found\n",num); return;
}
display()
{
struct node *ptr;
if(start==NULL)
{
printf("List is empty\n"); return;
}
ptr=start;
printf("List is :\n");
while(ptr!=NULL)
{
printf("%d ", ptr->info);
ptr=ptr->next;
}
printf("\n");
return;
}

 Output :-
1 : Create List .

2 : List before deleting any node .

3 : Deleting particular node .

4 : Display . 5 : Exit .
Q-15 : Write a C program to implement following operations in STACK (using Array)
• PUSH
• POP
• PEEK
• DISPLAY
• Exit
#include<stdio.h>
#define MAX 5

int top = -1;


int stack_arr[MAX];

main()
{
int choice;
clrscr();
while(1)
{
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

push()
{
int pushed_item;
if(top == (MAX-1))
printf("Stack Overflow OR stack is full\n");
else
{
printf("Enter the item to be pushed in stack :
"); scanf("%d",&pushed_item);
top=top+1;
stack_arr[top] = pushed_item;
}
}/*End of push()*/

pop()
{
if(top == -1)
printf("Stack Underflow OR Stack is Empty\n");
else
{
printf("Popped element is :
%d\n",stack_arr[top]);
top=top-1;
}
}/*End of pop()*/

peek()
{
if(top == -1)
printf("Stack Underflow OR Stack is Empty\n");
else
printf("The topmost element of stack is: %d\n",stack_arr[top]);
return(stack_arr[top]);
}
display()
{
int i;
if(top == -1)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n"); for(i = top; i
>=0; i--)
printf("%d\n", stack_arr[i] );
}
}/*End of display()*/

 Output :-
1 : PUSH . 2 : PUSH with Display . 3 : POP With Display

4 : Peek .
Q-16 : Write a C program to implement following operations in STACK (using
Linked List)
• PUSH
• POP
• PEEK
• DISPLAY
• Exit
# include<stdio.h>
# include<malloc.h>

struct node
{
int data;
struct node *next;
} *top=NULL;

main()
{
int choice;
clrscr();
while(1)
{ printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ") ;
scanf("%d", &choice);

switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main() */

push()
{
struct node *new_node;
int pushed_item;
new_node = malloc(sizeof(struct node));
printf("Input the new value to be pushed on
the stack : ");
scanf("%d",&pushed_item);
new_node-
>data=pushed_item; new_node-
>next=top; top=new_node;
return;
}/*End of push()*/

pop()
{
struct node *tmp;
if(top == NULL)
printf("Stack is empty\n");
else
{ tmp=top;
printf("Popped item is %d\n",tmp->data);
top=top->next;
free(tmp);
}

return;
}/*End of pop()*/

peek()
{
if(top==NULL)
printf("Stack is empty\n");
else
printf("The topmost element of the stack is: %d\n",top->data);
return top->data;
}
display()
{ struct node *ptr;
ptr=top;
if(top==NULL)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n"); while(ptr!= NULL)
{
printf("%d\n",ptr->data);
ptr = ptr->next;
}/*End of while
*/ }/*End of else*/
return;
}/*End of display()*/

 Output :-
1 : PUSH with Display . 2 : POP with Display . 3 : PEEK with Display .
Q-17 : Write a C program to reverse the string using the STACK.

#include<stdio.h>
#define MAX 20
#include<string.h>

int top = -1; char


stack[MAX];
char pop();
push(char);
void main()
{
char str[20];
int i;
clrscr();
printf("Enter the string : " ); gets(str);
//scanf( %S,STR)
/*Push characters of the string str on the stack */
for(i=0;i<strlen(str);i++)
push(str[i]);
/*Pop characters from the stack and store in string str */
for(i=0;i<strlen(str);i++)
str[i]=pop();
printf("Reversed string is : ");
puts(str); //printf
getch();
}/*End of main()*/

push(char item)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
stack[++top] =item;
return;
}/*End of push()*/
char pop()
{
if(top == -1)
printf("Stack Underflow\n");
else
return stack[top--];
}/*End of pop()*/

 Output :-
1 : Reverse String .
Q-18 : Write a C program to implement recursion.

#include<stdio.h>
main( )
{
int n,value;
clrscr();
printf( "Enter the number : ");
scanf( "%d", &n );
if ( n < 0 )
printf( "No factorial of negative
number\n");
else if (
n==0 )
printf( "Factorial of zero is 1\n" );
else
{
value = factorial( n ); /* Function for factorial of number */ printf( "Factorial
of %d = %d\n", n, value );
}
getch();
}/*End of main()*/ factorial(
int k )
{
int fact =1;
if ( k > 1 )
fact = k * factorial( k-1 ); /* Recursive function call */
return ( fact );
}
 Output :-
1 : Factorial ..
Q-19 : Write a C program to implement conversion of infix expression into postfix
expression (parentheses and non-parentheses).

#include<stdio.h>
#include<string.h>
#include<math.h>
#define Blank ' '
#define Tab '\t'
#define MAX 50

long int pop ();


char infix[MAX], postfix[MAX]; long
int stack[MAX];
int top;
void main()
{
long int value;

char choice='y';
clrscr();
while(choice == 'y')
{
top = 0;
printf("Enter infix : ");
fflush(stdin); //display output
gets(infix);

infix_to_postfix();

printf("Postfix : %s\n",postfix);
printf("Want to continue(yes/no) : ");
scanf("%c",&choice);

}
getch();
}
infix_to_postfix()
{

int i,p=0,type,precedence,len;

char next
;

stack[top]='#';
if( !white_space(infix[i]))
len=strlen(infix);
infix[len]='#';
switch(infix[i])
for(i=0; infix[i]!='#';i++)
{ case
{
'(':
push(infix[i]);
{ break;

case ')':
while((next = pop()) != '(')
postfix[p++] = next;

break
; case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
precedence = prec(infix[i]);

while(stack[top]!='#' && precedence<= prec(stack[top]))


postfix[p++] = pop();
push(infix[i]);
break;
default: /*if an operand comes
*/ postfix[p++] = infix[i]; }
}
}
while(stack[top]!='#')
postfix[p++] = pop();
postfix[p] = '\0' ; /*End
postfix with'\0' to make it a string*/
}

/* This function returns the precedence of the operator */


prec(char symbol )
{
switch(symbol)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2; case '^':
return 3;
}
}

push(long int symbol)


{
if(top > MAX)
{
printf("Stack overflow\n");
exit(1);
}
else
{
top=top+1;
stack[top] = symbol;
}
}

long int pop()


{
if (top == -1 )
{
printf("Stack underflow \n");
exit(2);
}
else
return (stack[top--]);
}

white_space(char symbol)
{
if( symbol == Blank || symbol == Tab ||
symbol == '\0')
return 1;
else
return 0;
}

 Output :-
1 : Infix to Postfix .
Q-20 : Write a C program to implement following operations in
SIMPLE QUEUE (using array)
• ENQUEUE (Insertion)
• DEQUEUE (Deletion)
• DISPLAY
/*Program of queue using array*/
# include<stdio.h>
# define MAX 5

int queue_arr[MAX]; int


rear = -1;
int front = -1;

main()
{
int choice;
clrscr();
while(1)
{
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

insert()
{
int added_item;
if (rear==MAX-1)
printf("Queue Overflow\n");
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in
queue : ");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear] = added_item ;
}
return;
}/*End of insert()*/

del()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_arr[front]);
front=front+1;
}
return;
}/*End of del() */
display()
{
int i;
if (front == -1 ||
front > rear)
printf("Queue is empty\n");
else
{
printf("Queue is :\n"); for(i=front;i<=
rear;i++) printf("%d ",queue_arr[i]);
printf("\n");
}
return;
}/*End of display() */

 Output :-
1 : ENQUE . 2 : Display and DEQUE . 3 : DEQUE and Display
Q-21 : Write a C program to implement following operations in
SIMPLE QUEUE (using Linked List)
• ENQUEUE (Insertion)
• DEQUEUE (Deletion)
• DISPLAY
/* Program of queue using linked list*/
# include<stdio.h>
# include<malloc.h>

struct node
{
int data;
struct node *next;
}*front=NULL,*rear=NULL;

main()
{
int choice;
clrscr();
while(1)
{ printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d", &choice);

switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

insert()
{
struct node *new_node;
int val;
new_node = malloc(sizeof(struct node));
printf("Input the element for adding in queue :
"); scanf("%d",&val); new_node->data = val;
new_node->next=NULL;
if(front==NULL) /*If Queue is empty*/
front=new_node; else
rear->next=new_node;
rear=new_node;
return;
}/*End of insert()*/

del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
tmp=front;
printf("Deleted element is %d\n",tmp-
>data);
front=front->next;
free(tmp);
}
return;
}/*End of del()*/
display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{
printf("Queue elements :\n");
while(ptr != NULL)
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
printf("\n");
}/*End of else*/
return;
}/*End of display()*/

 Output :-
1 : ENQUE and Display . 2 : DEQUE and Display .
Q-22 : Write a C program to implement following operations in
CIRCULAR QUEUE (using array)
• ENQUEUE (Insertion)
• DEQUEUE (Deletion)
• DISPLAY
/*Program of queue using array*/
# include<stdio.h>
# define MAX 5

int queue_arr[MAX]; int


rear = -1;
int front = -1;

main()
{
int choice;
clrscr();
while(1)
{
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

insert()
{
int added_item;
if (rear==MAX-1)
printf("Queue Overflow\n");
else
{
if (front==-1) /*If queue is initially empty */
front=0;
printf("Input the element for adding in
queue : ");
scanf("%d", &added_item);
rear=rear+1;
queue_arr[rear] = added_item ;
}
return;
}/*End of insert()*/

del()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow\n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_arr[front]);
front=front+1;
}
return;
}/*End of del() */
display()
{
int i;
if (front == -1 ||
front > rear)
printf("Queue is empty\n");
else
{
printf("Queue is :\n"); for(i=front;i<=
rear;i++) printf("%d ",queue_arr[i]);
printf("\n");
}
return;
}/*End of display() */

 Output :-
1 : ENQUE . 2 : DEQUE and Display .
Q-23 :Write a C program to implement Linear / Sequential Search.

#include<stdio.h>
#include<conio.h> void
main()
{
int a[10],val,i,n,pos=-1;
clrscr();
printf("\nEnter the number of elements in the array:");
scanf("%d",&n);
printf("\n Enter Elements:");
for(i=0;i<n;i++)
{
printf("\n [%d]=",i);
scanf("%d",&a[i]);
}

printf("\nPlease Enter the number for search::");


scanf("%d",&val);

for(i=0;i<n;i++)
{
if(a[i] == val)
{
pos = i;
break;
}
}
if(pos == -1)
printf("\nSearch un-successful: Key Not Found.");
else
printf("\nSearch Successful: Key is found at location= %d",i);
getch();
}
 Output :-
Q-24 : Write a C program to implement Binary Search.

//Write a C progam to implement Binary Search


#include<stdio.h>
#include<conio.h> void
main()
{
int beg,mid,end,n,i,val,a[20];
clrscr();
printf("Enter the numbers of element in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter element %d:",i+1);
scanf("%d",&a[i]);
}
printf("\nEnter element for search:");
scanf("%d",&val);

beg=0;
end=n;

while(beg<=end)
{
mid=(beg+end)/2;
if(a[mid] == val)
{
printf("\n %d is found at position:
%d",val,mid);
break;
}
else if(a[mid] > val)
end = mid - 1;
else
beg = mid + 1;
}
if(beg>end)
printf("not found\n",val);
getch();
}

 Output :-
Q-25 : Write a C program to implement Bubble Sort.

#include<stdio.h> void
main()
{
int a[5],j,temp,n,i;
clrscr();
printf("Enter the number of element:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d:",i+1);
scanf("%d",&a[i]);
}
printf("Unstoted list is :\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n");

for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("sorted list is :\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
 Output :-
Q-26 : Write a C program to implement Selection Sort.

#include<stdio.h>
#include<conio.h> void
main()
{
int a[10],n,pass,i,t,l,min;
clrscr();
printf("\n Enter size of selection short :: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter a value %d: ",i+1);
scanf("%d",&a[i]);
}
printf("\n Before selection short :");
for(i=0;i<n;i++)
{
printf(" %d ",a[i]);
}

for(pass=0;pass<n-1;pass++)
{
min = pass;
for(i=pass+1;i<n;i++)
{
if(a[i] < a[min])
min = i ;
}
if(min != pass)
{
t = a[pass];
a[pass] = a[min];
a[min] = t;
}
}
printf("\n After selection short :");
for(i=0;i<n;i++)
{
printf(" %d ",a[i]);
}
getch();
}

 Output :-
Q-27 : Write a C program to implement Insertion Sort.

#include<stdio.h>
#include<conio.h> void
main()
{
int i,k,n,temp,j,arr[10];
clrscr();
printf("How many node you want to insert ::");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter element : %d:",i+1);
scanf("%d",&arr[i]);
}
printf("Unsorted Array:\n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}

for(k=1;k<n;k++)
{
temp=arr[k];
j=k-1;
while((temp<=arr[j])&&(j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
printf("\nSorted Array:");
for(i=0;i<n;i++)
printf("\n%d",arr[i]);
getch();
}

 Output :-
Q-28 : Write a C program to implement following operations on Binary
Search Tree using Linked List
• CREATION
• INSERTION
• TRAVERSAL (In-Order, Pre-Order, Post-Order)
/*Insertion ,Deletion and Traversal in Binary Search Tree*/
# include <stdio.h>
# include <malloc.h>

struct node
{
int info; struct
node *lchild;
struct node *rchild;
}*root;

void main()
{
int choice,num;
clrscr();
root=NULL;
while(1)
{
printf("\n");
printf("[Link]\n");
printf("[Link] Traversal\n");
printf("[Link] Traversal\n");
printf("[Link] Traversal\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("Enter the number to be
inserted : ");
scanf("%d",&num);
insert(num);
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
display(root,1);
break;
case 6:
exit();
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
getch();
}/*End of main()*/
find(int item,struct node **par,struct node **loc)
{
struct node *ptr,*ptrsave;
if(root==NULL) /*tree empty*/
{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->info) /*item is at root*/
{
*loc=root;
*par=NULL;
return;
}
/*Initialize ptr and ptrsave*/
if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;
while(ptr!=NULL)
{
if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}/*End of while */
*loc=NULL; /*item not found*/
*par=ptrsave;
}/*End of find()*/
insert(int item)
{ struct node *tmp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{
printf("Item already present");
return;
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=item; tmp-
>lchild=NULL;
tmp->rchild=NULL;
if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}/*End of insert()*/
preorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
printf("%d ",ptr->info); preorder(ptr-
>lchild);
preorder(ptr->rchild);
}
}/*End of preorder()*/
inorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/
postorder(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->info);
}
}/*End of postorder()*/

display(struct node *ptr,int level)


{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/
 Output :-
1 : INSERT . 2 : Insert and Display . 3 : preorder and postorder.

You might also like