DS LAB MANUAL - Deepa
DS LAB MANUAL - Deepa
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the structure for a day of the week
struct Day {
char *dayName;
int date;
char *activity;
};
// Function to create a new day entry
struct Day create() {
struct Day newDay;
newDay.dayName = (char *)malloc(20 * sizeof(char));
newDay.activity = (char *)malloc(100 * sizeof(char));
return newDay;
}
// Function to read data from the keyboard and populate the calendar
void read(struct Day calendar[], int numDays) {
for (int i = 0; i < numDays; i++) {
printf("Enter details for Day %d:\n", i + 1);
calendar[i] = create();
}
}
// Function to display the calendar
void display(struct Day calendar[], int numDays) {
printf("\nWeek's Activity Details Report:\n");
for (int i = 0; i < numDays; i++) {
printf("Day: %s, Date: %d, Activity: %s\n", calendar[i].dayName, calendar[i].date,
calendar[i].activity);
}
}
int main() {
int numDays = 7;
struct Day calendar[7];
read(calendar, numDays);
display(calendar, numDays);
#include <stdio.h>
#include <string.h>
int main() {
char str[MAX], pat[MAX], rep[MAX],result[200];
int i = 0, j = 0, k, found = 0;
if (pat[k] == '\0') {
found = 1;
for (k = 0; rep[k] != '\0'; k++) {
result[j++] = rep[k];
}
i += strlen(pat);
} else {
result[j++] = str[i++];
}
}
result[j] = '\0';
if (found) {
printf("After replacement: %s\n", result);
} else {
printf("Pattern not found.\n");
}
return 0;
}
OUTPUT:
PROGRAM 3
3. Develop a menu driven Program in C for the following operations on STACK of
Integers
(Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int a[MAX];
int top=-1;
void push(int item)
{
if(top==(MAX-1))
{
printf("STACK OVERFLOW\n");
return;
}
top=top+1;
a[top]=item;
}
int pop()
{
if(top==-1)
{
printf("STACK UNDERFLOW\n");
return -1;
}
int item=a[top];
top=top-1;
return item;
}
void display()
{
int i;
if(top==-1)
{
printf("STACK IS EMPTY");
return;
}
printf("STACK ELEMENTS ARE:\n");
for(i=top;i>=0;i--)
{
printf("%d ",a[i]);
}
printf("\n");
}
void cpalindrome()
{
int flag=1;
int i;
for(i=0; i<=top/2; i++)
{
if(a[i]!=a[top-i])
{
flag=0;
break;
}
}
if(flag==1)
{
printf("\n It is a Palindrome\n");
}
else
{
printf("\n It is not a Palindrome\n");
}
}
void main()
{
int choice,item;
for(;;)
{
printf("\nMENU\n");
printf("press 1:Push\t2:pop\t3:display\t4:cpalindrome\t5:exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter the item to be inserted\n");
scanf("%d",&item);
push(item);
break;
case 2: item=pop();
if(item!=-1)
printf("item popped: %d\n",item);
break;
case 3: display();
break;
case 4: cpalindrome();
break;
case 5: exit(0);
}
}
}
OUTPUT:
PROGRAM 4
#include<stdio.h>
#include<string.h>
#define MAX 10
char st[MAX];
int top=-1;
void push ( char c)
{
if(top==MAX-1)
printf("stack is full");
else
{
top = top + 1;
st[top ] = c;
}
}
void pop ( )
{
if(top == -1)
printf("stack is empty");
else
{
top = top - 1;
}
}
int prec(char c)
{
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
void infixToPostfix(char *s)
{
int n=strlen(s);
for (int i = 0; i < n; i++)
{
char c = s[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
printf("%c \t",c);
else if (c == '(')
push('(');
else if (c == ')')
{
while (st[top] != '(')
{
printf("%c \t",st[top]);
pop();
}
pop();
}
else {
while (top!=-1 && prec(c) <= prec(st[top]))
{
printf("%c \t",st[top]);
pop();
}
push(c);
}
}
while (top!=-1)
{
printf("%c \t",st[top]);
pop();
}
}
int main()
{
char exp[20];
printf("Enter the Infix expression:");
scanf("%s",exp);
infixToPostfix(exp);
return 0;
}
OUTPUT:
PROGRAM 5
5a. Develop a Program in C for the following Stack Applications
Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,^
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#define MAX 100
double pop() {
double del;
if (top == -1) {
printf("\nStack Underflow!!!");
} else {
del = s[top];
top = top - 1;
return del;
}
}
int main() {
char suffix[50];
int i;
double val, op1, op2, res;
OUTPUT:
5 b. Solving Tower of Hanoi problem with n disks
#include <stdio.h>
int main() {
int n;
printf("\nEnter the number of disks: ");
scanf("%d", &n);
tower_hanoi(n, 'A', 'B', 'C');
return 0;
}
Output:
PROGRAM 6
#include <stdio.h>
#define MAX 5
if (f == (r + 1) % MAX) {
printf("\nQueue Overflow!!!");
return;
} else if (f == -1) {
f = 0;
}
r = (r + 1) % MAX;
cq[r] = elem;
}
int main() {
int ch;
char cq[MAX];
for (;;) {
printf("\n---------------------------------------------");
printf("\nCIRCULAR QUEUE OPERATIONS");
printf("\n1: Insert \n2: Delete \n3: Display \n4: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert(cq);
break;
case 2:
delete(cq);
break;
case 3:
display(cq);
break;
case 4:
return 0;
default:
printf("\nInvalid Choice!!!\n");
}
}
return 0;
}
OUTPUT
PROGRAM 7
7. Develop a menu driven program in C for the following operations on Singly Linked List
(SLL) of Student Data with the fields: USN, Name, Programme, Sem, PhNo.
#include <stdio.h>
#include <stdlib.h>
struct student {
char usn[20];
char name[20];
char program[20];
int sem;
long int phno;
struct student *link;
};
STUDENT create() {
STUDENT getnode;
getnode = (STUDENT)malloc(sizeof(struct student));
if (getnode == NULL) {
printf("\nMemory could not be allocated!!!");
}
printf("\nEnter the details of Student");
printf("\nEnter the usn: ");
scanf("%s", getnode->usn);
printf("\nEnter the name: ");
scanf("%s", getnode->name);
printf("\nEnter the branch: ");
scanf("%s", getnode->program);
printf("\nEnter the sem: ");
scanf("%d", &getnode->sem);
printf("\nEnter the phno: ");
scanf("%ld", &getnode->phno);
getnode->link = NULL;
return getnode;
}
void insert_front() {
STUDENT node;
node = create();
if (start == NULL) {
start = node;
} else {
node->link = start;
start = node;
}
}
void delete_front() {
STUDENT temp;
if (start == NULL) {
printf("\nList is Empty");
} else {
temp = start;
start = temp->link;
printf("\nThe deleted student usn is %s", temp->usn);
free(temp);
}
}
void create_list() {
int n, i;
printf("\nEnter the number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
insert_front();
}
}
void status() {
STUDENT temp;
int count = 0;
if (start == NULL) {
printf("\nList is Empty");
return;
}
temp = start;
printf("\nThe Student details are: ");
while (temp != NULL) {
printf("\n%s\n%s\n%s\n%d\n%ld\n", temp->usn, temp->name, temp->program, temp-
>sem, temp->phno);
temp = temp->link;
count++;
}
printf("\nThe number of nodes are: %d", count);
}
void insert_end() {
STUDENT node, temp;
node = create();
if (start == NULL) {
start = node;
} else {
temp = start;
while (temp->link != NULL) {
temp = temp->link;
}
temp->link = node;
}
}
void delete_end() {
STUDENT temp, prev;
temp = start;
if (temp == NULL) {
printf("\nList is Empty");
} else if (temp->link == NULL) {
printf("\nThe deleted student usn is %s", temp->usn);
free(temp);
start = NULL;
} else {
while (temp->link != NULL) {
prev = temp;
temp = temp->link;
}
printf("\nThe deleted student usn is %s", temp->usn);
free(temp);
prev->link = NULL;
}
}
void stack_demo() {
int ch;
for (;;) {
printf("\n---------------------------------------------");
printf("\nSTACK OPERATIONS");
printf("\n1: Insert End \n2: Delete End \n3: Status of Stack \n4: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert_end();
break;
case 2:
delete_end();
break;
case 3:
status();
break;
case 4:
return;
default:
printf("\nInvalid Choice!!!");
}
}
}
int main() {
int ch;
for (;;) {
printf("\n---------------------------------------------");
printf("\nSINGLY LINKED LIST OPERATIONS");
printf("\n 1: Create List \n 2: Status of List \n 3: Insert End \n 4: Delete End \n 5: Insert
Front \n 6: Delete Front \n 7: Stack Demo \n 8: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
create_list();
break;
case 2:
status();
break;
case 3:
insert_end();
break;
case 4:
delete_end();
break;
case 5:
insert_front();
break;
case 6:
delete_front();
break;
case 7:
stack_demo();
break;
case 8:
return 0;
default:
printf("\nInvalid Choice!!!");
}
}
return 0;
}
SAMPLE OUTPUT:
PROGRAM 8
8. Develop a menu driven Program in C for the following operations on Doubly Linked List
(DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit
#include <stdio.h>
#include <stdlib.h>
struct employee {
char ssn[20];
char name[20];
char dept[20];
char designation[20];
int salary;
long int phno;
struct employee *llink, *rlink;
};
EMPLOYEE create() {
EMPLOYEE getnode;
getnode = (EMPLOYEE)malloc(sizeof(struct employee));
if (getnode == NULL) {
printf("\nMemory couldn't be allocated!!!");
return 0;
}
printf("\nEnter the details of Employee");
printf("\nEnter the ssn: ");
scanf("%s", getnode->ssn);
printf("\nEnter the name: ");
scanf("%s", getnode->name);
printf("\nEnter the department: ");
scanf("%s", getnode->dept);
printf("\nEnter the designation: ");
scanf("%s", getnode->designation);
printf("\nEnter the salary: ");
scanf("%d", &getnode->salary);
printf("\nEnter the phno: ");
scanf("%ld", &getnode->phno);
getnode->llink = NULL;
getnode->rlink = NULL;
return getnode;
}
void insert_end() {
EMPLOYEE node, temp;
node = create();
if (start == NULL) {
start = node;
} else {
temp = start;
while (temp->rlink != NULL) {
temp = temp->rlink;
}
temp->rlink = node;
node->llink = temp;
}
}
void delete_end() {
EMPLOYEE temp, prev;
temp = start;
if (temp == NULL) {
printf("\nList is Empty");
} else if (temp->rlink == NULL) {
printf("\nThe deleted employee ssn is %s", temp->ssn);
free(temp);
start = NULL;
} else {
while (temp->rlink != NULL) {
prev = temp;
temp = temp->rlink;
}
prev->rlink = NULL;
printf("\nThe deleted employee ssn is %s", temp->ssn);
free(temp);
}
}
void create_list() {
int n, i;
printf("\nEnter the number of employees: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
insert_end();
}
}
void status() {
EMPLOYEE temp;
int count = 0;
if (start == NULL) {
printf("\nList is Empty");
return;
}
temp = start;
printf("\nThe Employee details are: ");
while (temp != NULL) {
printf("\n%s\n%s\n%s\n%s\n%d\n%ld\n", temp->ssn, temp->name, temp->dept, temp-
>designation, temp->salary, temp->phno);
temp = temp->rlink;
count++;
}
printf("\nThe number of nodes are: %d", count);
}
void insert_front() {
EMPLOYEE node;
node = create();
if (start == NULL) {
start = node;
} else {
node->rlink = start;
start->llink = node;
start = node;
}
}
void delete_front() {
EMPLOYEE temp;
temp = start;
if (temp == NULL) {
printf("\nList is Empty");
} else if (temp->rlink == NULL) {
printf("\nThe deleted employee ssn is %s", temp->ssn);
free(temp);
start = NULL;
} else {
start = temp->rlink;
start->llink = NULL;
printf("\nThe deleted employee ssn is %s", temp->ssn);
free(temp);
}
}
void double_ended_queue() {
int ch;
for (;;) {
printf("\n---------------------------------------------");
printf("\nDOUBLE ENDED QUEUE OPERATIONS");
printf("\n1: Insert Rear \n2: Delete Rear \n3: Insert Front \n4: Delete Front \n5: Display
\n6: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
insert_end();
break;
case 2:
delete_end();
break;
case 3:
insert_front();
break;
case 4:
delete_front();
break;
case 5:
status();
break;
case 6:
return;
default:
printf("\nInvalid Choice!!!");
}
}
}
int main() {
int ch;
for (;;) {
printf("\n---------------------------------------------");
printf("\nDOUBLY LINKED LIST OPERATIONS");
printf("\n1: Create List \n2: Status of List \n3: Insert End \n4: Delete End \n5: Insert
Front \n6: Delete Front \n7: Double Ended Queue Demo \n8: Exit");
printf("\n---------------------------------------------");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
create_list();
break;
case 2:
status();
break;
case 3:
insert_end();
break;
case 4:
delete_end();
break;
case 5:
insert_front();
break;
case 6:
delete_front();
break;
case 7:
double_ended_queue();
break;
case 8:
return 0;
default:
printf("\nInvalid Choice!!!");
}
}
return 0;
}
SAMPLE OUTPUT:
PROGRAM 9
9. Develop a Program in C for the following operations on Singly Circular Linked List
(SCLL) with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the
result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations.
9A.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct polynomial {
int coeff, x, y, z;
struct polynomial *link;
};
POLYNOMIAL create() {
POLYNOMIAL getnode;
getnode = (POLYNOMIAL)malloc(sizeof(struct polynomial));
if (getnode == NULL) {
printf("\nMemory couldn't be allocated!!!");
return 0;
}
return getnode;
}
POLYNOMIAL insert(POLYNOMIAL head, int c, int px, int py, int pz) {
POLYNOMIAL node, temp;
node = create();
node->coeff = c;
node->x = px;
node->y = py;
node->z = pz;
node->link = NULL;
temp = head->link;
while (temp->link != head) { /* Traverse till the end of the list. */
temp = temp->link;
}
temp->link = node; /* Attach the node to the end of the list. */
node->link = head; /* Assign the address of the head to node's link. */
return head;
}
int main() {
POLYNOMIAL head;
int res;
head = create();
head->link = head;
printf("\nEnter the polynomial to be evaluated: ");
head = input_polynomial(head);
printf("\nThe given polynomial is: ");
display(head);
res = evaluate_polynomial(head);
printf("\nThe result after evaluation is: %d", res);
return 0;
}
Sample output:
9B.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct polynomial {
int coeff, x, y, z;
struct polynomial *link;
};
/* Function definition to insert a polynomial in a singly circular linked list with header node. */
POLYNOMIAL insert_end(POLYNOMIAL head, int c, int px, int py, int pz) {
POLYNOMIAL node, temp;
node = create();
node->coeff = c;
node->x = px;
node->y = py;
node->z = pz;
node->link = NULL;
temp = head->link;
while (temp->link != head) { /* Traverse till the end of the list. */
temp = temp->link;
}
temp->link = node; /* Attach the node to the end of the list. */
node->link = head; /* Assign the address of the head to node's link. */
return head;
}
int main() {
POLYNOMIAL head1, head2, head3;
/* Create a header node for polynomial1 whose link field points to the address of itself initially. */
head1 = create();
head1->link = head1;
/* Create a header node for polynomial2 whose link field points to the address of itself initially. */
head2 = create();
head2->link = head2;
/* Create a header node for sum of polynomial whose link field points to the address of itself
initially. */
head3 = create();
head3->link = head3;
return 0;
}
SAMPLE OUTPUT:
PROGRAM 10
10. Develop a menu driven Program in C for the following operations on Binary Search Tree
(BST) of Integers.
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
#include <stdio.h>
#include <stdlib.h>
struct BST {
int data;
struct BST *lchild;
struct BST *rchild;
};
NODE create() {
NODE temp;
temp = (NODE)malloc(sizeof(struct BST));
printf("Enter The value: ");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void main() {
int ch, i, n;
NODE root = NULL, newnode;
while (1) {
printf("\n\n~~~~BST MENU~~~~");
printf("\n1. Create a BST");
printf("\n2. Search");
printf("\n3. BST Traversals");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter the number of elements: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
}
break;
case 2:
search(root);
break;
case 3:
if (root == NULL)
printf("\nTree Is Not Created");
else {
printf("\nThe Preorder display: ");
preorder(root);
printf("\nThe Inorder display: ");
inorder(root);
printf("\nThe Postorder display: ");
postorder(root);
}
break;
case 4:
exit(0);
}
}
}
SAMPLE OUTPUT:
PROGRAM 11
#include <stdio.h>
#include <stdlib.h>
void bfs(int v) {
int i, cur;
bvisited[v] = 1;
q[++rear] = v;
void dfs(int v) {
int i;
dvisited[v] = 1;
s[++top] = v;
int main() {
int ch, start, i, j;
printf("\nEnter the number of vertices in graph: ");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
}
while (1) {
printf("\nMENU");
printf("\n==> 1. BFS");
printf("\n==> 2. DFS");
printf("\n==> 3. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);
break;
case 2:
printf("\nNodes reachable from starting vertex %d are: ", start);
dfs(start);
break;
case 3:
exit(0);
default:
printf("\nPlease enter a valid choice:");
}
}
return 0;
}
SAMPLE OUTPUT:
PROGRAM 12
12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of M
memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the
keys in K and addresses in L are Integers.
Develop a Program in C that uses Hash function H: K →L as H(K)=K mod m (remainder
method), and implement hashing technique to map a given key K to the address space L.
Resolve the collision (if any) using linear probing
#include <stdio.h>
#include <stdlib.h>
int key[20], n, m;
int *ht, index;
int count = 0;
void display() {
int i;
if (count == 0) {
printf("\nHash Table is empty");
return;
}
void main() {
int i;
printf("\nEnter the two-digit memory locations (m) for hash table: ");
scanf("%d", &m);
Sample output: