Program 1:
Write a C Program to create a student record structure to store, N records, each record having the
structure shown below: USN, Student Name and Semester. Write necessary functions
a. To display all the records in the file.
b. To search for a specific record based on the USN. In case the record is not found, suitable
message should be displayed. Both the options in this case must be demonstrated. (Use pointer to
structure for dynamic memory allocation)
Source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student{
int USN;
char student_name[20];
int semester;
};
struct Student *n1;
int main()
{
int n = 2;
int I;
int ID;
n1= (struct Student *) malloc(n*sizeof(struct Student));
for (i=0;i<n; i++)
{
printf("Enter the USN:");
scanf("%d", &n1[i].USN );
printf("Enter the student name:");
scanf("%s", n1[i].student_name );
printf("Enter the semester:");
scanf("%d", &n1[i].semester);
}
for (i=0; i<n; i++)
{
printf( "The student Id: %d\n",n1[i].USN );
printf("The student name is:\t%s\n",n1[i].student_name );
printf("Enter the semester:\t%d\n", n1[i].semester);
}
printf("***************");
printf("Enter the USN to be searched:");
scanf("%d", &ID);
1
for (i=0;i<n; i++)
{
if (ID== n1[i].USN )
{
printf( "The student Id: %d\n",n1[i].USN );
printf("The student name is:\t%s\n",n1[i].student_name );
printf("Enter the semester:\t%d\n", n1[i].semester);
}
}
}
2
Program 2:
Write a C Program to construct a stack of integers and perform the following operations on it:
a. Push
b. Pop
c. Display The program should print appropriate messages for stack overflow, stack underflow, and stack
empty
Source Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 4
int top = -1, inp_array[SIZE];
void push();
void pop();
void show();
int main()
{
int choice;
while (1)
{
printf("\nPerform operations on the stack:");
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
printf("\n\nEnter the choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice!!");
}
3
}
}
void push()
{
int x;
if (top == SIZE - 1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter the element to be added onto the stack: ");
scanf("%d", &x);
top = top + 1;
inp_array[top] = x;
}
}
void pop()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d", inp_array[top]);
top = top - 1;
}
}
void show()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nElements present in the stack: \n");
for (int i = top; i >= 0; --i)
printf("%d\n", inp_array[i]);
}
}
4
Output:
5
Program 3
Write a C Program to convert and print a given valid parenthesized infix arithmetic expression to postfix
expression. The expression consists of single character operands and the binary operators + (plus), -
(minus), * (multiply) and / (divide).
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Function to return precedence of operators
int prec(char c) {
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
// Function to perform infix to postfix conversion
void infixToPostfix(char* exp) {
int len = strlen(exp);
char result[len + 1];
char stack[len];
int j = 0;
int top = -1;
for (int i = 0; i < len; i++) {
char c = exp[i];
// If the scanned character is an operand, add it to the output string.
if (isalnum(c))
result[j++] = c;
// If the scanned character is an ‘(‘, push it to the stack.
else if (c == '(')
stack[++top] = '(';
// If the scanned character is an ‘)’, pop and add to the output string from the stack
// until an ‘(‘ is encountered.
6
else if (c == ')') {
while (top != -1 && stack[top] != '(') {
result[j++] = stack[top--];
}
top--;
}
// If an operator is scanned
else {
while (top != -1 && (prec(c) < prec(stack[top]) ||
prec(c) == prec(stack[top]))) {
result[j++] = stack[top--];
}
stack[++top] = c;
}
}
// Pop all the remaining elements from the stack
while (top != -1) {
result[j++] = stack[top--];
}
result[j] = '\0';
printf("%s\n", result);
}
int main() {
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
infixToPostfix(exp);
return 0;
}
Output:
7
Program 4:
Write a C Program to simulate the working of a queue of integers using an array. Provide
the following operations:
a. Insert
b. Delete
c. Display
Source Code:
#include<stdio.h>
#include<stdlib.h>
void insert();
void dequeue();
void display();
int front = -1, rear = -1 ,maxsize;
int queue[100];
int main ()
{
int choice;
printf("\n Enter the size of QUEUE : ");
scanf("%d",&maxsize);
printf("\n QUEUE OPERATIONS USING ARRAY");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit");
while(choice != 4)
{
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
8
return 0;
}
void insert()
{
int item;
printf("\nEnter the element\n");
scanf("\n%d",&item);
if(rear == maxsize-1)
{
printf("\nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
front = 0;
rear = 0;
}
else
{
rear = rear+1;
}
queue[rear] = item;
printf("\nValue inserted ");
}
void dequeue()
{
int item;
if (front == -1 || front > rear)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
9
}
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{
printf("\n Elements in the queue are\n");
for(i=front;i<=rear;i++)
{
printf("\n%d",queue[i]);
}
}
}
Output:
10
Program 5:
Write a C Program using dynamic variables and pointers to construct a stack of integers using singly
linked list and to perform the following operations:
a. Push
b. Pop
c. Display the program should print appropriate messages for stack overflow and stack empty.
Source Code:
// C program to implement a stack using singly linked list
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// Struct representing a node in the linked list
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* createNode(int new_data) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
// Struct to implement stack using a singly linked list
typedef struct Stack {
Node* head;
} Stack;
// Constructor to initialize the stack
void initializeStack(Stack* stack) { stack->head = NULL; }
// Function to check if the stack is empty
int isEmpty(Stack* stack) {
// If head is NULL, the stack is empty
return stack->head == NULL;
}
// Function to push an element onto the stack
void push(Stack* stack, int new_data) {
// Create a new node with given data
11
Node* new_node = createNode(new_data);
// Check if memory allocation for the new node failed
if (!new_node) {
printf("\nStack Overflow");
return;
}
// Link the new node to the current top node
new_node->next = stack->head;
// Update the top to the new node
stack->head = new_node;
}
// Function to remove the top element from the stack
void pop(Stack* stack) {
// Check for stack underflow
if (isEmpty(stack)) {
printf("\nStack Underflow\n");
return;
}
else {
// Assign the current top to a temporary variable
Node* temp = stack->head;
// Update the top to the next node
stack->head = stack->head->next;
// Deallocate the memory of the old top node
free(temp);
}
}
// Function to return the top element of the stack
int peek(Stack* stack) {
// If stack is not empty, return the top element
if (!isEmpty(stack))
return stack->head->data;
else {
printf("\nStack is empty");
return INT_MIN;
}
}
12
// Driver program to test the stack implementation
int main() {
// Creating a stack
Stack stack;
initializeStack(&stack);
// Push elements onto the stack
push(&stack, 11);
push(&stack, 22);
push(&stack, 33);
push(&stack, 44);
// Print top element of the stack
printf("Top element is %d\n", peek(&stack));
// removing two elemements from the top
printf("Removing two elements...\n");
pop(&stack);
pop(&stack);
// Print top element of the stack
printf("Top element is %d\n", peek(&stack));
return 0;
}
Output:
13
Program 6:
Write a C Program to support the following operations on a doubly linked list where each node consists
of integers:
a. Create a doubly linked list by adding each node at the front.
b. Insert a new node to the left of the node whose key value is read as an input
c. Delete the node of a given data, if it is found, otherwise display appropriate message. d. Display the
contents of the list. (Note: Only either (a,b and d) or (a, c and d) may be asked in the examination)
Source Code:
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
struct stud
{
char ssn[11],name[15],desg[15],phno[11],dept[15];
float sal;
struct stud *next,*prev;
}*f=NULL,*r=NULL,*t=NULL;
void ins(int ch)
{
t=(struct stud*)malloc(sizeof(struct stud));
printf("\nEnter SSN:");
scanf("%s",t->ssn);
printf("Enter Name:");
scanf("%s",t->name);
printf("Enter Dept:");
scanf("%s",t->dept);
printf("Enter desg:");
scanf("%s",t->desg);
printf("Enter Phno:");
scanf("%s",t->phno);
printf("Enter salary:");
scanf("%f",&t->sal);
t->next=t->prev=NULL;
if(!r)
f=r=t;
else
{
if(ch)
{
r->next=t;
t->prev=r;
r=t;
}
14
else
{
t->next=f;
f->prev=t;
f=t;
}
}
}
void disp()
{
if(!f)
printf("\nList Empty!!!");
else
printf("\nList elements are:\n");
for(t=f;t;t=t->next)
printf("\nSSN:%s\nName:%s\nDept:%s\nDesg:%s\nPhno:%s\nSalary:%f\n",t->ssn,t->name,t-
>dept,t->desg,t->phno,t->sal);
}
void main()
{
int ch,n,i;
printf("\n........Menu..........,\n");
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert at end\n");
printf("4.Delete at end\n");
printf("5.Insert at beg\n");
printf("6.Delete at beg\n");
printf("7.Exit\n");
while(1)
{
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter no. of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
ins(0);
break;
case 2:disp();break;
case 3:ins(1);break;
case 4:ins(0);break;
case 5:exit(0);
default:printf("\nInvalid choice!!!!");
}
}
15
Output:
16
Program 7:
Write a C Program
a. To construct a binary search tree of integers.
b. To traverse the tree using all the methods i.e., inorder, preorder, and postorder.
c. To display the elements in the tree.
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Inorder Traversal\n");
printf("3 - Preorder Traversal\n");
printf("4- Postorder Traversal\n");
17
printf("5 - Exit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
/* To insert a node in the tree */
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
/* To create a node */
void create()
{
int data;
18
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;
}
/* Function to search the appropriate position to insert the new node */
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;
}
/* recursive function to perform inorder traversal of tree */
void inorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
printf("%d -> ", t->value);
if (t->r != NULL)
inorder(t->r);
}
/* To find the preorder traversal */
void preorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display");
return;
19
}
printf("%d -> ", t->value);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}
/* To find the postorder traversal */
void postorder(struct btnode *t)
{
if (root == NULL)
{
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->value);
}
20
Output:
21
Program 8:
Write recursive C Programs for
a. Searching an element on a given list of integers using the Binary Search method.
b. Solving the Towers of Hanoi problem.
a. Source Code
#include <stdio.h>
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == target) {
return mid; // Element found
} else if (arr[mid] < target) {
low = mid + 1; // Search right half
} else {
high = mid - 1; // Search left half
}
}
return -1; // Element not found
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 31};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 12;
int result = binarySearch(arr, n, target);
if (result != -1) {
printf("Element is found at index %d\n", result);
} else {
printf("Element is not present in array\n");
}
return 0;
}
Output:
22
b. Source Code:
#include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
if (n == 0) {
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
// Driver code
int main() {
int N = 3;
// A, B and C are names of rods
towerOfHanoi(N, 'A', 'C', 'B');
return 0;
}
Output:
23
Program 9:
a. Write a program to traverse a graph using BFS method.
b. Write a program to check whether given graph is connected or not using DFS method.
Source Code:
#include<stdio.h>
#include<stdlib.h>
int a[10][10], n, m, i, j, source, s[10], b[10];
int visited[10];
void create()
{
printf("\nEnter the number of vertices of the digraph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix of the graph:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d", &a[i][j]);
}
void bfs()
{
int q[10], u, front=0, rear=-1;
printf("\nEnter the source vertex to find other nodes reachable or not: ");
scanf("%d", &source);
q[++rear] = source;
visited[source] = 1;
printf("\nThe reachable vertices are: ");
while(front<=rear)
{
u = q[front++];
for(i=1; i<=n; i++)
{
if(a[u][i] == 1 && visited[i] == 0)
{
q[++rear] = i;
visited[i] = 1;
printf("\n%d", i);
}
24
}
}
void dfs(int source)
{
int v, top = -1;
s[++top] = 1;
b[source] = 1;
for(v=1; v<=n; v++)
{
if(a[source][v] == 1 && b[v] == 0)
{
printf("\n%d -> %d", source, v);
dfs(v);
}
}
}
void main()
{
int ch;
while(1)
{
printf("\n1.Create Graph\n2.BFS\n3.Check graph connected or
not(DFS)\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: create(); break;
case 2: bfs();
for(i=1;i<=n;i++)
if(visited[i]==0)
printf("\nThe vertex that is not rechable %d" ,i);
break;
case 3: printf("\nEnter the source vertex to find the connectivity: ");
scanf("%d",&source);
m=1;
dfs(source);
for(i=1;i<=n;i++)
{
25
if(b[i]==0)
m=0;
}
if(m==1)
printf("\nGraph is Connected");
else
printf("\nGraph is not Connected");
break;
default: exit(0);
}
}
}
Output:
26
Program 10:
Design and develop a program in C that uses Hash Function H:K->L as H(K)=K mod
m(reminder method) and implement hashing technique to map a given key K to the address
space L. Resolve the collision (if any) using linear probing.
Source Code:
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct employee
{
int id;
char name[20];
};
typedef struct employee EMP;
EMP emp[MAX];
//int a[MAX];
int create(int num)
{
return num%10;
}
int getemp(EMP e[],int key)
{
printf("\nEnter emp id,name: ");
scanf("%d%s",&e[key].id,emp[key].name);
return key;
}
void display(EMP emp[])
{
int i;
printf("\nThe hash table is:\n");
printf("HT key\tEmp ID\tEmp name\n");
for(i=0;i<MAX;i++)
printf("\t%d\t%d\t%s\n",i,emp[i].id,emp[i].name);
}
void linearprobe(int a[MAX],int key,int num)
{
int flag=0,i,count=0;
27
if(a[key]==-1)
a[key]=getemp(emp,key);
else
{
printf("\nCollision detected\n");
i=0;
while(i<MAX)
{
if(a[i]!=-1)
count++;
i++;
}
printf("\nCollision avoided successful using Linear probing\n");
if(count==MAX)
{
printf("\nHash Table if full");
display(emp);
exit(1);
}
for(i=key;i<MAX;i++)
{
if(a[i]==-1)
{
a[i]=num;
flag=1;
break;
}
}
i=0;
while((i<key) && (flag==0))
{
if(a[i]==-1)
{
a[i]=num;
flag=1;
break;
}
i++;
}
}
}
void main()
{
int a[MAX],num,key,i,ans=1;
for(i=0;i<MAX;i++)
28
{
a[i]=-1;
}
do
{
printf("\nEnter the data: ");
scanf("%d",&num);
key=create(num);
linearprobe(a,key,num);
printf("\nDo you wish to Continue ?(1/0)");
scanf("%d",&ans);
} while(ans);
display(emp);
}
29