0% found this document useful (0 votes)
8 views272 pages

C Slip 16

The document is a practical work certificate from Alard College of Business Studies for students completing various programming tasks in C. It includes a list of programming assignments, such as creating binary search trees, linked lists, and evaluating polynomials. Each assignment is detailed with specific tasks and sample code implementations.

Uploaded by

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

C Slip 16

The document is a practical work certificate from Alard College of Business Studies for students completing various programming tasks in C. It includes a list of programming assignments, such as creating binary search trees, linked lists, and evaluating polynomials. Each assignment is detailed with specific tasks and sample code implementations.

Uploaded by

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

ALARD COLLEGE OF BUSINESS STUDIES

CERTIFICATE

This is to certify that Mr/Ms . _________________________________________________of

Class ____________________has completed his/her practical work for subject

___________________as a part of curriculum by Alard College of Business Studies ,Savitribai

Phule Pune University, Pune during the Academic year [ ]

--------------------------- -----------------------------
Signature of Examiner Signature of Examiner
(External) (internal)

------------------------ Date of Examination


(Principal) ---------------------------

--------------------- ----------------------
(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.

28 A) Write a ‘C’ program to create a Binary Search tree and


implements following functions-
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.
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.
B) Write a ‘C’ program to create Circular Singly Link list
and display it.
30 A)Implement Dynamic implementation of circular queue
of integers with following operation:
B) Write a ‘C’ program to sort elements of a singly linked
list in ascending order and display the sorted List.
Slip No 1
Q1) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes

- Create a Binary Search Tree

- Insert element in a Binary Search Tree

- Display

ANS=

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

