Lab Programs
Lab Programs
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
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>
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
// 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;
}
}
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 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:
11
Node* new_node = createNode(new_data);
12
// Driver program to test the stack implementation
int main() {
// Creating a stack
Stack stack;
initializeStack(&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");
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 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;
}
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);
}
20
Output:
21
Program 8:
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;
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>
// Driver code
int main() {
int N = 3;
Output:
23
Program 9:
Source Code:
#include<stdio.h>
#include<stdlib.h>
24
}
}
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];
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