C Slip 16
C Slip 16
CERTIFICATE
--------------------------- -----------------------------
Signature of Examiner Signature of Examiner
(External) (internal)
--------------------- ----------------------
(Subject Prof.) (HOD)
INDEX
NAME-
SUBJECT-
SEMESTER- YEAR-
CLASS- ROLL NO-
EXAM SEAT NO-
SUBMISSION REPORT
Pract. Date Title Page Subject Teacher
No. No. Signature
01 A) Write menu driven program using ‘C’ for Binary
Search Tree. The menu includes
B) Write a ‘C’ program to evaluate a given polynomial
using function. (Use array).
02 A) Write a ‘C’ program to accept a string from user and
reverse it using Static implementation of Stack.
B) Write a ‘C’ program to create Circularly Doubly Linked
list and display it.
03 A)Write a program to create two singly linked list of
elements of type integer and findthe union of the linked
lists. (Accept elements in the sorted order)
B) Write a ‘C’ program to read the adjacency matrix of
directed graph and convert it into adjacency list.
04 A) Write menu driven program using ‘C’ for Binary
Search Tree. The menu includes
B) Write a ‘C’ program to accept two polynomial and
find the addition of accepted polynomials.(use array)
05 A)Write menu driven program using ‘C’ for Binary Search
Tree. The menu includes
B) Write a ‘C’ program to create linked list with given
number in which data part of each node contains
individual digit of the number
06 A) Write menu driven program using ‘C’ for Binary
Search Tree. The menu includes
B) Write a ‘C’ program to accept and sort n elements in
ascending order by using bubble sort.
07 A) Write menu driven program using ‘C’ for Binary
Search Tree. The menu includes
B) Write a ‘C’ program to create a singly linked list and
count total number of nodes in it and display the list and
total number of Nodes.
08 A) Write menu driven program using ‘C’ for Binary
Search Tree. The menu includes
B) Write a ‘C’ program to accept and sort n elements in
ascending order by using insertion sort.
09 A) Write a program to accept a postfix expression and
evaluate the expression using the stack.
B) Write a ‘C’ program to create a singly linked list,
reverse it and display both the list.
10 A) Write a ‘C’ program to read ‘n’ integers and store
them in a Binary search tree and display the nodes level
wise.
B) Write a ‘C’ program to sort randomly generated array
elements using Insertion sort method. (Use Random
Function)
11 A) Write a menu driven program using ‘C’ for singly
linked list
B) Write a menu driven program using ‘C’ for Dynamic
implementation of Queue for integers. The menu
includes
12 A) Write a C program that accepts the graph as an
adjacency matrix and checks if the graph is undirected.
The matrix for undirected graph is symmetric. Also
calculate in
degree of all vertices
B) Write a ‘C’ program to accept and sort n elements in
ascending order using Selection sort method.
13 A) Write a C program to accept an infix expression and
convert it into postfix form.(Use Static Implementation
of Stack)
B) Write a ‘C’ program to create doubly link list and
display nodes having odd value
14 A) Write a ‘C’ program to accept a string from user and
reverse it using Dynamic implementation of Stack.
B)Write a ‘C’ program to accept names from the user
and sort in alphabetical order using bubble sort
15 A) Write a ‘C’ program to accept an infix expression,
convert it into its equivalent postfix expression and
display the result.(Use Dynamic Implementation of
Stack)
B)Write menu driven program using ‘C’ for Dynamic
implementation of Stack. The menu includes following
operations:
16 A) Write a ‘C’ program which accept the string and
reverse each word of the string using Static
implementation of stack.
B) Write a ‘C’ program to create to a Singly linked list.
Accept the number from user, search the number in the
list.If the number is present display the Position of
node .If number not present print the message “Number
not Found”.
17 A) Write a ‘C’ program to read a postfix expression,
evaluate it and display the result.(Use Static
Implementation of Stack).
B) Write a ‘C’ program to accept the names of cities and
store them in array. Accept the city name from user and
use linear search algorithm to check whether the city is
present in array or not.
18 A) Write a ‘C’ program to read ‘n’ integers and store
them in a binary Search tree structure and count the
following and display it.
B) Write a ‘C’ program to accept and sort n elements in
ascending order using Merge sort method.
19 A) Write a ‘C’ program which accept the string and
reverse each word of the string using Dynamic
implementation of stack.
B) Write a ‘C’ program to create a singly Link list and
display its alternative nodes.(start displaying from first
node)
20 A) Write a ‘C’ program which accept the string and check
whether the string is Palindrome or not using stack. (Use
Static/Dynamic implementation of Stack)
B) Write a ‘C’ program to swap mth and nth element of
singly linked list.
21 A) Write a ‘C’ program to read an adjacency matrix of a
directed graph and traverse using BFS.
B) Write a ‘C’ program Accept n elements from user
store it in an array. Accept a value from the user and use
linear/Sequential search method to check whether the
value is present in array or not. Display proper message.
22 A) Write a ‘C’ program which accept an Expression and
check whether the expression is Parenthesized or not
using stack. (Use Static/Dynamic implementation of
Stack)
B) Write a ‘C’ program to count all non-zero elements,
odd numbers and even numbers in the singly linked list.
23 A) Write a menu driven program using ‘C’ for singly
linked list
B) Write a ‘C’ program to create a random array of n
integers. Accept a value x from user and use Binary
search algorithm to check whether the number is
present in array or not.(Students can accept sorted array
or can use any sorting method to sort the array)
24 A) Write a ‘C’ program to read a postfix expression,
evaluate it and display the result.(Use Dynamic
Implementation of Stack)
B) Write a ‘C’ program to remove last node of the singly
linked list and insert it at the beginning of list.
25 A) Write a ‘C’ program to read an adjacency matrix of a
directed graph and traverse it using DFS.
B) Write a menu driven program using ‘C’ for singly
linked list
26 A) Write a ‘C’ program to accept an infix expression,
convert it into its equivalent prefix expression and
display the result. (Use Static Implementation of Stack).
B) Write a ‘C’ program to create two singly linked lists
and concatenate one list at the end of another list.
27 A)Implement Static implementation of circular queue of
integers with following operation:
B) Write a ‘C’ program to create Doubly Link list and
display it.
- Display
ANS=
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
if (root == NULL) {
return createNode(value);
return root;
if (root != NULL) {
displayTree(root->left);
displayTree(root->right);
int main() {
do {
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch(choice) {
case 1:
root = NULL;
break;
case 2:
scanf("%d", &value);
printf("Element inserted.\n");
break;
case 3:
displayTree(root);
printf("\n");
break;
case 4:
break;
default:
return 0;
OUTPUT=
Binary Search Tree Menu
4. Exit
3. Display
4. Exit
Element inserted.
3. Display
4. Exit
3. Display
4. Exit
ANS=
#include <stdio.h>
double result = 0;
int i;
return result;
int main() {
int degree, i;
scanf("%d", °ree);
double coefficients[degree+1];
scanf("%lf", &coefficients[i]);
}
double x;
scanf("%lf", &x);
return 0;
OUTPUT=
Enter the degree of the polynomial: 3
Q1) Write a ‘C’ program to accept a string from user and reverse it using Static
implementation of Stack.
Ans=
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stack {
int top;
char data[MAX_SIZE];
};
int main() {
char str[MAX_SIZE];
Output
Enter a string: anjali
Reversed string: ilajna
Q2) Write a ‘C’ program to create Circularly Doubly Linked list and display it.
Ans=
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
if (*head == NULL) {
*head = newNode;
(*head)->prev = *head;
(*head)->next = *head;
return;
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
insertAtEnd(&head, 5);
display(head);
return 0;
}
OUTPUT
Q1) Write a program to create two singly linked list of elements of type integer
and find the union of the linked lists. (Accept elements in the sorted order)
Solution
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct LinkedList {
struct Node* head;
};
if (list->head == NULL) {
list->head = new_node;
} else {
struct Node* current = list->head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_node;
}
}
int main() {
struct LinkedList* list1 = (struct LinkedList*)malloc(sizeof(struct LinkedList));
list1->head = NULL;
insert(list1, 1);
insert(list1, 3);
insert(list1, 5);
insert(list2, 2);
insert(list2, 3);
insert(list2, 4);
Output
Union of the linked lists: 1 2 3 4 5
1
dash: 2: 1: not found
2
dash: 3: 2: not found
Q2) Write a ‘C’ program to read the adjacency matrix of directed graph and
convert it into adjacency list.
Solution
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
struct Graph {
int numVertices;
struct Node** adjLists;
};
struct Node* createNode(int vertex) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->vertex = vertex;
newNode->next = NULL;
return newNode;
}
int i;
for (i = 0; i < numVertices; i++) {
graph->adjLists[i] = NULL;
}
return graph;
}
int main() {
int numVertices, i, j;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &numVertices);
int adjMatrix[numVertices][numVertices];
printf("Enter the adjacency matrix:\n");
for (i = 0; i < numVertices; i++) {
for (j = 0; j < numVertices; j++) {
scanf("%d", &adjMatrix[i][j]);
}
}
return 0;
}
Output
Enter the number of vertices in the graph: 3
Enter the adjacency matrix:
10 20 30
40 50 60
70 80 90
Adjacency list representation of the graph:
Adjacency list of vertex 0: NULL
Adjacency list of vertex 1: NULL
Adjacency list of vertex 2: NULL
Slip 4
A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes -
Create a Binary Search Tree - Traverse it by using Inorder and Postorder traversing
technique
Solution:
#include <stdio.h>
#include <stdlib.h>
struct BST
int data;
};
NODE *node;
if (node == NULL)
{
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->data = data;
return temp;
return node;
{
if(node != NULL)
inorder(node->left);
printf("%d\t", node->data);
inorder(node->right);
if(node != NULL)
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
int main()
NODE *root=NULL;
while (ch!=4)
{
printf("\n\n1.Insertion in Binary Search Tree");
printf("\n2.Inorder");
printf("\n3.Postorder");
printf("\n4.Exit\n");
scanf("%d", &ch);
switch (ch)
scanf("%d", &n);
scanf("%d", &data);
root=createtree(root, data);
break;
inorder(root);
break;
case 3: printf("\nPostorder Traversal: \n");
postorder(root);
break;
case 4: exit(0);
break;
}
Output-:
1.Insertion in Binary Search Tree
2.Inorder
3.Postorder
4.Exit
2.Inorder
3.Postorder
4.Exit
Postorder Traversal:
1 3
2.Inorder
3.Postorder
4.Exit
B) Write a ‘C’ program to accept two polynomial and find the addition of accepted
polynomials.(use array)
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
int a[20]={0},b[20]={0},c[20]={0},i,j,deg1,deg2,k;
// 1st equation.
scanf("%d",°1);
for(i=deg1;i>=0;i--)
scanf("%d",&a[i]);
// 2nd equation.
scanf("%d",°2);
printf("\nEnter your 2nd equation (Coefficient only):\n");
for(j=deg2;j>=0;j--)
scanf("%d",&b[j]);
// printing equations.
for(i=deg1;i>=0;i--)
if(a[i]>=0)
printf("+");
else
printf("-");
printf("%dx^%d",abs(a[i]),i);
if(b[j]>=0)
printf("+");
else
printf("-");
printf("%dx^%d",abs(b[j]),j);
i=j=k=0;
if(deg1>deg2)
while(i<=deg1)
c[k]=a[i]+b[j];
i++;
j++;
k++;
}
printf("\n\nResult of addition : ");
for(k=deg1;k>=0;k--)
if(c[k]>=0)
printf("+");
else
printf("-");
printf("%dx^%d",abs(c[k]),k);
else
while(i<=deg2)
c[k]=b[j]+a[i];
i++;
j++;
k++;
if(c[k]>=0)
printf("+");
else
printf("-");
printf("%dx^%d",abs(c[k]),k);
}
Output-:
Enter degree of 1st polynomial equation :
#include <stdio.h>
#include <stdlib.h>
struct BST
int data;
};
NODE *node;
if (node == NULL)
{
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->data = data;
return temp;
return node;
{
if(node != NULL)
inorder(node->left);
printf("%d\t", node->data);
inorder(node->right);
if(node != NULL)
printf("%d\t", node->data);
preorder(node->left);
preorder(node->right);
int main()
NODE *root=NULL;
while (ch!=4)
{
printf("\n\n1.Insertion in Binary Search Tree");
printf("\n2.Inorder");
printf("\n3.Preorder");
printf("\n4.Exit\n");
scanf("%d", &ch);
switch (ch)
scanf("%d", &n);
scanf("%d", &data);
root=createtree(root, data);
break;
inorder(root);
break;
case 3: printf("\nPreorder Traversal: \n");
preorder(root);
break;
case 4: exit(0);
break;
Output-:
1.Insertion in Binary Search Tree
2.Inorder
3.Preorder
4.Exit
2.Inorder
3.Preorder
4.Exit
Inorder Traversal:
3 4
2.Inorder
3.Preorder
4.Exit
B) Write a ‘C’ program to create linked list with given number in which data part of
each node contains individual digit of the number. (Ex. Suppose the number is 368
then the nodes of linked list should contain 3, 6, 8 )
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
struct node* n;
return(n);
int i=0,x,digits[100];
scanf("%d",&x);
while(x!=0)
digits[i]=x%10;
x=x/10;
i++;
i--;
for(i;i>=0;i--)
head=cn();
head->data=digits[i];
head->next=NULL;
temp=head;
else
newnode=cn();
newnode->data=digits[i];
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
int cnt=0;
temp=head;
while(temp!=NULL)
printf("%d\n",temp->data);
temp=temp->next;
cnt++;
int main()
int choice,a=0;
while(choice!=3)
printf("3. Exit\n\n");
scanf("%d",&choice);
printf("\n");
switch(choice)
case 1: if(a==1)
}
else
{ createnode();
a++;
break;
case 2: traverse();
break;
case 3: exit(0);
}
Output-:
1. Create Linked list.
3. Exit
3. Exit
3. Exit
3. Exit
Slip 6
A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes -
Create a Binary Search Tree - Traverse it by using Preorder and Postorder traversing
technique
Solution:
#include <stdio.h>
#include <stdlib.h>
struct BST
int data;
};
NODE *node;
if (node == NULL)
{
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->data = data;
return temp;
return node;
if(node != NULL)
{
printf("%d\t", node->data);
preorder(node->left);
preorder(node->right);
if(node != NULL)
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
int main()
NODE *root=NULL;
while (ch!=4)
printf("\n2.Preorder");
printf("\n3.Postorder");
printf("\n4.Exit\n");
scanf("%d", &ch);
switch (ch)
scanf("%d", &n);
scanf("%d", &data);
root=createtree(root, data);
break;
preorder(root);
break;
postorder(root);
break;
case 4: exit(0);
break;
}
Output-:
1.Insertion in Binary Search Tree
2.Preorder
3.Postorder
4.Exit
2.Preorder
3.Postorder
4.Exit
Preorder Traversal:
1 2 3
2.Preorder
3.Postorder
4.Exit
B) Write a ‘C’ program to accept and sort n elements in ascending order by using
bubble sort.
Solution:
#include<stdio.h>
void main()
int A[20],temp,i,j,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
bubble_sort(A,n);
for(i=0;i<n;i++)
printf("%d\n",A[i]);
{
int i,j,temp;
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(A[j]>A[j+1])
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
Output-:
Enter size of Array :
1
Enter Array elements :
2
Sorted Array :
2
slip 7
Q.1 A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes
- Display
Solution
#include <stdio.h>
#include <stdlib.h>
struct btnode
int value;
// Function declaration
void create();
void insert();
void search(struct btnode *t);
void delete();
int flag = 1;
void main() {
int ch;
printf("\nOPERATIONS ---");
printf("2 - Display\n");
printf("3 - Delete\n");
printf("4 - Exit\n");
while(1) {
scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
display(root);
break;
case 3:
delete(root);
break;
case 4:
exit(0);
default :
printf("Invalid Input!");
break;
void create() {
int data;
scanf("%d", &data);
temp->value = data;
}
void insert() {
create();
if (root == NULL)
root = temp;
else
search(root);
search(t->r);
t->r = temp;
search(t->l);
t->l = temp;
}
if ((data>t->value)) {
t1 = t;
search1(t->r, data);
t1 = t;
search1(t->l, data);
else if ((data==t->value)) {
delete1(t);
if (root == NULL) {
return;
}
printf("%d -> ", t->value);
if (t->l != NULL) {
display(t->l);
if (t->r != NULL) {
display(t->r);
void delete() {
int data;
if (root == NULL) {
return;
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);
}
// To delete a node
int k;
if (t1->l == t) {
t1->l = NULL;
else {
t1->r = NULL;
t = NULL;
free(t);
return;
if (t1 == t) {
root = t->l;
t1 = root;
}
else if (t1->l == t) {
t1->l = t->l;
else {
t1->r = t->l;
t = NULL;
free(t);
return;
if (t1 == t) {
root = t->r;
t1 = root;
else if (t1->r == t) {
t1->r = t->r;
else {
t1->l = t->r;
t == NULL;
free(t);
return;
t2 = root;
if (t->r != NULL) {
k = smallest(t->r);
flag = 1;
else {
k =largest(t->l);
flag = 2;
search1(root, k);
t->value = k;
t2 = t;
if (t->l != NULL) {
t2 = t;
return(smallest(t->l));
else {
return (t->value);
if (t->r != NULL) {
t2 = t;
return(largest(t->r));
else {
return(t->value);
OUTPUT
1 - Insert an element into tree
2 - Display
3 - Delete
4 - Exit
Enter your choice : 1
2 ->
2 ->
Q.2 B) Write a ‘C’ program to create a singly linked list and count total number of nodes
Solution
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
}*stnode;
void createNodeList(int n) {
int num, i;
if(stnode == NULL) {
else {
scanf("%d", &num);
tmp = stnode;
if(fnNode == NULL) {
break;
}
else {
fnNode->num = num;
fnNode->nextptr = NULL;
tmp->nextptr = fnNode;
tmp = tmp->nextptr;
int NodeCount() {
int ctr = 0;
tmp = stnode;
while(tmp != NULL) {
ctr++;
tmp = tmp->nextptr;
return ctr;
}
void displayList() {
if(stnode == NULL) {
else {
tmp = stnode;
while(tmp != NULL) {
tmp = tmp->nextptr;
int main()
int n,totalNode;
scanf("%d", &n);
createNodeList(n);
printf("\n Data entered in the list are : \n");
displayList();
totalNode = NodeCount();
return 0;
OUTPUT
Input the number of nodes : 3
Data = 10
Data = 20
Data = 30
A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes
- Display
Solution
#include <stdlib.h>
#include <string.h>
typedef struct node
{
struct node *leftchild;
int info;
struct node *rightchild;
}
NODE;
NODE * get_node(int val)
{
NODE *p;
p = (NODE*)malloc(sizeof(NODE));
Name:
Class:
Roll No:
p->info=val;
p->leftchild = p->rightchild = NULL;
return p;
}
NODE* insert(NODE *h, int key)
{
NODE *p,*q;
q = get_node(key);
if(h==NULL)
h = q;
else
{
p = h;
while(1)
{
if(p->info>q->info)
{
if(p->leftchild==0)
{
p->leftchild = q;
break;
}
else
p=p->leftchild;
}
else
{
if(p->rightchild==0)
{
p->rightchild = q;
break;
}
else
p=p->rightchild;
}
}
}
return h;
}
NODE * create()
{
int i,n,key;
NODE *h=NULL;
printf("Enter no.of keys:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter key:");
scanf("%d",&key);
h = insert(h,key);
}
return h;
}
Name:
Class:
Roll No:
void inorder(NODE *h)
{
if(h!=NULL)
{
inorder(h->leftchild);
printf("%d\t",h->info);
inorder(h->rightchild);
}
}
int main()
{
NODE *root;
int ch,key;
while(1)
{
printf("1.Create\n");
printf("2.Insert\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("Enter your choice (1-6):");
scanf("%d",&ch);
switch(ch)
{
case 1:
root = create();
break;
case 2:
printf("Enter key to insert:");
scanf("%d",&key);
root=insert(root,key);
break;
case 3:
inorder(root);
printf("\n");
break;
case 4:
exit(0);
}
getch();
}
}
Output-
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):1
Enter no.of keys:2
Enter key:3
Enter key:4
1.Create
Name:
Class:
Roll No:
2.Insert
3.Display
4.Exit
Enter your choice (1-4):2
Enter key to insert:5
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):3
3 4 5
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):4
B) Write a ‘C’ program to accept and sort n elements in ascending order by using insertion sort.
Solution
#include <stdio.h>
int main(void)
int n, i, j, temp;
int arr[64];
scanf("%d", &n);
scanf("%d", &arr[i]);
j = i;
temp = arr[j];
arr[j - 1] = temp;
j--;
{
printf("%d\n", arr[i]);
return 0;
Output:-
Enter number of elements 5
Enter 5 integers
37295
23579
Slip 9
A) Write a program to accept a postfix expression and evaluate the expression
using the stack. Example: Input: ab+cd-* Values: a=4, b=2, c=5, d=3 Answer: 12
Solution:
#include<stdio.h>
#include<ctype.h>
int stack[20];
void push(int x)
stack[++top] = x;
int pop()
return stack[top--];
int main()
char exp[20];
char *e;
int n1,n2,n3,num;
scanf("%s",exp);
e = exp;
while(*e != '\0')
if(isdigit(*e))
num = *e - 48;
push(num);
else
n1 = pop();
n2 = pop();
switch(*e)
case '+':
n3 = n1 + n2;
break;
case '-':
n3 = n2 - n1;
break;
case '*':
{
n3 = n1 * n2;
break;
case '/':
n3 = n2 / n1;
break;
push(n3);
e++;
return 0;
Output-:
Enter the expression :: 55-55
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
return(n);
int i,x;
scanf("%d",&x);
head=cn();
head->data=x;
head->next=NULL;
temp=head;
else
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
void display()
while (temp!=NULL)
printf("%d\n",temp->data);
temp=temp->next;
void reverselist()
prev=head;
head=head->next;
cur=head;
prev->next=NULL;
while (head!=NULL)
head=head->next;
cur->next=prev;
prev=cur;
cur=head;
}
head=prev;
void main()
int n;
scanf("%d",&n);
createnode(n);
printf("Displaying list:\n");
display();
reverselist();
display();
}
Output-:
Enter size of Linked list
Displaying list:
Reverse list is :
2
Slip 10
A) Write a ‘C’ program to read ‘n’ integers and store them in a Binary search tree
and display the nodes level wise.
Solution:
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
temp->data = data;
return temp;
{
if(i<n)
root=temp;
root->left=insertLevelOrder(arr,root->left,(2*i)+1,n);
root->right=insertLevelOrder(arr,root->right,(2*i)+2,n);
return root;
int h = height(root);
int i;
printGivenLevel(root, i);
printf("\n");
}
}
if (root == NULL)
return;
if (level == 1)
printGivenLevel(root->left, level-1);
printGivenLevel(root->right, level-1);
if (node==NULL)
return 0;
else
return(lheight+1);
else return(rheight+1);
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
int main()
int n,arr[50],i;
scanf("%d", &n);
printf("\nEnter the Values to Create Binary tree\n");
scanf("%d",&arr[i]);
root=insertLevelOrder(arr,root,0,n);
printLevelOrder(root);
return 0;
Output-:
Enter size of tree: 1
12
12
2.
.
B) Write a ‘C’ program to sort randomly generated array elements using Insertion
sort method. (Use Random Function)
Solution:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void insertionsort(int [],int);
void array(int [],int);
int main()
{
int A[30],n,i;
printf("Enter size of array :\n");
scanf("%d",&n);
array(A,n);
insertionsort(A,n);
printf("\nSorted Array :\n");
for(i=0;i<n;i++)
{
printf("%d\n",A[i]);
}
}
void insertionsort(int A[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=A[i];
j=i-1;
Array :
546009555 633649643
Sorted Array :
546009555
633649643
Slip 11
A) Write a menu driven program using ‘C’ for singly linked list- - To create linked list.
- To display linked list - To search node in linked list. - Insert at last position
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
struct node* n;
return(n);
int i,n,x;
for(i=1;i<=n;i++)
scanf("%d",&x);
head=cn();
head->data=x;
head->next=NULL;
temp=head;
else
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
temp=head;
while(temp!=NULL)
printf("%d\n",temp->data);
temp=temp->next;
int pos,i=1,x;
scanf("%d",&pos);
scanf("%d",&x);
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp=head;
if(head==NULL) //checking in linked list is empty
head=newnode;
head->next=NULL;
if(pos==1)
newnode->next=head;
head=newnode;
else
while((i<pos-1)&&(temp!=NULL))
temp=temp->next;
i++;
}
if(temp==NULL)
else
newnode->next=temp->next;
temp->next=newnode;
printf("node inserted\n");
int key,i=1,flag=0;
scanf("%d",&key);
temp=head;
if(head==NULL)
else
while(temp!=NULL)
if(key==temp->data)
flag=1;
break;
temp=temp->next;
i++;
if(flag==0)
else
}
}
void main()
int choice,a=0;
while(choice!=5)
printf("4. Search.\n");
printf("5. Exit\n\n");
scanf("%d",&choice);
printf("\n");
switch(choice)
case 1: if(a==1)
else
{ createnode();
a++;
}
break;
case 2: traverse();
break;
case 3: insertnode();
break;
case 4: search();
break;
case 5: exit(0);
}
Output-:
1. Create Linked list.
4. Search.
5. Exit
4. Search.
5. Exit
4. Search.
5. Exit
B) Write a menu driven program using ‘C’ for Dynamic implementation of Queue for
integers. The menu includes - Insert - Delete - Display - Exit
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
return(n);
void createqueue()
int n,x,i;
for(i=1;i<=n;i++)
scanf("%d",&x);
if(front==NULL)
newnode=cn();
newnode->data=x;
newnode->next=NULL;
front=newnode;
rear=newnode;
else
newnode=cn();
newnode->data=x;
rear->next=newnode;
newnode->next=NULL;
rear=newnode;
}
void insert()
int x;
printf("Enter element:\n");
scanf("%d",&x);
newnode=cn();
newnode->data=x;
newnode->next=NULL;
if(front==NULL)
front=newnode;
rear=newnode;
else
rear->next=newnode;
rear=newnode;
void delete()
printf("Queue is empty.\n");
else
temp=front;
front=front->next;
temp->next=NULL;
free(temp);
void display()
temp=front;
printf("\n");
while(temp->next!=NULL)
printf("%d\n",temp->data);
temp=temp->next;
printf("%d\n",temp->data);
}
void main()
int choice,a=0;
while(choice!=5)
printf("2. Insert\n");
printf("3. Delete\n");
printf("4. Display\n");
printf("5. Exit\n\n");
scanf("%d",&choice);
switch(choice)
case 1: if(a==1)
else
createqueue();
a=1;
}
break;
case 2: insert();
break;
case 3: delete();
break;
case 4: display();
break;
case 5: exit(0);
}
Output-:
1. Create queue
2. Insert
3. Delete
4. Display
5. Exit
1. Create queue
2. Insert
3. Delete
4. Display
5. Exit
1. Create queue
2. Insert
3. Delete
4. Display
5. Exi
Slip 12
Write a C program that accepts the graph as an adjacency matrix and checks if the
graph is undirected. The matrix for an undirected graph is symmetric. Also calculate
in degree of all vertices - Read a graph as adjacency Matrix - Check the matrix is
symmetric or not - Calculate indegree of all vertices
Solution:
#include<stdio.h>
#include<stdlib.h>
int n,g[10][10];
void inoutdegree()
{
int i,j,id=0,od=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
od+=g[i][j];
id+=g[j][i];
}
printf("v%d indegree =%d \t Outdegree= %d\n",i,id,od);
}
}
int main()
{
int i,j,cnt=0;
printf("How many vertices:\n");
scanf("%d",&n);
printf("Enter matrix elements:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&g[i][j]);
}
}
printf("\nAdjacency matrix Is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(g[i][j]!=g[j][i])
cnt++;
printf("%d\t",g[i][j]);
}
printf("\n");
}
if(cnt!=0)
{
printf("\nGiven graph is Directed\n\n");
inoutdegree();
}
else
{
printf("Graph is undirected\n\n");
}
return 0;
}
Output-:
How many vertices:
12
22
2222
2222
3 2 2
2 12 2
22 2222 2222
Output-:
Enter size of array :
Sorted Array :
3
Slip 13
A) Write a C program to accept an infix expression and convert it into postfix form.
(Use Static Implementation of Stack) Example: - A * B + C as AB*C+
Solution:
#include<stdio.h>
#include<string.h>
char stack[SIZE];
printf("\nStack Overflow.");
}
else
top = top+1;
stack[top] = item;
char pop()
char item ;
if(top <0)
getchar();
exit(1);
else
item = stack[top];
top = top-1;
return(item);
}
/* define function that is used to determine whether any symbol is operator or not
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
return 1;
else
return 0;
return(3);
return(2);
return(1);
else
return(0);
int i, j;
char item;
char x;
push('('); /* push '(' onto stack */
i=0;
j=0;
if(item == '(')
push(item);
j++;
x=pop();
push(x);
postfix_exp[j] = x;
j++;
x = pop();
else
{ /* if current symbol is neither operand not '(' nor ')' and nor
operator */
getchar();
exit(1);
i++;
if(top>0)
getchar();
exit(1);
if(top>0)
getchar();
exit(1);
}
/* main function begins */
int main()
* in parentheses ( )
* in algorithm
* */
printf("ASSUMPTION: The infix expression contains single letter variables and single
digit constants only.\n");
gets(infix);
return 0;
}
Output-:
Enter Infix expression : 58-95
#include<stdio.h>
struct node
int data;
};
struct node * n;
return (n);
void createnode()
int i,n,x;
scanf("%d",&n);
scanf("%d",&x);
if(head==NULL)
head=cn();
head->data=x;
head->next=NULL;
head->prev=NULL;
temp=head;
else
newnode=cn();
newnode->data=x;
newnode->prev=temp;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
}
void display()
printf("\n");
if(head==NULL)
printf("List is empt.\n");
else
temp=head;
while(temp->next!=NULL)
if(temp->data%2==1)
printf("%d\n",temp->data);
temp=temp->next;
printf("%d\n",temp->data);
int main()
{
int choice,a=0;
while(choice!=4)
printf("2. Display.\n");
printf("3. Exit.\n\n");
scanf("%d",&choice);
switch(choice)
case 1: if(a==1)
else
{ createnode();
a++;
break;
case 2: display();
break;
case 3: exit(0);
defalut: printf("Enter a valid choice\n");
}
Output-:
1. Create Linked list.
2. Display.
3. Exit.
List is empt.
2. Display.
3. Exit.
2. Display.
3. Exit.
Slip 14
Q1 A : A) Write a ‘C’ program to accept a string from user and reverse it using Dynamic implementation
of Stack.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Stack structure
struct Stack {
int top;
unsigned capacity;
char* array;
};
stack->capacity = capacity;
stack->top = -1;
return stack;
}
// Stack is empty when the top is equal to -1
if (isFull(stack))
return;
stack->array[++stack->top] = item;
if (isEmpty(stack))
return '\0';
return stack->array[stack->top--];
int n = strlen(str);
int i;
for (i = 0; i < n; i++)
push(stack, str[i]);
// Pop all characters from the stack and put them back in the string
str[i] = pop(stack);
// Main function
int main() {
char str[100];
scanf("%s", str);
reverseString(str);
return 0;
Output
Enter a string: hello
namman
B)Write a ‘C’ program to accept names from the user and sort in alphabetical order using bubble sort -
Accept n name - Bubble sort Function - Display
#include <stdio.h>
#include <string.h>
int i, j;
char temp[50];
strcpy(temp, arr[j]);
strcpy(arr[j], arr[j+1]);
strcpy(arr[j+1], temp);
int main() {
int n, i;
char names[100][50];
scanf("%d", &n);
// Accepting names
for (i = 0; i < n; i++) {
scanf("%s", names[i]);
bubbleSort(names, n);
printf("%s\n", names[i]);
return 0;
Output-:
Enter the number of names: 2
guna
pune
Slip 15
Slip 15 - A) Write a ‘C’ program to accept an infix expression, convert it into its equivalent postfix
expression and display the result.(Use Dynamic Implementation of Stack)
Solution
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 50
struct Stack
int top;
unsigned capacity;
int* array;
};
malloc(sizeof(struct Stack));
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
sizeof(int));
return stack;
return stack->top == -1 ;
return stack->array[stack->top];
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
stack->array[++stack->top] = op;
switch (ch)
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
return -1;
int i, k;
if(!stack)
return -1 ;
{
if (isOperand(exp[i]))
exp[++k] = exp[i];
push(stack, exp[i]);
exp[++k] = pop(stack);
return -1;
else
pop(stack);
else
exp[++k] = pop(stack);
push(stack, exp[i]);
}
while (!isEmpty(stack))
exp[++k] = pop(stack );
exp[++k] = '\0';
int main()
char exp[MAX] ;
scanf("%s",exp);
infixToPostfix(exp);
return 0;
Output-:
ENter the EXpression
->a+b*c-d
=abc*+d-
Slip 15 - B) Write menu driven program using ‘C’ for Dynamic implementation of
Stack. The menu includes following operations: - Push - Pop - Display - Exit
Solution:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct stack
{
int data;
struct stack * next;
};
struct stack *top=NULL;
void push()
{
struct stack *newnode;
int x;
printf("Enter element of stack :\n");
scanf("%d",&x);
if(top==NULL)
{
newnode=cn();
newnode->data=x;
newnode->next=NULL;
top=newnode;
}
else
{
newnode=cn();
newnode->data=x;
newnode->next=top;
top=newnode;
}
printf("Element pushed successfully.\n");
}
void pop()
{
int pop,flag=0,item;
struct stack *temp;
if(top==NULL)
{
printf("Stack is empty.\n");
}
else
{
temp=top;
temp->data=item;
top=top->next;
free(temp);
flag=1;
}
if(flag==1)
{
printf("Element popped successfully.\n");
}
}
void display()
{
struct stack *temp;
printf("\n");
if(top==NULL)
{
printf("Stack is empty.\n");
}
else
{
temp=top;
while(temp->next!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
printf("%d\n",temp->data);
}
}
void main()
{
int choice;
while(choice!=4)
{
printf("\n\n1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("Enter a valid choice.\n");
}
}
}
Output-:
1. Push
2. Pop
3. Display
4. Exit
2
Stack is empty.
1. Push
2. Pop
3. Display
4. Exit
1
Enter element of stack :
2
Element pushed successfully.
1. Push
2. Pop
3. Display
4. Exit
Slip 16
A) Write a ‘C’ program which accept the string and reverse each word of the string using Static
implementation of stack. Example: Input - This is an input string Output - sihTsinatupnignirts
Solution:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int top=-1, i;
void reverse();
void main()
printf("Enter string:\n");
fgets(array1,sizeof(array1),stdin);
for(i=0;array1[i]!='\0';i++)
top=top+1;
stack[top]=array1[i];
reverse();
void reverse()
for(i=0;top>0;i++)
top=top-1;
array2[i]=stack[top];
}
Slip 16 - B) Write a ‘C’ program to create to a Singly linked list. Accept the number
from user, search the number in the list.If the number is present display the Position
of node .If number not present print the message “Number not Found”.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
struct node* head=NULL;
}
if(flag==0)
{
printf("Elemet not found.\n");
}
else
{
printf("Element found at position :%d",i);
}
}
void main()
{
int n,key;
printf("Enter size of Linked list :\n");
scanf("%d",&n);
createnode(n);
printf("\n\nLinked List :\n");
display();
printf("\nEnter element you want to search :\n");
scanf("%d",&key);
search(key);
}
Output-:
Enter size of Linked list :
2
Enter elements of linked list :
2
3
Linked List :
2
3
#include<stdio.h>
#include<ctype.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression : ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
Output-:
Enter the expression : 82+
The result of expression 82+ = 10
Slip 17 - B) Write a ‘C’ program to accept the names of cities and store them in
array. Accept the city name from user and use linear search algorithm to check
whether the city is present in array or not .
Solution:-
#include <stdio.h>
#include <string.h>
int main() {
char cities[MAX_SIZE][50];
int num_cities, i;
char search_city[50];
int found = 0;
if (found) {
printf("City %s found at position %d.\n", search_city, i + 1);
} else {
printf("City %s not found.\n", search_city);
}
return 0;
}
Output-:
Solution:
#include<stdio.h>
#include<stdlib.h>
struct binary_tree
int info;
};
NODE *node=NULL;
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->info = data;
return temp;
if(i<n)
NODE *temp=newnode(arr[i]);
root=temp;
root->left=insertLevelOrder(arr,root->left,(2*i)+1,n);
root->right=insertLevelOrder(arr,root->right,(2*i)+2,n);
return root;
if(node==NULL)
return 0;
else
return 1+count(node->left)+count(node->right);
if(node == NULL)
return 0;
return 1;
else
return getLeafCount(node->left)+
getLeafCount(node->right);
if(node==NULL)
printf("Tree is empty\n");
return 0;
return 2;
return 1;
int main()
{
int data,ch,i,n,arr[50],count1,count2,count3;
NODE *root=NULL;
while (ch!=5)
printf("\n3.Degree of tree");
printf("\n4.Leaf nodes");
printf("\n5.Exit");
scanf("%d", &ch);
switch (ch)
scanf("%d", &n);
scanf("%d",&arr[i]);
root=insertLevelOrder(arr,root,0,n);
break;
case 2: count1=count(root);
case 3: count2=degree(root);
break;
case 4: count3=getLeafCount(root);
break;
case 5: exit(0);
break;
}
Output-:
1.Insertion in Binary Search Tree
3.Degree of tree
4.Leaf nodes
5.Exit
23
3.Degree of tree
4.Leaf nodes
5.Exit
2
Slip 18 - B) Write a ‘C’ program to accept and sort n elements in ascending order
using Merge sort method.
Solution:
#include<stdio.h>
void mergeSort(int [],int,int,int);
void partition(int [],int,int);
void main()
{
int A[50],n,i;
printf("Enter size of Array :\n");
scanf("%d",&n);
printf("Enter elements of array :\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
partition(A,0,n-1);
printf("Sorted Array :\n");
for(i=0;i<n;i++)
{
printf("%d\n",A[i]);
}
}
void partition(int A[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(A,low,mid);
partition(A,mid+1,high);
mergeSort(A,low,mid,high);
}
}
void mergeSort(int A[],int low,int mid,int high)
{
int i,m,k,l,temp[50];
l=i=low;
m=mid+1;
while((l<=mid)&&(m<=high))
{
if(A[l]<=A[m])
{
temp[i]=A[l];
l++;
}
else
{
temp[i]=A[m];
m++;
}
i++;
}
while(l<=mid)
{
temp[i]=A[l];
l++;
i++;
}
while(m<=high)
{
temp[i]=A[m];
i++;
m++;
}
for(k=low;k<=high;k++)
{
A[k]=temp[k];
}
}
Output-:
Enter size of Array :
Sorted Array :
5
Slip 19
Slip 19 - A) Write a ‘C’ program which accept the string and reverse each word of the string using
Dynamic implementation of stack. Example: Input - This is an input string Output - sihTsinatupnignirts
Solution:
#include <stdio.h>
#include <string.h>
char stack[50];
char array1[50];
char array2[50];
void reverse();
int main()
printf("Enter string:\n");
top = top + 1;
stack[top] = array1[i];
reverse();
puts(array2);
return 0;
}
void reverse()
int j = 0;
array2[j] = stack[i];
j++;
Output-:
Enter string:
hello I am Abhi
Reversed String :
ihbA ma I olleh
Slip 19 - B) Write a ‘C’ program to create a singly Link list and display its alternative
nodes. (start displaying from first node)
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
struct node* head=NULL;
void main()
{
createnode();
printf("Displaying alternative nodes.\n");
display();
}
Output-:
Enter size of linked list :
3
Enter elements of linked list :
1
2
3
Displaying alternative nodes.
1
3
Segmentation fault
1
Slip 20
Slip 20 - A) Write a ‘C’ program which accept the string and check whether the
string is Palindrome or not using stack. (Use Static/Dynamic implementation of
Stack)
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
int stack[MAX];
void push(char);
void pop();
void push(char a)
top++;
stack[top] = a;
void pop()
top--;
int main()
{
int i, cho;
char s[MAX], b;
scanf("%s", s);
b = s[i];
push(b);
if (stack[top] == stack[front])
pop();
front++;
else
{
printf("'%s' is palindrome.\n\n", s);
front = 0;
top = -1;
return 0;
Output-:
Enter string : level
'level' is palindrome.
Enter string : hello
'hello' is not palindrome.
Slip 20 - B) Write a ‘C’ program to swap mth and nth element of singly linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start=NULL;
void creat(int);
void swap();
void disp();
int main()
{
int ch,i,n,m;
do
{
printf("\n\n1.Create Linked list");
printf("\n2.Display");
printf("\n3.Swap");
printf("\n4.Exit\n");
printf("\nEnter your choice :\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter number of nodes :\n");
scanf("%d",&n);
printf("\nEnter elements of Linked list:\n");
for(i=0;i<n;i++)
{
scanf("%d",&m);
creat(m);
}
break;
case 2:
disp();
break;
case 3:
swap();
break;
case 4:
exit(0);
}
}while(ch!=4);
return 0;
}
void creat(int m)
{
struct list *tmp,*q;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
void disp()
{
struct list *q;
if(start==NULL)
{
printf("list is empty");
}
else
{
q=start;
printf("\n");
while(q!=NULL)
{
printf("%d\n",q->data);
q=q->link;
}
}
}
void swap()
{
int m,n,i,tmp;
struct list *q,*ptr,*ptr1;
printf("\nEnter the mth and nth position:\n");
scanf("%d%d",&m,&n);
for(i=1,ptr=start;i<m && ptr!=NULL;ptr=ptr->link,i++);
for(i=1,ptr1=start;i<n && ptr1!=NULL;ptr1=ptr1->link,i++);
if(ptr!=NULL && ptr1!=NULL)
{
tmp=ptr->data;
ptr->data=ptr1->data;
ptr1->data=tmp;
}
else
{
printf("\nPosition Not Found");
}
}
Output-:
1.Create Linked list
2.Display
3.Swap
4.Exit
struct queue {
int items[SIZE];
int front;
int rear;
};
struct node {
int vertex;
struct node* next;
};
struct Graph {
int numVertices;
struct node** adjLists;
int* visited;
};
// BFS algorithm
void bfs(struct Graph* graph, int startVertex) {
struct queue* q = createQueue();
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
int currentVertex = dequeue(q);
printf("\nVisited %d\n", currentVertex);
while (temp) {
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
}
// Creating a node
struct node* createNode(int v) {
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
// Creating a graph
struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
void addEdge(struct Graph* graph,int graph_type)
{
int i,max_edges,u,v;
if(graph_type==1)
{
max_edges = n*(n-1)/2;
}
else
{
max_edges = n*(n-1);
}
for(i=1;i<=max_edges;i++)
{
printf("Enter edge [%d] (-1 -1 to quit): ",i);
scanf("%d%d",&u,&v);
if((u==-1)&&(v==-1))
{
break;
}
else if(u>=n || v>=n || u<0 || v<0)
{
printf("Invalid vertex\n");
i--;
}
else
{
// Add edge from s to d
struct node* newNode = createNode(v);
newNode->next = graph->adjLists[u];
graph->adjLists[u] = newNode;
if(graph_type==1)
{ // Add edge from d to s
newNode = createNode(u);
newNode->next = graph->adjLists[v];
graph->adjLists[v] = newNode;
}
}
}
}
// Create a queue
struct queue* createQueue() {
struct queue* q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}
if (isEmpty(q)) {
printf("Queue is empty");
} else {
printf("\nQueue contains \n");
for (i = q->front; i < q->rear + 1; i++) {
printf("%d ", q->items[i]);
}
}
}
int main()
{
int graph_type;
printf("\nEnter 1 for Undirected graph\nEnter 2 for Directed graph\n");
printf("\nEnter your choice :: ");
scanf("%d",&graph_type);
printf("Enter number of vertices :\n");
scanf("%d",&n);
bfs(graph, 0);
return 0;
}
Output-:
Enter 1 for Undirected graph
Enter 2 for Directed graph
void main() {
int arr[100], n, i, search;
if(i == n) {
printf("\n%d not found in the array.\n",search);
}
}
Output-:
Enter the number of elements you want in the array: 3
Enter value for index 0: 1
Enter value for index 1: 2
Enter value for index 2: 3
Enter the number you want to search in the array: 2
2 found at 1 position.
Slip 22
A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes
- Display
Solution.
#include <stdlib.h>
#include <string.h>
typedef struct node
{
struct node *leftchild;
int info;
struct node *rightchild;
}
NODE;
NODE * get_node(int val)
{
NODE *p;
p = (NODE*)malloc(sizeof(NODE));
Name:
Class:
Roll No:
p->info=val;
p->leftchild = p->rightchild = NULL;
return p;
}
NODE* insert(NODE *h, int key)
{
NODE *p,*q;
q = get_node(key);
if(h==NULL)
h = q;
else
{
p = h;
while(1)
{
if(p->info>q->info)
{
if(p->leftchild==0)
{
p->leftchild = q;
break;
}
else
p=p->leftchild;
}
else
{
if(p->rightchild==0)
{
p->rightchild = q;
break;
}
else
p=p->rightchild;
}
}
}
return h;
}
NODE * create()
{
int i,n,key;
NODE *h=NULL;
printf("Enter no.of keys:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter key:");
scanf("%d",&key);
h = insert(h,key);
}
return h;
}
Name:
Class:
Roll No:
void inorder(NODE *h)
{
if(h!=NULL)
{
inorder(h->leftchild);
printf("%d\t",h->info);
inorder(h->rightchild);
}
}
int main()
{
NODE *root;
int ch,key;
while(1)
{
printf("1.Create\n");
printf("2.Insert\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("Enter your choice (1-6):");
scanf("%d",&ch);
switch(ch)
{
case 1:
root = create();
break;
case 2:
printf("Enter key to insert:");
scanf("%d",&key);
root=insert(root,key);
break;
case 3:
inorder(root);
printf("\n");
break;
case 4:
exit(0);
}
getch();
}
}
Output-
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):1
Enter no.of keys:2
Enter key:3
Enter key:4
1.Create
Name:
Class:
Roll No:
2.Insert
3.Display
4.Exit
Enter your choice (1-4):2
Enter key to insert:5
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):3
3 4 5
1.Create
2.Insert
3.Display
4.Exit
Enter your choice (1-4):4
B) Write a ‘C’ program to accept and sort n elements in ascending order by using insertion sort.
Solution
#include <stdio.h>
int main(void)
int n, i, j, temp;
int arr[64];
scanf("%d", &n);
scanf("%d", &arr[i]);
j = i;
temp = arr[j];
arr[j - 1] = temp;
j--;
printf("%d\n", arr[i]);
}
return 0;
Output:-
Enter 5 integers
37295
23579
Slip 23
A) Write a menu driven program using ‘C’ for singly linked list- - To create linked list. - To display linked
list - To insert node at last position of linked list. - To delete node from specific position of linked list.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
struct node* n;
return(n);
int i,n,x;
for(i=1;i<=n;i++)
scanf("%d",&x);
head=cn();
head->data=x;
head->next=NULL;
temp=head;
else
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
temp=head;
while(temp!=NULL)
printf("%d\n",temp->data);
temp=temp->next;
int pos,i=1,x;
scanf("%d",&pos);
scanf("%d",&x);
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp=head;
if(head==NULL) //checking in linked list is empty
head=newnode;
head->next=NULL;
if(pos==1)
newnode->next=head;
head=newnode;
else
while((i<pos-1)&&(temp!=NULL))
temp=temp->next;
i++;
}
if(temp==NULL)
else
newnode->next=temp->next;
temp->next=newnode;
printf("node inserted\n");
int pos,i=1,x;
scanf("%d",&pos);
temp=head;
if(head==NULL)
else if(pos==1)
head=head->next;
free(temp);
else
temp=temp->next;
i++;
if(temp!=NULL)
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
else
}
}
void main()
int choice,a=0;
while(choice!=5)
printf("5. Exit\n\n");
scanf("%d",&choice);
printf("\n");
switch(choice)
case 1: if(a==1)
else
{ createnode();
a++;
}
break;
case 2: traverse();
break;
case 3: insertnode();
break;
case 4: deletenode();
break;
case 5: exit(0);
}
output:-
B) Write a ‘C’ program to create a random array of n integers. Accept a value x from user and use Binary
search algorithm to check whether the number is present in array or not. (Students can accept sorted
array or can use any sorting method to sort the array)
Solution:
#include <stdio.h>
int main()
scanf("%d",&n);
scanf("%d",&array[i]);
low = 0;
high = n - 1;
mid = (low+high)/2;
low = mid + 1;
break;
else
high = mid - 1;
return 0;
}
output:-
Enter number of elements:
Enter 2 integers :
3 found at location 1.
Slip 24
A) Write a ‘C’ program to read a postfix expression, evaluate it and display the
result. (Use Dynamic Implementation of Stack)
Solution:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct Node {
int data;
};
new_node->data = val;
new_node->next = top;
top = new_node;
int pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return -1;
top = top->next;
free(temp);
return val;
int i;
if (isdigit(exp[i]))
push(exp[i] - '0');
else {
switch (exp[i]) {
case '+':
push(val2 + val1);
break;
case '-':
push(val2 - val1);
break;
case '*':
push(val2 * val1);
break;
case '/':
push(val2 / val1);
break;
case '^':
push(pow(val2, val1));
break;
return pop();
int main() {
char exp[100];
scanf("%s", exp);
return 0;
}
Output-:
Enter the postfix expression: 53+
Result: 8
B) Write a ‘C’ program to remove last node of the singly linked list and insert it at
the beginning of list .
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
};
struct node* head=NULL;
temp=head;
if(head==NULL)
{
printf("Linked list is empty. \n");
}
else
{
while((i<n-1) && (temp!=NULL))
{
temp=temp->next;
i++;
}
if(temp!=NULL)
{
temp1=temp->next;
temp->next=NULL;
}
}
temp1->next=head;
head=temp1;
}
int main()
{
int choice,a=0;
if(a==1)
{
printf("You have already created a Linked list.");
}
else
{ createnode();
a++;
}
deletenode();
traverse();
}
Output-:
Enter size of linked list :
2
Slip 25
A) Write a ‘C’ program to read an adjacency matrix of a directed graph and traverse
it using DFS .
Solution:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
int n;
struct node {
int vertex;
};
struct Graph
int numVertices;
int* visited;
};
// Creating a node
struct node* createNode(int v) {
newNode->vertex = v;
newNode->next = NULL;
return newNode;
// Creating a graph
graph->numVertices = vertices;
int i;
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
return graph;
int i,max_edges,u,v;
max_edges=n*(n-1)/2;
for(i=1;i<=max_edges;i++)
scanf("%d%d",&u,&v);
if((u==-1)&&(v==-1))
break;
printf("Invalid vertex\n");
i--;
else
newNode->next = graph->adjLists[u];
graph->adjLists[u] = newNode;
newNode = createNode(u);
newNode->next = graph->adjLists[v];
graph->adjLists[v] = newNode;
graph->visited[vertex] = 1;
if (graph->visited[connectedVertex] == 0)
DFS(graph, connectedVertex);
}
temp = temp->next;
int v;
while (temp)
temp = temp->next;
printf("\n");
int main()
scanf("%d",&n);
addEdge(graph);
printGraph(graph);
DFS(graph, 0);
return 0;
Output-:
Enter number of vertices :
2 -> 1 ->
2 -> 1 ->
Visited 0
Visited 2
Visited 3
Visited 1
B) Write a menu driven program using ‘C’ for singly linked list- - To create linked list.
- To display linked list
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
struct node* n;
return(n);
int i,n,x;
scanf("%d",&n);
printf("Enter elements of linked list :\n");
for(i=1;i<=n;i++)
scanf("%d",&x);
head=cn();
head->data=x;
head->next=NULL;
temp=head;
else
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
{
struct node * temp;
temp=head;
while(temp!=NULL)
printf("%d\n",temp->data);
temp=temp->next;
void main()
int choice,a=0;
while(choice!=3)
printf("3. Exit\n\n");
scanf("%d",&choice);
printf("\n");
switch(choice)
{
case 1: if(a==1)
else
{ createnode();
a++;
break;
case 2: traverse();
break;
case 3: exit(0);
}
Output-:
1. Create Linked list.
3. Exit
3. Exit
3. Exit
Slip 26
A) Write a ‘C’ program to accept an infix expression, convert it into its equivalent
prefix expression and display the result. (Use Static Implementation of Stack).
Solution:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include <stdlib.h>
#define MAX 20
void push(int);
char pop();
void infix_to_prefix();
char stack[20],infix[20],prefix[20];
int main()
scanf("%s",infix);
infix_to_prefix();
return 0;
if(top == MAX-1)
printf("\nSTACK OVERFLOW\n");
else {
top++;
stack[top] = infix[pos];
}}
char pop()
char ch;
if(top < 0)
printf("\nSTACK UNDERFLOW\n");
exit(0);
else
ch = stack[top];
stack[top] = '\0';
top--;
return(ch);
}
return 0;
void infix_to_prefix()
int i = 0,j = 0;
strrev(infix);
while(infix[i] != '\0')
prefix[j] = infix[i];
j++;
i++;
push(i);
i++;
if(infix[i] == '(')
while(stack[top] != ')')
prefix[j] = pop();
j++;
pop();
i++;
while(stack[top] != ']')
prefix[j] = pop();
j++;
pop();
i++;
while(stack[top] != '}')
{
prefix[j] = pop();
j++;
pop();
i++;
}}
else
if(top == -1)
push(i);
i++;
prefix[j] = pop();
j++;
prefix[j] = pop();
j++;
if(top < 0) {
break;
}}
push(i);
i++;
push(i);
i++;
}}}
while(top != -1)
prefix[j] = pop();
j++;
strrev(prefix);
prefix[j] = '\0';
return(1);
return(2);
return 0;
Output-:
Enter the infix expression: (A+B)*C-D/E
(A+B)*C-D/E
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
struct node * n;
return(n);
{
int x,i=1;
for(i=1;i<=n;i++)
scanf("%d",&x);
if(*head==NULL)
newnode=cn();
newnode->data=x;
newnode->next=NULL;
*head=newnode;
temp=*head;
else
newnode=cn();
newnode->data=x;
newnode->next=NULL;
temp->next=newnode;
temp=newnode;
}
}
void concatination()
int i;
temp1=head1;
temp2=head2;
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=head2;
traverse(&head1);
temp3=*head;
while(temp3!=NULL)
printf("%d\n",temp3->data);
temp3=temp3->next;
int main()
int h1,h2,choice;
scanf("%d",&h1);
createnode(&head1,h1);
scanf("%d",&h2);
createnode(&head2,h2);
concatination();
}
Output-:
Enter size of first linked list :
9
Slip 27
A) Implement Static implementation of circular queue of integers with following
operation: - Initialize(),insert(), delete(), isempty(), isfull(), display()
Solution:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
# define maxsize 5
void insert();
void delete();
void display();
int front=-1,rear=-1;
int queue[maxsize];
void main()
int choice;
while(choice!=4)
printf("\n4. Exit\n");
printf("\nEnter your choice :\n");
scanf("%d",&choice);
switch(choice)
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
void insert()
int item;
scanf("%d",&item);
if((rear+1)%maxsize==front)
printf("\nOverflow");
return ;
else if((front==-1)&&(rear==-1))
front=rear=0;
else if((rear==maxsize-1)&&(front!=0))
rear=0;
else
rear=(rear+1)%maxsize;
queue[rear]=item;
void delete()
int item;
if((front==-1)&&(rear==-1))
printf("Underflow");
return ;
else if(front==rear)
front=-1;
rear=-1;
else
front=front+1;
void display()
int i;
if(front==-1)
else
{
i=front;
if(front<=rear)
while(i<=rear)
printf("%d \n",queue[i]);
i++;
else
while(i<=maxsize-1)
printf("%d \n",queue[i]);
i++;
i=0;
while(i<=rear)
printf("%d \n",queue[i]);
i++;
}
}
Output-:
1. Insert an element
2. Delete an element
3. Display Queue
4. Exit
Enter element :
1. Insert an element
2. Delete an element
3. Display Queue
4. Exit
2
B) Write a ‘C’ program to create Doubly Link list and display it.
Solution:
#include<stdlib.h>
#include<stdio.h>
struct node
int data;
};
struct node * n;
return (n);
void createnode()
int i,n,x;
scanf("%d",&n);
scanf("%d",&x);
if(head==NULL)
head=cn();
head->data=x;
head->next=NULL;
head->prev=NULL;
temp=head;
else
newnode=cn();
newnode->data=x;
newnode->prev=temp;
newnode->next=NULL;
temp->next=newnode;
head->prev=NULL;
temp=newnode;
}
void display()
printf("\n");
if(head==NULL)
printf("List is empt.\n");
else
temp=head;
while(temp->next!=NULL)
printf("%d\n",temp->data);
temp=temp->next;
printf("%d\n",temp->data);
int main()
{
int choice,a=0;
while(choice!=3)
printf("2. Display.\n");
printf("3. Exit.\n\n");
scanf("%d",&choice);
switch(choice)
case 1: if(a==1)
else
{ createnode();
a++;
break;
case 2: display();
break;
case 4: exit(0);
defalut: printf("Enter a valid choice\n");
Output-:
1. Create Linked list.
2. Display.
3. Exit.
2. Display.
3. Exit.
2. Display.
3. Exit.
Slip 28
A) Write a ‘C’ program to create a Binary Search tree and implements following
functions - Create Binary Search Tree - Search a node in binary search tree - Display
a binary search tree (Post order Traversal)
Solution:
#include <stdio.h>
#include <stdlib.h>
struct BST
int data;
};
NODE *node;
if (node == NULL)
{
NODE *temp;
temp= (NODE*)malloc(sizeof(NODE));
temp->data = data;
return temp;
return node;
if(node == NULL)
printf("\nElement Not Found...!/n");
node->left=search(node->left, data);
node->right=search(node->right, data);
else
return node;
if(node != NULL)
postorder(node->left);
postorder(node->right);
printf("%d\t", node->data);
int main()
NODE *root=NULL;
while (ch!=4)
printf("\n3.Postorder");
printf("\n4.Exit\n");
scanf("%d", &ch);
switch (ch)
scanf("%d", &n);
scanf("%d", &data);
root=createtree(root, data);
break;
scanf("%d", &data);
root=search(root, data);
break;
postorder(root);
break;
case 4: exit(0);
break;
}
Output-:
1.Insertion in Binary Search Tree
3.Postorder
4.Exit
3.Postorder
4.Exit
Postorder Traversal:
4 3 2
3.Postorder
4.Exit
B) Write a ‘C’ program to read n integers and create two lists such that all positive
numbers are in one list and negative numbers are in another list. Display both the
lists.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
struct node * n;
return(n);
{
int i;
for(i=0;i<n;i++)
if(arr[i]>1)
if(head1==NULL)
newnode1=cn();
newnode1->data=arr[i];
newnode1->next=NULL;
head1=newnode1;
temp1=head1;
else
newnode1=cn();
newnode1->data=arr[i];
newnode1->next=NULL;
temp1->next=newnode1;
temp1=newnode1;
else
{
if(head2==NULL)
newnode2=cn();
newnode2->data=arr[i];
newnode2->next=NULL;
head2=newnode2;
temp2=head2;
else
newnode2=cn();
newnode2->data=arr[i];
newnode2->next=NULL;
temp2->next=newnode2;
temp2=newnode2;
temp3=*head;
while(temp3!=NULL)
printf("%d\n",temp3->data);
temp3=temp3->next;
int main()
int arr[100],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
createnode(arr,n);
traverse(&head1);
traverse(&head2);
}
Output-:
Enter number of elements.
1
Slip 29
A) Write a ‘C’ program to create a Binary tree, traverse it using recursive operations
like inorder, preorder and postorder and display the result of each one separately.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct binary_tree
int info;
};
NODE *node=NULL;
root->info = data;
return root;
{
NODE* node = (NODE*) malloc (sizeof(NODE));
node->info = data;
return node;
if(node != NULL)
inorder(node->left);
printf("%d\t", node->info);
inorder(node->right);
if(node != NULL)
printf("%d\t", node->info);
preorder(node->left);
preorder(node->right);
}
if(node != NULL)
postorder(node->left);
postorder(node->right);
printf("%d\t", node->info);
void main()
root->left = create_node(20);
root->right = create_node(30);
root->left->left = create_node(40);
root->left->right = create_node(50);
root->right->left = create_node(60);
root->right->right = create_node(70);
printf("----Preorder Traversal:----\n");
preorder(root);
printf("\n\n");
printf("----Inorder Traversal:----\n");
inorder(root);
printf("\n\n");
printf("----Postorder Traversal:----\n");
postorder(root);
printf("\n\n");
Output-:
----Preorder Traversal:----
10 20 40 50 30 60 70
----Inorder Traversal:----
40 20 50 10 60 30 70
--Postorder Traversal:----
40 50 20 60 70 30 10
B) Write a ‘C’ program to create Circular Singly Link list and display it.
Solution:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
last = newNode;
return last;
newNode->data = data;
newNode->next = last->next;
last->next = newNode;
return last;
}
// add node to the end
newNode->data = data;
newNode->next = last->next;
last->next = newNode;
last = newNode;
return last;
p = last->next;
do {
if (p->data == item) {
newNode->data = data;
newNode->next = p->next;
p->next = newNode;
return last;
}
p = p->next;
} while (p != last->next);
return last;
// delete a node
free(*last);
*last = NULL;
return;
// if last is to be deleted
if ((*last)->data == key) {
temp->next = (*last)->next;
free(*last);
*last = temp->next;
temp = temp->next;
if (temp->next->data == key) {
d = temp->next;
temp->next = d->next;
free(d);
struct Node* p;
if (last == NULL) {
printf("The list is empty");
return;
p = last->next;
do {
p = p->next;
} while (p != last->next);
int main() {
traverse(last);
deleteNode(&last, 8);
printf("\n");
traverse(last);
return 0;
Output-:
2 10 6 8
10 6 2
Slip 30
A) Implement Dynamic implementation of circular queue of integers with following
operation: - Initialize(),insert(), delete(), isempty(), isfull(), display()
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
struct node* n;
n->data = d;
n->next = NULL;
if((r==NULL)&&(f==NULL))
f = r = n;
r->next = f;
else
{
r->next = n;
r = n;
n->next = f;
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
f = r = NULL;
free(t);
else{
f = f->next;
r->next = f;
free(t);
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else{
do{
printf("\n%d",t->data);
t = t->next;
}while(t != f);
int main()
int opt,n,i,data;
do{
printf("\n\n1 Insert the Data in Queue\n2 show the Data in Queue \n3 Delete
the data from the Queue\n4 Exit \n");
scanf("%d",&opt);
switch(opt){
case 1:
scanf("%d",&data);
enqueue(data);
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 4:
exit (0);
default:
printf("\nIncorrect Choice");
}while(opt!=4);
return 0;
Output-:
Enter Your Choice:-
4 Exit
2225
1 Insert the Data in Queue
4 Exit
2225
4 Exit
B) Write a ‘C’ program to sort elements of a singly linked list in ascending order and
display the sorted List.
Solution:
// Write a 'C' program to sort elements of a singly linked list in ascending order and display
the sorted List.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
};
void append()
scanf("%d", &temp->data);
temp->link = NULL;
if(root == NULL) {
root = temp;
else {
struct node* p;
p = root;
while(p->link != NULL) {
p = p->link;
p->link = temp;
void display() {
temp = root;
if(temp == NULL) {
printf("\nLIST IS EMPTY!!\n");
else {
printf("\n");
while(temp != NULL) {
printf("%d\t", temp->data);
temp = temp->link;
printf("\n");
void sort() {
int temp;
temp = p->data;
p->data = q->data;
q->data = temp;
void main() {
int n, i;
append();
display();
sort();
display();
Output-:
Enter the number of nodes: 2
2 2
2 2