struct Node* createNode(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

struct Node* insertNode(struct Node* root, int value) {

if (root == NULL) {

return createNode(value);

if (value < root->data) {

root->left = insertNode(root->left, value);

} else if (value > root->data) {


root->right = insertNode(root->right, value);

return root;

void displayTree(struct Node* root) {

if (root != NULL) {

displayTree(root->left);

printf("%d ", root->data);

displayTree(root->right);

int main() {

struct Node* root = NULL;

int choice, value;

do {

printf("\nBinary Search Tree Menu\n");

printf("1. Create a Binary Search Tree\n");

printf("2. Insert element in a Binary Search Tree\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch(choice) {

case 1:
root = NULL;

printf("Binary Search Tree created.\n");

break;

case 2:

printf("Enter the element to be inserted: ");

scanf("%d", &value);

root = insertNode(root, value);

printf("Element inserted.\n");

break;

case 3:

printf("Binary Search Tree: ");

displayTree(root);

printf("\n");

break;

case 4:

printf("Exiting the program.\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

} while (choice != 4);

return 0;

OUTPUT=
Binary Search Tree Menu

1. Create a Binary Search Tree

2. Insert element in a Binary Search Tree


3. Display

4. Exit

Enter your choice: 1

Binary Search Tree created.

Binary Search Tree Menu

1. Create a Binary Search Tree

2. Insert element in a Binary Search Tree

3. Display

4. Exit

Enter your choice: 2

Enter the element to be inserted: 10

Element inserted.

Binary Search Tree Menu

1. Create a Binary Search Tree

2. Insert element in a Binary Search Tree

3. Display

4. Exit

Enter your choice: 3

Binary Search Tree: 10

Binary Search Tree Menu

1. Create a Binary Search Tree

2. Insert element in a Binary Search Tree

3. Display

4. Exit

Enter your choice:4

Exiting the program.


Q2) Write a ‘C’ program to evaluate a given polynomial using function. (Use array).

ANS=

#include <stdio.h>

double evaluatePolynomial(int degree, double coefficients[], double x) {

double result = 0;

int i;

for (i = degree; i >= 0; i--) {

result = result * x + coefficients[i];

return result;

int main() {

int degree, i;

printf("Enter the degree of the polynomial: ");

scanf("%d", &degree);

double coefficients[degree+1];

for (i = degree; i >= 0; i--) {

printf("Enter the coefficient of x^%d: ", i);

scanf("%lf", &coefficients[i]);

}
double x;

printf("Enter the value of x: ");

scanf("%lf", &x);

double result = evaluatePolynomial(degree, coefficients, x);

printf("The value of the polynomial at x = %.2lf is %.2lf\n", x, result);

return 0;

OUTPUT=
Enter the degree of the polynomial: 3

Enter the coefficient of x^3:


Slip No 2

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>

#define MAX_SIZE 100

struct Stack {
int top;
char data[MAX_SIZE];
};

void initialize(struct Stack* stack) {


stack->top = -1;
}

int isEmpty(struct Stack* stack) {

return stack->top == -1;

int isFull(struct Stack* stack) {


return stack->top == MAX_SIZE - 1;
}

void push(struct Stack* stack, char element) {


if (isFull(stack)) {
printf("Stack overflow\n");
return;
}
stack->data[++stack->top] = element;
}

char pop(struct Stack* stack) {


if (isEmpty(stack)) {
printf("Stack underflow\n");
return '\0';
}
return stack->data[stack->top--];
}

void reverseString(char* str) {


struct Stack stack;
initialize(&stack);

int length = strlen(str);


for (int i = 0; i < length; i++) {
push(&stack, str[i]);
}
for (int i = 0; i < length; i++) {
str[i] = pop(&stack);
}
}

int main() {
char str[MAX_SIZE];

printf("Enter a string: ");


fgets(str, MAX_SIZE, stdin);

Remove newline character from fgets


if (str[strlen(str) - 1] == '\n') {
str[strlen(str) - 1] = '\0';
}
reverseString(str);
printf("Reversed string: %s\n", str);
return 0;
}

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

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

void insertAtEnd(struct Node** head, int data) {


struct Node* newNode = createNode(data);

if (*head == NULL) {
*head = newNode;
(*head)->prev = *head;
(*head)->next = *head;
return;

struct Node* last = (*head)->prev;


last->next = newNode;
newNode->prev = last;
newNode->next = *head;
(*head)->prev = newNode;
}

void display(struct Node* head) {


if (head == NULL) {
printf("Circular Doubly Linked List is empty.\n");
return;
}

struct Node* current = head;

printf("Circular Doubly Linked List: ");


do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}

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

Circular Doubly Linked List: 1 2 3 4 5


1
dash: 2: 1: not found
2
dash: 3: 2: not found
12345
dash: 4: 1: not found
Slip No 3

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

void insert(struct LinkedList* list, int data) {


struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = NULL;

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

void display(struct LinkedList* list) {


struct Node* current = list->head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

struct LinkedList* find_union(struct LinkedList* list1, struct LinkedList* list2) {


struct LinkedList* union_list = (struct LinkedList*)malloc(sizeof(struct
LinkedList));
union_list->head = NULL;

struct Node* current1 = list1->head;


struct Node* current2 = list2->head;
while (current1 != NULL && current2 != NULL) {
if (current1->data < current2->data) {
insert(union_list, current1->data);
current1 = current1->next;
} else if (current1->data > current2->data) {
insert(union_list, current2->data);
current2 = current2->next;
} else {
insert(union_list, current1->data);
current1 = current1->next;
current2 = current2->next;
}
}

while (current1 != NULL) {


insert(union_list, current1->data);
current1 = current1->next;
}

while (current2 != NULL) {


insert(union_list, current2->data);
current2 = current2->next;
}
return union_list;
}

int main() {
struct LinkedList* list1 = (struct LinkedList*)malloc(sizeof(struct LinkedList));
list1->head = NULL;

insert(list1, 1);
insert(list1, 3);
insert(list1, 5);

struct LinkedList* list2 = (struct LinkedList*)malloc(sizeof(struct LinkedList));


list2->head = NULL;

insert(list2, 2);
insert(list2, 3);
insert(list2, 4);

struct LinkedList* union_list = find_union(list1, list2);

printf("Union of the linked lists: ");


display(union_list);
return 0;
}

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

struct Graph* createGraph(int numVertices) {


struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->numVertices = numVertices;
graph->adjLists = (struct Node*)malloc(numVertices * sizeof(struct Node));

int i;
for (i = 0; i < numVertices; i++) {
graph->adjLists[i] = NULL;
}

return graph;
}

void addEdge(struct Graph* graph, int src, int dest) {


struct Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
}

void printGraph(struct Graph* graph) {


int i;
for (i = 0; i < graph->numVertices; i++) {
struct Node* temp = graph->adjLists[i];
printf("Adjacency list of vertex %d: ", i);
while (temp) {
printf("%d -> ", temp->vertex);
temp = temp->next;
}
printf("NULL\n");
}
}

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]);
}
}

struct Graph* graph = createGraph(numVertices);


for (i = 0; i < numVertices; i++) {
for (j = 0; j < numVertices; j++) {
if (adjMatrix[i][j] == 1) {
addEdge(graph, i, j);
}
}
}

printf("Adjacency list representation of the graph:\n");


printGraph(graph);

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;

struct BST *left;

struct BST *right;

};

typedef struct BST NODE;

NODE *node;

NODE* createtree(NODE *node, int data)

if (node == NULL)

{
NODE *temp;

temp= (NODE*)malloc(sizeof(NODE));

temp->data = data;

temp->left = temp->right = NULL;

return temp;

if (data < (node->data))

node->left = createtree(node->left, data);

else if (data > node->data)

node -> right = createtree(node->right, data);

return node;

void inorder(NODE *node)

{
if(node != NULL)

inorder(node->left);

printf("%d\t", node->data);

inorder(node->right);

void postorder(NODE *node)

if(node != NULL)

postorder(node->left);

postorder(node->right);

printf("%d\t", node->data);

int main()

int data, ch, i, n;

NODE *root=NULL;

while (ch!=4)

{
printf("\n\n1.Insertion in Binary Search Tree");

printf("\n2.Inorder");

printf("\n3.Postorder");

printf("\n4.Exit\n");

printf("\nEnter your Choice: ");

scanf("%d", &ch);

switch (ch)

case 1: printf("\nEnter size of tree: " );

scanf("%d", &n);

printf("\nEnter the elements of tree)\n");

for(i=0; i<n; i++)

scanf("%d", &data);

root=createtree(root, data);

break;

case 2: printf("\nInorder Traversal: \n");

inorder(root);

break;
case 3: printf("\nPostorder Traversal: \n");

postorder(root);

break;

case 4: exit(0);

default: printf("\nEnter valid choice\n");

break;

}
Output-:
1.Insertion in Binary Search Tree

2.Inorder

3.Postorder

4.Exit

Enter your Choice: 1

Enter size of tree: 2

Enter the elements of tree)

1.Insertion in Binary Search Tree

2.Inorder

3.Postorder

4.Exit

Enter your Choice: 3

Postorder Traversal:

1 3

1.Insertion in Binary Search Tree

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.

printf("Enter degree of 1st polynomial equation : \n");

scanf("%d",&deg1);

printf("\nEnter your 1st equation (Coefficient only):\n");

for(i=deg1;i>=0;i--)

scanf("%d",&a[i]);

// 2nd equation.

printf("\nEnter degree of 2nd polynomial equation : \n");

scanf("%d",&deg2);
printf("\nEnter your 2nd equation (Coefficient only):\n");

for(j=deg2;j>=0;j--)

scanf("%d",&b[j]);

// printing equations.

printf("\n1st equation : ");

for(i=deg1;i>=0;i--)

if(a[i]>=0)

printf("+");

else

printf("-");

printf("%dx^%d",abs(a[i]),i);

printf("\n\n2nd equation : ");


for(j=deg2;j>=0;j--)

if(b[j]>=0)

printf("+");

else

printf("-");

printf("%dx^%d",abs(b[j]),j);

// addition of two equations

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++;

printf("\n\nResult of addition : ");


for(k=deg2;k>=0;k--)

if(c[k]>=0)

printf("+");

else

printf("-");

printf("%dx^%d",abs(c[k]),k);

}
Output-:
Enter degree of 1st polynomial equation :

Enter your 1st equation (Coefficient only):

Enter degree of 2nd polynomial equation :

Enter your 2nd equation (Coefficient only):

41st equation : +2x^2+3x^1+1x^0

2nd equation : +4x^4+4x^3+4x^2+4x^1+4x^0

Result of addition : +4x^4+4x^3+6x^2+7x^1+5x^0


Slip 5
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 Preorder traversing
technique
Solution:

#include <stdio.h>

#include <stdlib.h>

struct BST

int data;

struct BST *left;

struct BST *right;

};

typedef struct BST NODE;

NODE *node;

NODE* createtree(NODE *node, int data)

if (node == NULL)

{
NODE *temp;

temp= (NODE*)malloc(sizeof(NODE));

temp->data = data;

temp->left = temp->right = NULL;

return temp;

if (data < (node->data))

node->left = createtree(node->left, data);

else if (data > node->data)

node -> right = createtree(node->right, data);

return node;

void inorder(NODE *node)

{
if(node != NULL)

inorder(node->left);

printf("%d\t", node->data);

inorder(node->right);

void preorder(NODE *node)

if(node != NULL)

printf("%d\t", node->data);

preorder(node->left);

preorder(node->right);

int main()

int data, ch, i, n;

NODE *root=NULL;

while (ch!=4)

{
printf("\n\n1.Insertion in Binary Search Tree");

printf("\n2.Inorder");

printf("\n3.Preorder");

printf("\n4.Exit\n");

printf("\nEnter your Choice: ");

scanf("%d", &ch);

switch (ch)

case 1: printf("\nEnter size of tree: " );

scanf("%d", &n);

printf("\nEnter the elements of tree)\n");

for(i=0; i<n; i++)

scanf("%d", &data);

root=createtree(root, data);

break;

case 2: printf("\nInorder Traversal: \n");

inorder(root);

break;
case 3: printf("\nPreorder Traversal: \n");

preorder(root);

break;

case 4: exit(0);

default: printf("\nEnter valid choice\n");

break;

Output-:
1.Insertion in Binary Search Tree

2.Inorder

3.Preorder

4.Exit

Enter your Choice: 1

Enter size of tree: 2

Enter the elements of tree)

1.Insertion in Binary Search Tree

2.Inorder
3.Preorder

4.Exit

Enter your Choice: 2

Inorder Traversal:

3 4

1.Insertion in Binary Search Tree

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 * next;

};

struct node* head=NULL;


struct node *cn()

struct node* n;

n=(struct node *)malloc(sizeof(struct node));

return(n);

void createnode() //Function to create Linked list.

int i=0,x,digits[100];

struct node * temp,*newnode;

printf("Enter any number :\n");

scanf("%d",&x);

while(x!=0)

digits[i]=x%10;

x=x/10;

i++;

i--;

for(i;i>=0;i--)

if(head==NULL) //checking in linked list is empty

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;

void traverse() //Function to traverse Linked list.

struct node * temp;

int cnt=0;

temp=head;

while(temp!=NULL)

printf("%d\n",temp->data);

temp=temp->next;
cnt++;

printf("Total number of node :%d",cnt);

int main()

int choice,a=0;

while(choice!=3)

printf("\n\n1. Create Linked list.\n");

printf("2. Display Linked list.\n");

printf("3. Exit\n\n");

scanf("%d",&choice);

printf("\n");

switch(choice)

case 1: if(a==1)

printf("You have already created a Linked list.");

}
else

{ createnode();

a++;

break;

case 2: traverse();

break;

case 3: exit(0);

defalut: printf("Enter a valid choice\n");

}
Output-:
1. Create Linked list.

2. Display Linked list.

3. Exit

Enter any number :

1. Create Linked list.

2. Display Linked list.

3. Exit

You have already created a Linked list.

1. Create Linked list.

2. Display Linked list.

3. Exit

Total number of node :1

1. Create Linked list.

2. Display Linked list.

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;

struct BST *left;

struct BST *right;

};

typedef struct BST NODE;

NODE *node;

NODE* createtree(NODE *node, int data)

if (node == NULL)

{
NODE *temp;

temp= (NODE*)malloc(sizeof(NODE));

temp->data = data;

temp->left = temp->right = NULL;

return temp;

if (data < (node->data))

node->left = createtree(node->left, data);

else if (data > node->data)

node -> right = createtree(node->right, data);

return node;

void preorder(NODE *node)

if(node != NULL)
{

printf("%d\t", node->data);

preorder(node->left);

preorder(node->right);

void postorder(NODE *node)

if(node != NULL)

postorder(node->left);

postorder(node->right);

printf("%d\t", node->data);

int main()

int data, ch, i, n;

NODE *root=NULL;

while (ch!=4)

printf("\n\n1.Insertion in Binary Search Tree");

printf("\n2.Preorder");
printf("\n3.Postorder");

printf("\n4.Exit\n");

printf("\nEnter your Choice: ");

scanf("%d", &ch);

switch (ch)

case 1: printf("\nEnter size of tree: " );

scanf("%d", &n);

printf("\nEnter the elements of tree)\n");

for(i=0; i<n; i++)

scanf("%d", &data);

root=createtree(root, data);

break;

case 2: printf("\nPreorder Traversal: \n");

preorder(root);

break;

case 3: printf("\nPostorder Traversal: \n");

postorder(root);
break;

case 4: exit(0);

default: printf("\nEnter valid choice\n");

break;

}
Output-:
1.Insertion in Binary Search Tree

2.Preorder

3.Postorder

4.Exit

Enter your Choice: 1

Enter size of tree: 3

Enter the elements of tree)

1.Insertion in Binary Search Tree

2.Preorder

3.Postorder

4.Exit

Enter your Choice: 2

Preorder Traversal:

1 2 3

1.Insertion in Binary Search Tree

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 bubble_sort(int [],int);

void main()

int A[20],temp,i,j,n;

printf("Enter size of Array :\n");

scanf("%d",&n);

printf("\nEnter Array elements :\n");

for(i=0;i<n;i++)

scanf("%d",&A[i]);

bubble_sort(A,n);

printf("\nSorted Array :\n");

for(i=0;i<n;i++)

printf("%d\n",A[i]);

void bubble_sort(int A[],int n)

{
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

- Create a Binary Search Tree

- Display

- Delete a given element from Binary Search Tree

Solution
#include <stdio.h>

#include <stdlib.h>

struct btnode

int value;

struct btnode *l;

struct btnode *r;

}*root = NULL, *temp = NULL, *t2, *t1;

// Function declaration

void create();

void insert();
void search(struct btnode *t);

void search1(struct btnode *t, int data);

void display(struct btnode *t);

void delete();

void delete1(struct btnode *t);

int flag = 1;

void main() {

int ch;

printf("\nOPERATIONS ---");

printf("\n1 - Insert an element into tree\n");

printf("2 - Display\n");

printf("3 - Delete\n");

printf("4 - Exit\n");

while(1) {

printf("\nEnter your choice : ");

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;

printf("Enter data of node to be inserted : ");

scanf("%d", &data);

temp = (struct btnode *)malloc(1*sizeof(struct btnode));

temp->value = data;

temp->l = temp->r = NULL;

}
void insert() {

create();

if (root == NULL)

root = temp;

else

search(root);

// find the right position in the tree to insert the data

void search(struct btnode *t) {

if ((temp->value > t->value) && (t->r != NULL)) {

/* value more than root node value insert at right */

search(t->r);

else if ((temp->value > t->value) && (t->r == NULL)) {

t->r = temp;

else if ((temp->value < t->value) && (t->l != NULL)) {

/* value less than root node value insert at left */

search(t->l);

else if ((temp->value < t->value) && (t->l == NULL)) {

t->l = temp;
}

// Search for the appropriate position to insert the new node

void search1(struct btnode *t, int data) {

if ((data>t->value)) {

t1 = t;

search1(t->r, data);

else if ((data < t->value)) {

t1 = t;

search1(t->l, data);

else if ((data==t->value)) {

delete1(t);

void display(struct btnode *t) {

if (root == NULL) {

printf("No elements in a tree to display");

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) {

printf("No elements in a tree to delete");

return;

printf("Enter the data to be deleted : ");

scanf("%d", &data);

t1 = root;

t2 = root;

search1(root, data);
}

// To delete a node

void delete1(struct btnode *t) {

int k;

// To delete leaf node

if ((t->l == NULL) && (t->r == NULL)) {

if (t1->l == t) {

t1->l = NULL;

else {

t1->r = NULL;

t = NULL;

free(t);

return;

// To delete node having one left hand child

else if ((t->r == NULL)) {

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;

// To delete node having right hand child

else if (t->l == NULL) {

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;

// To delete node having two child

else if ((t->l != NULL) && (t->r != NULL)) {

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;

// To find the smallest element in the right sub tree

int smallest(struct btnode *t) {

t2 = t;

if (t->l != NULL) {
t2 = t;

return(smallest(t->l));

else {

return (t->value);

// To find the largest element in the left sub tree

int largest(struct btnode *t) {

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

Enter data of node to be inserted : 2

Enter your choice : 2

2 ->

Enter your choice : 2

2 ->

Enter your choice :

Enter data of node to be inserted : 11

Enter your choice : 4

Q.2 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.

Solution
#include <stdio.h>

#include <stdlib.h>

struct node {

int num;

struct node *nextptr;

}*stnode;
void createNodeList(int n) {

struct node *fnNode, *tmp;

int num, i;

stnode = (struct node *)malloc(sizeof(struct node));

if(stnode == NULL) {

printf(" Memory can not be allocated.");

else {

printf(" Input data for node 1 : ");

scanf("%d", &num);

stnode-> num = num;

stnode-> nextptr = NULL;

tmp = stnode;

for(i=2; i<=n; i++)

fnNode = (struct node *)malloc(sizeof(struct node));

if(fnNode == NULL) {

printf(" Memory can not allocated.");

break;

}
else {

printf(" Input data for node %d : ", i);

scanf(" %d", &num);

fnNode->num = num;

fnNode->nextptr = NULL;

tmp->nextptr = fnNode;

tmp = tmp->nextptr;

int NodeCount() {

int ctr = 0;

struct node *tmp;

tmp = stnode;

while(tmp != NULL) {

ctr++;

tmp = tmp->nextptr;

return ctr;

}
void displayList() {

struct node *tmp;

if(stnode == NULL) {

printf(" No data found in the list.");

else {

tmp = stnode;

while(tmp != NULL) {

printf(" Data = %d\n", tmp->num);

tmp = tmp->nextptr;

int main()

int n,totalNode;

printf(" Input the number of nodes : ");

scanf("%d", &n);

createNodeList(n);
printf("\n Data entered in the list are : \n");

displayList();

totalNode = NodeCount();

printf("\n Total number of nodes = %d\n", totalNode);

return 0;

OUTPUT
Input the number of nodes : 3

Input data for node 1 : 10

Input data for node 2 : 20

Input data for node 3 : 30

Data entered in the list are :

Data = 10

Data = 20

Data = 30

Total number of nodes = 3


Slip 8

A) Write menu driven program using ‘C’ for Binary Search Tree. The menu includes

- Create a Binary Search Tree

- Display

- Search the element in Binary Search Tree.

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];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)

scanf("%d", &arr[i]);

for (i = 1; i < n; i++)

j = i;

while (j > 0 && arr[j - 1] > arr[j])

temp = arr[j];

arr[j] = arr[j - 1];

arr[j - 1] = temp;

j--;

printf("Sorted list in ascending order:\n");

for (i = 0; i < n; i++)

{
printf("%d\n", arr[i]);

return 0;

Output:-
Enter number of elements 5

Enter 5 integers

37295

Sorted list in ascending order:

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];

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 :: 55-55

The result of expression 55-55 = 5


B) Write a ‘C’ program to create a singly linked list, reverse it and display both the
list.
Solution:

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *head=NULL;

struct node *cn()

struct node *n;

n=(struct node *)malloc(sizeof(struct node));

return(n);

void createnode(int n) //Function to create Linked list.

int i,x;

struct node * temp,*newnode;

printf("Enter elements of linked list :\n");


for(i=1;i<=n;i++)

scanf("%d",&x);

if(head==NULL) //checking in linked list is empty

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()

struct node *temp;


temp=head;

while (temp!=NULL)

printf("%d\n",temp->data);

temp=temp->next;

void reverselist()

struct node *prev,*cur;

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;

printf("Enter size of Linked list\n");

scanf("%d",&n);

createnode(n);

printf("Displaying list:\n");

display();

printf("\nReverse list is :\n");

reverselist();

display();

}
Output-:
Enter size of Linked list

Enter elements 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;

struct node* left, *right;

};

struct node *node;

struct node *newnode(int data)

struct node *temp;

temp= (struct node*)malloc(sizeof(struct node));

temp->data = data;

temp->left = NULL;temp->right = NULL;

return temp;

struct node *insertLevelOrder(int arr[],struct node *root,int i,int n)

{
if(i<n)

struct 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;

void printGivenLevel(struct node* root, int level);

int height(struct node* node);

struct node* newNode(int data);

void printLevelOrder(struct node* root)

int h = height(root);

int i;

for (i=1; i<=h; i++)

printGivenLevel(root, i);

printf("\n");

}
}

void printGivenLevel(struct node* root, int level)

if (root == NULL)

return;

if (level == 1)

printf("%d ", root->data);

else if (level > 1)

printGivenLevel(root->left, level-1);

printGivenLevel(root->right, level-1);

int height(struct node* node)

if (node==NULL)

return 0;

else

int lheight = height(node->left);


int rheight = height(node->right);

if (lheight > rheight)

return(lheight+1);

else return(rheight+1);

struct node* newNode(int data)

struct node* node = (struct node*)

malloc(sizeof(struct node));

node->data = data;

node->left = NULL;

node->right = NULL;

return(node);

int main()

struct node *root = NULL;

int n,arr[50],i;

printf("\nEnter size of tree: " );

scanf("%d", &n);
printf("\nEnter the Values to Create Binary tree\n");

for(i=0; i<n; i++)

scanf("%d",&arr[i]);

root=insertLevelOrder(arr,root,0,n);

printf("Level Order traversal of binary tree is \n");

printLevelOrder(root);

return 0;

Output-:
Enter size of tree: 1

Enter the Values to Create Binary tree

12

Level Order traversal of binary tree is

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;

while(temp<A[j] && j>=0)


{
A[j+1]=A[j];
j=j-1;
}
A[j+1]=temp;
}
}

void array(int A[],int n)


{
int i;
srand(time(0));
for(i = 0; i<n; i++)
{
A[i]=rand();
}
printf("Array :\n");
for(i = 0; i<n; i++)
{
printf("%d ",A[i]);
}
printf("\n");
}
Output-:
Enter size of array :

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 * next;

};

struct node* head=NULL;

struct node *cn()

struct node* n;

n=(struct node *)malloc(sizeof(struct node));

return(n);

void createnode() //Function to create Linked list.

int i,n,x;

struct node * temp,*newnode;

printf("Enter size of linked list :\n");


scanf("%d",&n);

printf("Enter elements of linked list :\n");

for(i=1;i<=n;i++)

scanf("%d",&x);

if(head==NULL) //checking in linked list is empty

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 traverse() //Function to traverse Linked list.


{

struct node * temp;

temp=head;

while(temp!=NULL)

printf("%d\n",temp->data);

temp=temp->next;

void insertnode() //Function to insert node in Linked list.

struct node * temp,*newnode;

int pos,i=1,x;

printf("Enter position for insertion of new node :\n");

scanf("%d",&pos);

printf("Enter value for that node :\n");

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)

printf("Position out of bound.\n");

else

newnode->next=temp->next;

temp->next=newnode;

printf("node inserted\n");

void search() //Function to delete a node from Linked list.

struct node * temp;

int key,i=1,flag=0;

printf("Enter element you want to search :\n");

scanf("%d",&key);

temp=head;

if(head==NULL)

printf("Linked list is empty. \n");


}

else

while(temp!=NULL)

if(key==temp->data)

flag=1;

break;

temp=temp->next;

i++;

if(flag==0)

printf("Elemet not found.\n");

else

printf("Element found at position :%d",i);

}
}

void main()

int choice,a=0;

while(choice!=5)

printf("\n\n1. Create Linked list.\n");

printf("2. Display Linked list.\n");

printf("3. Insert node at any position.\n");

printf("4. Search.\n");

printf("5. Exit\n\n");

scanf("%d",&choice);

printf("\n");

switch(choice)

case 1: if(a==1)

printf("You have already created a Linked list.");

else

{ createnode();

a++;

}
break;

case 2: traverse();

break;

case 3: insertnode();

break;

case 4: search();

break;

case 5: exit(0);

defalut: printf("Enter a valid choice\n");

}
Output-:
1. Create Linked list.

2. Display Linked list.

3. Insert node at any position.

4. Search.

5. Exit

Enter size of linked list :

Enter elements of linked list :

1. Create Linked list.

2. Display Linked list.

3. Insert node at any position.

4. Search.

5. Exit

1. Create Linked list.

2. Display Linked list.

3. Insert node at any position.

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;

struct node *next;

};

struct node *front=NULL;

struct node *rear=NULL;

struct node *cn()

struct node *n;

n=(struct node *)malloc(sizeof(struct node));

return(n);

void createqueue()

int n,x,i;

struct node *newnode;

printf("Enter size of queue :\n");


scanf("%d",&n);

printf("Enter elements of queue :\n");;

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()

struct node *newnode;

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;

printf("Element inserted successfully");

void delete()

struct node *temp;


if(front==NULL)

printf("Queue is empty.\n");

else

temp=front;

front=front->next;

temp->next=NULL;

free(temp);

printf("Element deleted successfully");

void display()

struct node *temp;

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("\n\n1. Create queue\n");

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)

printf("You have already create queue.\n");

else

createqueue();

a=1;
}

break;

case 2: insert();

break;

case 3: delete();

break;

case 4: display();

break;

case 5: exit(0);

deafult:printf("Enter valid choice.");

}
Output-:
1. Create queue

2. Insert

3. Delete

4. Display

5. Exit

Enter size of queue :

Enter elements of queue :

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:

Enter matrix elements:

12

22

2222

2222

Adjacency matrix Is:

3 2 2

2 12 2

22 2222 2222

Given graph is Directed

v0 indegree =27 Outdegree= 7

v1 indegree =2263 Outdegree= 23

v2 indegree =4489 Outdegree= 4489


B) Write a ‘C’ program to accept and sort n elements in ascending order using
Selection sort method .
Solution:
#include<stdio.h>
#include<conio.h>
void selection_sort(int [],int);
void main()
{
int A[20],i,j,min,n,temp;
printf("Enter size of array :\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
selection_sort(A,n);

printf("Sorted Array :\n");


for(i=0;i<n;i++)
{
printf("%d\n",A[i]);
}
}
void selection_sort(int A[],int n)
{
int i,min,j,temp;
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(A[j]<A[min])
{
min=j;
}
}
temp=A[i];
A[i]=A[min];
A[min]=temp;
}
}

Output-:
Enter size of array :

Enter array elements :

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<stdlib.h> /* for exit() */

#include<ctype.h> /* for isdigit(char ) */

#include<string.h>

#define SIZE 100

/* declared here as global variable because stack[]

* is used by more than one fucntions */

char stack[SIZE];

int top = -1;

/* define push operation */

void push(char item)

if(top >= SIZE-1)

printf("\nStack Overflow.");

}
else

top = top+1;

stack[top] = item;

/* define pop operation */

char pop()

char item ;

if(top <0)

printf("stack under flow: invalid infix expression");

getchar();

/* underflow may occur for invalid expression */

/* where ( and ) are not matched */

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

(that is symbol is operand)

* this fucntion returns 1 if symbol is opreator else return 0 */

int is_operator(char symbol)

if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')

return 1;

else

return 0;

/* define fucntion that is used to assign precendence to operator.

* Here ^ denotes exponent operator.

* In this fucntion we assume that higher integer value

* means higher precendence */

int precedence(char symbol)


{

if(symbol == '^')/* exponent operator, highest precedence*/

return(3);

else if(symbol == '*' || symbol == '/')

return(2);

else if(symbol == '+' || symbol == '-') /* lowest precedence */

return(1);

else

return(0);

void InfixToPostfix(char infix_exp[], char postfix_exp[])

int i, j;

char item;

char x;
push('('); /* push '(' onto stack */

strcat(infix_exp,")"); /* add ')' to infix expression */

i=0;

j=0;

item=infix_exp[i]; /* initialize before loop*/

while(item != '\0') /* run loop till end of infix expression */

if(item == '(')

push(item);

else if( isdigit(item) || isalpha(item))

postfix_exp[j] = item; /* add operand symbol to postfix expr */

j++;

else if(is_operator(item) == 1) /* means symbol is operator */

x=pop();

while(is_operator(x) == 1 && precedence(x)>= precedence(item))

postfix_exp[j] = x; /* so pop all higher precendence


operator and */
j++;

x = pop(); /* add them to postfix expresion */

push(x);

/* because just above while loop will terminate we have

oppped one extra item

for which condition fails and loop terminates, so that one*/

push(item); /* push current oprerator symbol onto stack */

else if(item == ')') /* if current symbol is ')' then */

x = pop(); /* pop and keep popping until */

while(x != '(') /* '(' encounterd */

postfix_exp[j] = x;

j++;

x = pop();

else

{ /* if current symbol is neither operand not '(' nor ')' and nor

operator */

printf("\nInvalid infix Expression.\n"); /* the it is illegeal symbol */

getchar();
exit(1);

i++;

item = infix_exp[i]; /* go to next symbol of infix expression */

} /* while loop ends here */

if(top>0)

printf("\nInvalid infix Expression.\n"); /* the it is illegeal symbol */

getchar();

exit(1);

if(top>0)

printf("\nInvalid infix Expression.\n"); /* the it is illegeal symbol */

getchar();

exit(1);

postfix_exp[j] = '\0'; /* add sentinel else puts() fucntion */

/* will print entire postfix[] array upto SIZE */

}
/* main function begins */

int main()

char infix[SIZE], postfix[SIZE]; /* declare infix string and postfix string */

/* why we asked the user to enter infix expression

* in parentheses ( )

* What changes are required in porgram to

* get rid of this restriction since it is not

* in algorithm

* */

printf("ASSUMPTION: The infix expression contains single letter variables and single
digit constants only.\n");

printf("\nEnter Infix expression : ");

gets(infix);

InfixToPostfix(infix,postfix); /* call to convert */

printf("Postfix Expression: ");

puts(postfix); /* print postfix expression */

return 0;

}
Output-:
Enter Infix expression : 58-95

Postfix Expression: 5895-


B) Write a ‘C’ program to create doubly link list and display nodes having odd value
Solution:
#include<stdlib.h>

#include<stdio.h>

struct node

int data;

struct node * prev,*next;

};

struct node *head=NULL;

struct node * cn()

struct node * n;

n=(struct node *)malloc(sizeof(struct node));

return (n);

void createnode()

int i,n,x;

struct node *temp,*newnode;

printf("Enter size of linked list:\n");

scanf("%d",&n);

printf("Enter elements of linked list:\n");


for(i=1;i<=n;i++)

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()

struct node * temp;

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("\n\n1. Create Linked list.\n");

printf("2. Display.\n");

printf("3. Exit.\n\n");

scanf("%d",&choice);

switch(choice)

case 1: if(a==1)

printf("You have already created a Linked list.\n");

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.

1. Create Linked list.

2. Display.

3. Exit.

Enter size of linked list:

Enter elements of linked list:

1. Create Linked list.

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;

};

// Function to create a stack of given capacity

struct Stack* createStack(unsigned capacity) {

struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

stack->capacity = capacity;

stack->top = -1;

stack->array = (char*)malloc(stack->capacity * sizeof(char));

return stack;

// Stack is full when the top is equal to the last index

int isFull(struct Stack* stack) {

return stack->top == stack->capacity - 1;

}
// Stack is empty when the top is equal to -1

int isEmpty(struct Stack* stack) {

return stack->top == -1;

// Function to add an item to the stack

void push(struct Stack* stack, char item) {

if (isFull(stack))

return;

stack->array[++stack->top] = item;

// Function to remove an item from the stack

char pop(struct Stack* stack) {

if (isEmpty(stack))

return '\0';

return stack->array[stack->top--];

// Function to reverse the string using a stack

void reverseString(char* str) {

// Get string length

int n = strlen(str);

// Create a stack of capacity equal to the length of the string

struct Stack* stack = createStack(n);

// Push all characters of the string to the stack

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

for (i = 0; i < n; i++)

str[i] = pop(stack);

// Main function

int main() {

char str[100];

printf("Enter a string: ");

scanf("%s", str);

reverseString(str);

printf("Reversed string is: %s\n", str);

return 0;

Output
Enter a string: hello

Reversed string is: olleh

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>

// Function to perform bubble sort

void bubbleSort(char arr[][50], int n) {

int i, j;

char temp[50];

for (i = 0; i < n-1; i++) {

for (j = 0; j < n-i-1; j++) {

if (strcmp(arr[j], arr[j+1]) > 0) {

strcpy(temp, arr[j]);

strcpy(arr[j], arr[j+1]);

strcpy(arr[j+1], temp);

int main() {

int n, i;

char names[100][50];

printf("Enter the number of names: ");

scanf("%d", &n);

// Accepting names
for (i = 0; i < n; i++) {

printf("Enter name %d: ", i + 1);

scanf("%s", names[i]);

// Sorting the names using bubble sort

bubbleSort(names, n);

// Displaying the sorted names

printf("\nSorted names in alphabetical order:\n");

for (i = 0; i < n; i++) {

printf("%s\n", names[i]);

return 0;

Output-:
Enter the number of names: 2

Enter name 1: pune

Enter name 2: guna

Sorted names in alphabetical order:

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;

};

struct Stack* createStack( unsigned capacity )

struct Stack* stack = (struct Stack*)

malloc(sizeof(struct Stack));

if (!stack)

return NULL;

stack->top = -1;

stack->capacity = capacity;

stack->array = (int*) malloc(stack->capacity *

sizeof(int));
return stack;

int isEmpty(struct Stack* stack)

return stack->top == -1 ;

char peek(struct Stack* stack)

return stack->array[stack->top];

char pop(struct Stack* stack)

if (!isEmpty(stack))

return stack->array[stack->top--] ;

return '$';

void push(struct Stack* stack, char op)

stack->array[++stack->top] = op;

int isOperand(char ch)

return (ch >= 'a' && ch <= 'z') ||

(ch >= 'A' && ch <= 'Z');

int Prec(char ch)


{

switch (ch)

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':

return 3;

return -1;

int infixToPostfix(char* exp)

int i, k;

struct Stack* stack = createStack(strlen(exp));

if(!stack)

return -1 ;

for (i = 0, k = -1; exp[i]; ++i)

{
if (isOperand(exp[i]))

exp[++k] = exp[i];

else if (exp[i] == '(')

push(stack, exp[i]);

else if (exp[i] == ')')

while (!isEmpty(stack) && peek(stack) != '(')

exp[++k] = pop(stack);

if (!isEmpty(stack) && peek(stack) != '(')

return -1;

else

pop(stack);

else

while (!isEmpty(stack) &&

Prec(exp[i]) <= Prec(peek(stack)))

exp[++k] = pop(stack);

push(stack, exp[i]);

}
while (!isEmpty(stack))

exp[++k] = pop(stack );

exp[++k] = '\0';

printf( "%s", exp );

int main()

char exp[MAX] ;

printf("ENter the EXpression\n");

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;

struct stack *cn()


{
struct stack *n;
n=(struct stack *)malloc(sizeof(struct stack));
return (n);
}

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;

char stack[50];char array1[50];char array2[50];

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();

printf("\n\nReversed String :\n");


puts(array2);

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;

struct node *cn()


{
struct node* n;
n=(struct node *)malloc(sizeof(struct node));
return(n);
}

void createnode(int n) //Function to create Linked list.


{
int i,x;
struct node * temp,*newnode;
printf("Enter elements of linked list :\n");
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(head==NULL) //checking in linked list is empty
{
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() //Function to traverse Linked list.


{
struct node * temp;
temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}

void search(int key) //Function to delete a node from Linked list.


{
struct node * temp;
int i=1,flag=0;
temp=head;
if(head==NULL)
{
printf("Linked list is empty. \n");
}
else
{
while(temp!=NULL)
{
if(key==temp->data)
{
flag=1;
break;
}
temp=temp->next;
i++;

}
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

Enter element you want to search :


2
Element found at position :1
Slip17
Slip 17 - A) Write a ‘C’ program to read a postfix expression, evaluate it and display
the result. (Use Static Implementation of Stack).
Solution:

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

#define MAX_SIZE 100

int main() {
char cities[MAX_SIZE][50];
int num_cities, i;
char search_city[50];
int found = 0;

printf("Enter the number of cities: ");


scanf("%d", &num_cities);

// Accept city names and store them in the array


for (i = 0; i < num_cities; i++) {
printf("Enter name of city %d: ", i + 1);
scanf("%s", cities[i]);
}

// Accept the city to be searched


printf("Enter the city to be searched: ");
scanf("%s", search_city);

// Perform linear search


for (i = 0; i < num_cities; i++) {
if (strcmp(cities[i], search_city) == 0) {
found = 1;
break;
}
}

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-:

Enter the number of cities: 2


Enter name of city 1: pune
Enter name of city 2: mumbai
Enter the city to be searched: pune
City pune found at position 1 .
Slip 18
Slip 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. - Number of nodes - Degree of tree - Leaf nodes

Solution:

#include<stdio.h>

#include<stdlib.h>

struct binary_tree

int info;

struct binary_tree *left;

struct binary_tree *right;

};

typedef struct binary_tree NODE;

NODE *node=NULL;

NODE *newnode(int data)

NODE *temp;

temp= (NODE*)malloc(sizeof(NODE));

temp->info = data;

temp->left = NULL;temp->right = NULL;

return temp;

NODE *insertLevelOrder(int arr[],NODE *root,int i,int n)

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;

unsigned int count(NODE *node)

if(node==NULL)

return 0;

else

return 1+count(node->left)+count(node->right);

unsigned int getLeafCount(NODE *node)

if(node == NULL)

return 0;

if(node->left == NULL && node->right==NULL)

return 1;

else

return getLeafCount(node->left)+
getLeafCount(node->right);

int degree(NODE *node)

if(node==NULL)

printf("Tree is empty\n");

else if(node->left==NULL && node->right==NULL)

return 0;

else if(node->left && node->right)

return 2;

else if(node->left || node->right)

return 1;

int main()
{

int data,ch,i,n,arr[50],count1,count2,count3;

NODE *root=NULL;

while (ch!=5)

printf("\n\n1.Insertion in Binary Search Tree");

printf("\n2.Total number of nodes");

printf("\n3.Degree of tree");

printf("\n4.Leaf nodes");

printf("\n5.Exit");

printf("\nEnter your Choice: \n");

scanf("%d", &ch);

switch (ch)

case 1: printf("\nEnter size of tree: " );

scanf("%d", &n);

printf("\nEnter the Values to Create Binary tree\n");

for(i=0; i<n; i++)

scanf("%d",&arr[i]);

root=insertLevelOrder(arr,root,0,n);

break;

case 2: count1=count(root);

printf("Number of nodes :%d\n",count1);


break;

case 3: count2=degree(root);

printf("Degree of tree :%d\n",count2);

break;

case 4: count3=getLeafCount(root);

printf("Number of Leaf nodes :%d\n",count3);

break;

case 5: exit(0);

default: printf("\nSelect a valid option\n");

break;

}
Output-:
1.Insertion in Binary Search Tree

2.Total number of nodes

3.Degree of tree

4.Leaf nodes

5.Exit

Enter your Choice:

Enter size of tree: 2

Enter the Values to Create Binary tree

23

1.Insertion in Binary Search Tree

2.Total number of nodes

3.Degree of tree

4.Leaf nodes

5.Exit

Enter your Choice:

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 :

Enter elements 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>

int top = -1, i;

char stack[50];

char array1[50];

char array2[50];

void reverse();

int 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();

printf("\n\nReversed String :\n");

puts(array2);

return 0;
}

void reverse()

int j = 0;

for (i = top; i >= 0; i--)

array2[j] = stack[i];

j++;

array2[j] = '\0'; // Add the string termination character

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;

struct node *cn()


{
struct node* n;
n=(struct node *)malloc(sizeof(struct node));
return(n);
}

void createnode() //Function to create Linked list.


{
int i,n,x;
struct node * temp,*newnode;
printf("Enter size of linked list :\n");
scanf("%d",&n);
printf("Enter elements of linked list :\n");
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(head==NULL) //checking in linked list is empty
{
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()
{
struct node * temp;
temp=head;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
while(temp->next!=NULL || temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next->next;
}
}
}

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 top = -1, front = 0;

int stack[MAX];

void push(char);

void pop();

/* push a character into stack */

void push(char a)

top++;

stack[top] = a;

/* delete an element in stack */

void pop()

top--;

int main()
{

int i, cho;

char s[MAX], b;

printf("\nEnter string : ");

scanf("%s", s);

for (i = 0;s[i] != '\0';i++)

b = s[i];

push(b);

for (i = 0;i < (strlen(s) / 2);i++)

if (stack[top] == stack[front])

pop();

front++;

else

printf("'%s' is not palindrome.\n\n", s);

if ((strlen(s) / 2)== front)

{
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

Enter your choice :


1
Enter number of nodes :
3
Enter elements of Linked list:
1
2
3
1.Create Linked list
2.Display
3.Swap
4.Exit

Enter your choice :


2
1
2
3
` Slip 21
A) Write a ‘C’ program to read an adjacency matrix of a directed graph and traverse
using BFS.
Solution:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
int n;

struct queue {
int items[SIZE];
int front;
int rear;
};

struct queue* createQueue();


void enqueue(struct queue* q, int);
int dequeue(struct queue* q);
void display(struct queue* q);
int isEmpty(struct queue* q);
void printQueue(struct queue* q);

struct node {
int vertex;
struct node* next;
};

struct node* createNode(int);

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);

struct node* temp = graph->adjLists[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;

graph->adjLists = malloc(vertices * sizeof(struct node*));


graph->visited = malloc(vertices * sizeof(int));

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

// Check if the queue is empty


int isEmpty(struct queue* q) {
if (q->rear == -1)
return 1;
else
return 0;
}
// Adding elements into queue
void enqueue(struct queue* q, int value) {
if (q->rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}

// Removing elements from queue


int dequeue(struct queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
printf("\nResetting queue ");
q->front = q->rear = -1;
}
}
return item;
}

// Print the queue


void printQueue(struct queue* q) {
int i = q->front;

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);

struct Graph* graph = createGraph(n);


addEdge(graph,graph_type);

bfs(graph, 0);

return 0;
}
Output-:
Enter 1 for Undirected graph
Enter 2 for Directed graph

Enter your choice :: 1


Enter number of vertices :
2
Enter edge [1] (-1 -1 to quit): 12
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.
Solution:
#include<stdio.h>
#include<conio.h>

void main() {
int arr[100], n, i, search;

printf("Enter the number of elements you want in the array: ");


scanf("%d", &n);

for(i=0; i<n; i++)


{
printf("Enter value for index %d: ", i);
scanf("%d", &arr[i]);
}

printf("\nEnter the number you want to search in the array: ");


scanf("%d", &search);

for(i=0; i<n; i++) {


if(arr[i] == search) {
printf("\n%d found at %d position.\n", search, i);
break;
}
}

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

- Create a Binary Search Tree

- Display

- Search the element in Binary Search Tree.

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];

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)

scanf("%d", &arr[i]);

for (i = 1; i < n; i++)

j = i;

while (j > 0 && arr[j - 1] > arr[j])

temp = arr[j];

arr[j] = arr[j - 1];

arr[j - 1] = temp;

j--;

printf("Sorted list in ascending order:\n");

for (i = 0; i < n; i++)

printf("%d\n", arr[i]);
}

return 0;

Output:-

Enter number of elements 5

Enter 5 integers

37295

Sorted list in ascending order:

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 * next;

};

struct node* head=NULL;

struct node *cn()

struct node* n;

n=(struct node *)malloc(sizeof(struct node));

return(n);

void createnode() //Function to create Linked list.

int i,n,x;

struct node * temp,*newnode;

printf("Enter size of linked list :\n");


scanf("%d",&n);

printf("Enter elements of linked list :\n");

for(i=1;i<=n;i++)

scanf("%d",&x);

if(head==NULL) //checking in linked list is empty

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 traverse() //Function to traverse Linked list.


{

struct node * temp;

temp=head;

while(temp!=NULL)

printf("%d\n",temp->data);

temp=temp->next;

void insertnode() //Function to insert node in Linked list.

struct node * temp,*newnode;

int pos,i=1,x;

printf("Enter position for insertion of new node :\n");

scanf("%d",&pos);

printf("Enter value for that node :\n");

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)

printf("Position out of bound.\n");

else

newnode->next=temp->next;

temp->next=newnode;

printf("node inserted\n");

void deletenode() //Function to delete a node from Linked list.

struct node * temp,*temp1;

int pos,i=1,x;

printf("Enter position for deletion :\n");

scanf("%d",&pos);

temp=head;

if(head==NULL)

printf("Linked list is empty. \n");


}

else if(pos==1)

head=head->next;

free(temp);

else

while((i<pos-1) && (temp!=NULL))

temp=temp->next;

i++;

if(temp!=NULL)

temp1=temp->next;

temp->next=temp1->next;

free(temp1);

else

printf("Position out of bound.\n");

}
}

void main()

int choice,a=0;

while(choice!=5)

printf("\n\n1. Create Linked list.\n");

printf("2. Display Linked list.\n");

printf("3. Insert node at any position.\n");

printf("4. Delete a node from any position.\n");

printf("5. Exit\n\n");

scanf("%d",&choice);

printf("\n");

switch(choice)

case 1: if(a==1)

printf("You have already created a Linked list.");

else

{ createnode();

a++;
}

break;

case 2: traverse();

break;

case 3: insertnode();

break;

case 4: deletenode();

break;

case 5: exit(0);

defalut: printf("Enter a valid choice\n");

}
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()

int i, low, high, mid, n, key, array[100];

printf("Enter number of elements:\n");

scanf("%d",&n);

printf("Enter %d integers :\n", n);

for(i = 0; i < n; i++)

scanf("%d",&array[i]);

printf("Enter value to find :\n");


scanf("%d", &key);

low = 0;

high = n - 1;

mid = (low+high)/2;

while (low <= high) {

if(array[mid] < key)

low = mid + 1;

else if (array[mid] == key) {

printf("%d found at location %d.", key, mid+1);

break;

else

high = mid - 1;

mid = (low + high)/2;

if(low > high)

printf("Not found! %d isn't present in the list.", key);

return 0;

}
output:-
Enter number of elements:

Enter 2 integers :

Enter value to find :

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;

struct Node* next;

};

struct Node* top = NULL;

void push(int val) {

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

new_node->data = val;

new_node->next = top;

top = new_node;

int pop() {

if (top == NULL) {
printf("Stack Underflow\n");

return -1;

struct Node* temp = top;

int val = temp->data;

top = top->next;

free(temp);

return val;

int evaluatePostfix(char* exp) {

int i;

for (i = 0; exp[i]; ++i) {

if (isdigit(exp[i]))

push(exp[i] - '0');

else {

int val1 = pop();

int val2 = pop();

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];

printf("Enter the postfix expression: ");

scanf("%s", exp);

int result = evaluatePostfix(exp);

printf("Result: %d\n", result);

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;

struct node *cn()


{
struct node* n;
n=(struct node *)malloc(sizeof(struct node));
return(n);
}
int n; //size of linked list

void createnode() //Function to create Linked list.


{
int i,x;
struct node * temp,*newnode;
printf("Enter size of linked list :\n");
scanf("%d",&n);
printf("Enter elements of linked list :\n");
for(i=1;i<=n;i++)
{
scanf("%d",&x);
if(head==NULL) //checking in linked list is empty
{
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 traverse() //Function to traverse Linked list.


{
struct node * temp;
temp=head;
printf("\n\nLinked list after deleting last node and adding it at beginnig.\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}

void deletenode() //Function to delete a node from Linked list.


{
struct node * temp,*temp1;
int i=1,x;

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 :

Enter elements of linked list :

Linked list after deleting last node and adding it at beginnig.

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 node* next;

};

struct node* createNode(int);

struct Graph

int numVertices;

struct node** adjLists;

int* visited;

};

// Creating a node
struct node* createNode(int v) {

struct node* newNode = (node*) malloc(sizeof(struct node));

newNode->vertex = v;

newNode->next = NULL;

return newNode;

// Creating a graph

struct Graph* createGraph(int vertices) {

struct Graph* graph = (Graph*) malloc(sizeof(struct Graph));

graph->numVertices = vertices;

graph->adjLists = (Graph*) malloc(vertices * sizeof(struct Graph*));

graph->visited = (Graph*) malloc(vertices * sizeof(int));

int i;

for (i = 0; i < vertices; i++)

graph->adjLists[i] = NULL;

graph->visited[i] = 0;

return graph;

void addEdge(struct Graph* graph)


{

int i,max_edges,u,v;

max_edges=n*(n-1)/2;

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;

// Add edge from d to s

newNode = createNode(u);
newNode->next = graph->adjLists[v];

graph->adjLists[v] = newNode;

void DFS(struct Graph* graph, int vertex)

struct node* adjList = graph->adjLists[vertex];

struct node* temp = adjList;

graph->visited[vertex] = 1;

printf("Visited %d \n", vertex);

while (temp != NULL) {

int connectedVertex = temp->vertex;

if (graph->visited[connectedVertex] == 0)

DFS(graph, connectedVertex);

}
temp = temp->next;

// Print the graph

void printGraph(struct Graph* graph) {

int v;

for (v = 0; v < graph->numVertices; v++) {

struct node* temp = graph->adjLists[v];

printf("\n Adjacency list of vertex %d\n ", v);

while (temp)

printf("%d -> ", temp->vertex);

temp = temp->next;

printf("\n");

int main()

printf("Enter number of vertices :\n");

scanf("%d",&n);

struct Graph* graph = createGraph(n);

addEdge(graph);
printGraph(graph);

DFS(graph, 0);

return 0;

Output-:
Enter number of vertices :

Enter edge [1] (-1 -1 to quit): 0 1

Enter edge [2] (-1 -1 to quit): 0 2

Enter edge [3] (-1 -1 to quit): 1 2

Enter edge [4] (-1 -1 to quit): 2 3

Enter edge [5] (-1 -1 to quit): 1 3

Enter edge [6] (-1 -1 to quit): -1 -1

Adjacency list of vertex 0

2 -> 1 ->

Adjacency list of vertex 1

3 -> 2 -> 0 ->

Adjacency list of vertex 2

3 -> 1 -> 0 ->

Adjacency list of vertex 3

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 * next;

};

struct node* head=NULL;

struct node *cn()

struct node* n;

n=(struct node *)malloc(sizeof(struct node));

return(n);

void createnode() //Function to create Linked list.

int i,n,x;

struct node * temp,*newnode;

printf("Enter size of linked list :\n");

scanf("%d",&n);
printf("Enter elements of linked list :\n");

for(i=1;i<=n;i++)

scanf("%d",&x);

if(head==NULL) //checking in linked list is empty

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 traverse() //Function to traverse Linked list.

{
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("\n\n1. Create Linked list.\n");

printf("2. Display Linked list.\n");

printf("3. Exit\n\n");

scanf("%d",&choice);

printf("\n");

switch(choice)
{

case 1: if(a==1)

printf("You have already created a Linked list.");

else

{ createnode();

a++;

break;

case 2: traverse();

break;

case 3: exit(0);

defalut: printf("Enter a valid choice\n");

}
Output-:
1. Create Linked list.

2. Display Linked list.

3. Exit

Enter size of linked list :

Enter elements of linked list :

1. Create Linked list.

2. Display Linked list.

3. Exit

1. Create Linked list.

2. Display Linked list.

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();

int precedence (char);

char stack[20],infix[20],prefix[20];

int top = -1;

int main()

printf("\nINPUT THE INFIX EXPRESSION : ");

scanf("%s",infix);

infix_to_prefix();

return 0;

void push(int pos)


{

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')

if(infix[i] >= 'a' && infix[i] <= 'z')

prefix[j] = infix[i];

j++;

i++;

else if(infix[i] == ')' || infix[i] == '}' || infix[i] == ']')

push(i);

i++;

else if(infix[i] == '(' || infix[i] == '{' || infix[i] == '[')


{

if(infix[i] == '(')

while(stack[top] != ')')

prefix[j] = pop();

j++;

pop();

i++;

else if(infix[i] == '[')

while(stack[top] != ']')

prefix[j] = pop();

j++;

pop();

i++;

else if(infix[i] == '{')

while(stack[top] != '}')

{
prefix[j] = pop();

j++;

pop();

i++;

}}

else

if(top == -1)

push(i);

i++;

else if( precedence(infix[i]) < precedence(stack[top]))

prefix[j] = pop();

j++;

while(precedence(stack[top]) > precedence(infix[i])){

prefix[j] = pop();

j++;

if(top < 0) {

break;
}}

push(i);

i++;

else if(precedence(infix[i]) >= precedence(stack[top]))

push(i);

i++;

}}}

while(top != -1)

prefix[j] = pop();

j++;

strrev(prefix);

prefix[j] = '\0';

printf("EQUIVALENT PREFIX NOTATION : %s ",prefix);

int precedence(char alpha)


{

if(alpha == '+' || alpha =='-')

return(1);

if(alpha == '*' || alpha =='/')

return(2);

return 0;

Output-:
Enter the infix expression: (A+B)*C-D/E

(A+B)*C-D/E

Equivalent Prefix Expression: -*+ABC/DE


B) Write a ‘C’ program to create two singly linked lists and concatenate one list at
the end of another list .
Solution:

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node* next;

};

struct node* head1=NULL;

struct node* head2=NULL;

void traverse(struct node** head);

struct node* cn()

struct node * n;

n=(struct node *)malloc(sizeof(struct node));

return(n);

void createnode(struct node **head,int n)

{
int x,i=1;

struct node * newnode,*temp;

printf("Enter elemets for linked list :\n");

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;

struct node *temp1,*temp2;

temp1=head1;

temp2=head2;

printf("\nConcatenated Linked List :\n");

while(temp1->next!=NULL)

temp1=temp1->next;

temp1->next=head2;

traverse(&head1);

void traverse(struct node** head) //Function to traverse Linked list.

struct node * temp3;

temp3=*head;
while(temp3!=NULL)

printf("%d\n",temp3->data);

temp3=temp3->next;

int main()

int h1,h2,choice;

printf("\nEnter size of first linked list :\n");

scanf("%d",&h1);

createnode(&head1,h1);

printf("\nEnter size of second node :\n");

scanf("%d",&h2);

createnode(&head2,h2);

concatination();

}
Output-:
Enter size of first linked list :

Enter elemets for linked list :

Enter size of second node :

Enter elemets for linked list :

Concatenated 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("\n1. Insert an element");

printf("\n2. Delete an element");

printf("\n3. Display Queue");

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);

default:printf("Enter valid choice\n");

void insert()

int item;

printf("\nEnter element :\n");

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;

printf("\nElement inserted successfully\n\n");

void delete()

int item;
if((front==-1)&&(rear==-1))

printf("Underflow");

return ;

else if(front==rear)

front=-1;

rear=-1;

else

front=front+1;

printf("Element popped successfully\n");

void display()

int i;

if(front==-1)

printf("\n Circular queue is empty.\n");

else
{

i=front;

printf("Circular queue elements are :\n");

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 your choice :

Enter element :

Element inserted successfully

1. Insert an element

2. Delete an element

3. Display Queue

4. Exit

Enter your choice :

Circular queue elements are :

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 * prev,*next;

};

struct node *head=NULL;

struct node * cn()

struct node * n;

n=(struct node *)malloc(sizeof(struct node));

return (n);

void createnode()

int i,n,x;

struct node *temp,*newnode;

printf("Enter size of linked list:\n");

scanf("%d",&n);

printf("Enter elements of linked list:\n");


for(i=1;i<=n;i++)

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()

struct node * temp;

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("\n\n1. Create Linked list.\n");

printf("2. Display.\n");

printf("3. Exit.\n\n");

scanf("%d",&choice);

switch(choice)

case 1: if(a==1)

printf("You have already created a Linked list.\n");

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.

Enter size of linked list:

Enter elements of linked list:

1. Create Linked list.

2. Display.

3. Exit.

1. Create Linked list.

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;

struct BST *left;

struct BST *right;

};

typedef struct BST NODE;

NODE *node;

NODE* createtree(NODE *node, int data)

if (node == NULL)

{
NODE *temp;

temp= (NODE*)malloc(sizeof(NODE));

temp->data = data;

temp->left = temp->right = NULL;

return temp;

if (data < (node->data))

node->left = createtree(node->left, data);

else if (data > node->data)

node -> right = createtree(node->right, data);

return node;

NODE* search(NODE *node, int data)

if(node == NULL)
printf("\nElement Not Found...!/n");

else if(data < node->data)

node->left=search(node->left, data);

else if(data > node->data)

node->right=search(node->right, data);

else

printf("\nElement Found is: %d", node->data);

return node;

void postorder(NODE *node)

if(node != NULL)

postorder(node->left);
postorder(node->right);

printf("%d\t", node->data);

int main()

int data, ch, i, n;

NODE *root=NULL;

while (ch!=4)

printf("\n\n1.Insertion in Binary Search Tree");

printf("\n2.Search Element in Binary Search Tree");

printf("\n3.Postorder");

printf("\n4.Exit\n");

printf("\nEnter your Choice: ");

scanf("%d", &ch);

switch (ch)

case 1: printf("\nEnter size of tree: " );

scanf("%d", &n);

printf("\nEnter the elements of tree)\n");


for(i=0; i<n; i++)

scanf("%d", &data);

root=createtree(root, data);

break;

case 2: printf("\nEnter the Element to Search: ");

scanf("%d", &data);

root=search(root, data);

break;

case 3: printf("\nPostorder Traversal: \n");

postorder(root);

break;

case 4: exit(0);

default: printf("\nEnter valid choice\n");

break;

}
Output-:
1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

3.Postorder

4.Exit

Enter your Choice: 1

Enter size of tree: 3

Enter the elements of tree)

1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

3.Postorder

4.Exit

Enter your Choice: 3

Postorder Traversal:

4 3 2

1.Insertion in Binary Search Tree

2.Search Element in Binary Search Tree

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* next;

};

struct node* head1=NULL;

struct node* head2=NULL;

void traverse(struct node** head);

struct node* cn()

struct node * n;

n=(struct node *)malloc(sizeof(struct node));

return(n);

void createnode(int arr[],int n)

{
int i;

struct node * newnode1,*temp1,*newnode2,*temp2;

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;

void traverse(struct node** head) //Function to traverse Linked list.

struct node * temp3;

temp3=*head;
while(temp3!=NULL)

printf("%d\n",temp3->data);

temp3=temp3->next;

int main()

int arr[100],i,n;

printf("Enter number of elements.\n");

scanf("%d",&n);

printf("Enter non-zero elements for Linked list.\n");

for(i=0;i<n;i++)

scanf("%d",&arr[i]);

createnode(arr,n);

printf("List 1(positive elements)\n");

traverse(&head1);

printf("List 2(negative elements)\n");

traverse(&head2);

}
Output-:
Enter number of elements.

Enter non-zero elements for Linked list.

List 1(positive elements)

List 2(negative 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;

struct binary_tree *left;

struct binary_tree *right;

};

typedef struct binary_tree NODE;

NODE *node=NULL;

NODE* init_tree(int data)

NODE* root = (NODE*) malloc (sizeof(NODE));

root->left = root->right = NULL;

root->info = data;

return root;

NODE* create_node(int data)

{
NODE* node = (NODE*) malloc (sizeof(NODE));

node->info = data;

node->left = node->right = NULL;

return node;

void inorder(NODE *node)

if(node != NULL)

inorder(node->left);

printf("%d\t", node->info);

inorder(node->right);

void preorder(NODE *node)

if(node != NULL)

printf("%d\t", node->info);

preorder(node->left);

preorder(node->right);
}

void postorder(NODE *node)

if(node != NULL)

postorder(node->left);

postorder(node->right);

printf("%d\t", node->info);

void main()

NODE* root = init_tree(10);

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:

// C code to perform circular linked list operations

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* addToEmpty(struct Node* last, int data) {

if (last != NULL) return last;

// allocate memory to the new node

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

// assign data to the new node

newNode->data = data;

// assign last to newNode

last = newNode;

// create link to iteself


last->next = last;

return last;

// add node to the front

struct Node* addFront(struct Node* last, int data) {

// check if the list is empty

if (last == NULL) return addToEmpty(last, data);

// allocate memory to the new node

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

// add data to the node

newNode->data = data;

// store the address of the current first node in the newNode

newNode->next = last->next;

// make newNode as head

last->next = newNode;

return last;

}
// add node to the end

struct Node* addEnd(struct Node* last, int data) {

// check if the node is empty

if (last == NULL) return addToEmpty(last, data);

// allocate memory to the new node

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

// add data to the node

newNode->data = data;

// store the address of the head node to next of newNode

newNode->next = last->next;

// point the current last node to the newNode

last->next = newNode;

// make newNode as the last node

last = newNode;

return last;

// insert node after a specific node

struct Node* addAfter(struct Node* last, int data, int item) {


// check if the list is empty

if (last == NULL) return NULL;

struct Node *newNode, *p;

p = last->next;

do {

// if the item is found, place newNode after it

if (p->data == item) {

// allocate memory to the new node

newNode = (struct Node*)malloc(sizeof(struct Node));

// add data to the node

newNode->data = data;

// make the next of the current node as the next of newNode

newNode->next = p->next;

// put newNode to the next of p

p->next = newNode;

// if p is the last node, make newNode as the last node

if (p == last) last = newNode;

return last;

}
p = p->next;

} while (p != last->next);

printf("\nThe given node is not present in the list");

return last;

// delete a node

void deleteNode(struct Node** last, int key) {

// if linked list is empty

if (*last == NULL) return;

// if the list contains only a single node

if ((*last)->data == key && (*last)->next == *last) {

free(*last);

*last = NULL;

return;

struct Node *temp = *last, *d;

// if last is to be deleted

if ((*last)->data == key) {

// find the node before the last node


while (temp->next != *last) temp = temp->next;

// point temp node to the next of last i.e. first node

temp->next = (*last)->next;

free(*last);

*last = temp->next;

// travel to the node to be deleted

while (temp->next != *last && temp->next->data != key) {

temp = temp->next;

// if node to be deleted was found

if (temp->next->data == key) {

d = temp->next;

temp->next = d->next;

free(d);

void traverse(struct Node* last) {

struct Node* p;

if (last == NULL) {
printf("The list is empty");

return;

p = last->next;

do {

printf("%d ", p->data);

p = p->next;

} while (p != last->next);

int main() {

struct Node* last = NULL;

last = addToEmpty(last, 6);

last = addEnd(last, 8);

last = addFront(last, 2);

last = addAfter(last, 10, 2);

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* next;

};

struct node *f = NULL;

struct node *r = NULL;

void enqueue(int d) //Insert elements in Queue

struct node* n;

n = (struct node*)malloc(sizeof(struct node));

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;

void dequeue() // Delete an element from Queue

struct node* t;

t = f;

if((f==NULL)&&(r==NULL))

printf("\nQueue is Empty");

else if(f == r){

f = r = NULL;

free(t);

else{

f = f->next;

r->next = f;

free(t);

void print(){ // Print the elements of Queue


struct node* 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;

printf("Enter Your Choice:-");

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:

printf("\nEnter your data:\n");

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:-

1 Insert the Data in Queue

2 show the Data in Queue

3 Delete the data from the Queue

4 Exit

Enter your data:

2225
1 Insert the Data in Queue

2 show the Data in Queue

3 Delete the data from the Queue

4 Exit

2225

1 Insert the Data in Queue

2 show the Data in Queue

3 Delete the data from the Queue

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;

struct node* link;

};

struct node* root = NULL;

void append()

struct node *temp;

temp = (struct node*)malloc(sizeof(struct node));

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() {

struct node* temp;

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() {

struct node *p, *q;

int temp;

for(p = root; p != NULL; p = p->link) {

for(q = p->link; q != NULL; q = q->link) {

if(p->data > q->data) {

temp = p->data;

p->data = q->data;

q->data = temp;

void main() {

int n, i;

printf("\nEnter the number of nodes: ");


scanf("%d", &n);

printf("\nEnter node data: \n");

for(i=0; i<n; i++)

append();

printf("\nOriginal linked list: \n");

display();

sort();

printf("\nSorted linked list is: \n");

display();

Output-:
Enter the number of nodes: 2

Enter node data:

Original linked list:

2 2

Sorted linked list is:

2 2

You might also like