DSA Lab Programs
DSA Lab Programs
Program 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>
int stack[MAX];
int top = -1; // Initialize the top of the stack to -1
// Function declarations
void pushMultiple(int n);
int pop();
int isPalindrome();
void display();
void checkOverflow();
void checkUnderflow();
int isEmpty();
int isFull();
int main() {
int choice, element, n;
do {
printf("\n\nMenu:");
printf("\n1. Push Elements onto Stack");
printf("\n2. Pop an Element from Stack");
printf("\n3. Check if Stack content forms a Palindrome");
printf("\n4. Demonstrate Overflow");
printf("\n5. Demonstrate Underflow");
printf("\n6. Display the Stack");
printf("\n7. Exit");
printf("\nEnter your choice: ");
Data Structure and Applications (BCSL305) 2
scanf("%d", &choice);
switch(choice) {
case 1:
printf("How many elements do you want to push? ");
scanf("%d", &n);
pushMultiple(n);
break;
case 2:
element = pop();
if (element != -1)
printf("Popped element: %d\n", element);
break;
case 3:
if (isPalindrome())
printf("The stack content forms a Palindrome.\n");
else
printf("The stack content does not form a Palindrome.\n");
break;
case 4:
checkOverflow();
break;
case 5:
checkUnderflow();
break;
case 6:
display();
break;
case 7:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 7);
return 0;
}
return;
}
printf("Enter element %d: ", i + 1);
scanf("%d", &element);
stack[++top] = element;
printf("Pushed %d onto the stack.\n", element);
}
}
int i, palindrome = 1;
for (i = 0; i <= top / 2; i++) {
if (stack[i] != stack[top - i]) {
palindrome = 0;
break;
}
}
return palindrome;
}
#include <stdio.h>
#include <ctype.h> // For isalnum()
#include <string.h> // For strlen()
char stack[MAX];
int top = -1;
// Pop function to remove and return the top element from the stack
char pop() {
if (top != -1) {
return stack[top--];
Data Structure and Applications (BCSL305) 6
}
return -1; // Return -1 if the stack is empty
}
push(ch);
}
}
int main() {
char infix[MAX], postfix[MAX];
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <math.h>
int stack[MAX];
int top = -1;
void push(int x) {
Data Structure and Applications (BCSL305) 8
if (top == MAX - 1) {
printf("Stack overflow\n");
return;
}
stack[++top] = x;
}
int pop() {
if (top == -1) {
printf("Stack underflow\n");
return -1;
}
return stack[top--];
}
int main() {
char exp[MAX];
printf("Enter a postfix expression: ");
scanf("%s", exp);
printf("Result of evaluation: %d\n", evaluatePostfix(exp));
return 0;
}
Data Structure and Applications (BCSL305) 9
B.
#include <stdio.h>
int main() {
int n;
printf("Enter the number of disks: ");
scanf("%d", &n);
towerOfHanoi(n, 'A', 'C', 'B'); // A, B, and C are names of rods
return 0;
}
Sample Inputs:
For evaluating a postfix (suffix) expression using the program in part a, you can input an
expression where the operands are single-digit numbers and the operators are from the set {+, -,
*, /, %, ^}.
Example Input 1:
Enter a postfix expression: 23*54*+
Explanation:
Expected Output:
Result of evaluation: 26
Example Input 2:
Explanation:
Expected Output:
rust
Copy code
Result of evaluation: 35
Program 6: Develop a menu driven Program in C for the following operations on Circular
QUEUE of Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit Support the program with appropriate functions for each of the above operations
Data Structure and Applications (BCSL305) 11
#include <stdio.h>
#include <stdlib.h>
char queue[MAX];
int front = -1, rear = -1;
if (isEmpty()) {
front = 0;
}
if (front == rear) {
// Queue has only one element, reset queue after deletion
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
do {
printf("\nMenu:\n");
printf("1. Insert an Element into Circular Queue\n");
printf("2. Delete an Element from Circular Queue\n");
printf("3. Demonstrate Overflow and Underflow\n");
printf("4. Display the status of Circular Queue\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter an element to insert: ");
Data Structure and Applications (BCSL305) 13
case 2:
deleteElement();
break;
case 3:
printf("Demonstrating Overflow and Underflow situations:\n");
// Overflow Test
if (isFull()) {
printf("Queue is full (Overflow)! Cannot insert new elements.\n");
} else {
printf("Queue is not full yet. Insert more elements to reach Overflow.\n");
}
// Underflow Test
if (isEmpty()) {
printf("Queue is empty (Underflow)! No elements to delete.\n");
} else {
printf("Queue is not empty yet. Continue deleting elements to reach
Underflow.\n");
}
break;
case 4:
displayQueue();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
} while (choice != 5);
return 0;
}
Sample Output:
Data Structure and Applications (BCSL305) 14
Menu:
1. Insert an Element into Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow and Underflow
4. Display the status of Circular Queue
5. Exit
Enter your choice: 1
Enter an element to insert: A
Inserted 'A' into the queue.
Menu:
1. Insert an Element into Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow and Underflow
4. Display the status of Circular Queue
5. Exit
Enter your choice: 4
Queue elements are: A
Menu:
1. Insert an Element into Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow and Underflow
4. Display the status of Circular Queue
5. Exit
Enter your choice: 2
Deleted 'A' from the queue.
Menu:
1. Insert an Element into Circular Queue
2. Delete an Element from Circular Queue
3. Demonstrate Overflow and Underflow
4. Display the status of Circular Queue
5. Exit
Enter your choice: 4
Queue is Empty!
Data Structure and Applications (BCSL305) 15
Program 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
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit
10-> 20->30-> 40
10<-20<-30<-40
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function prototypes
void createList(int n);
void displayList();
void insertEnd();
void deleteEnd();
void insertFront();
Data Structure and Applications (BCSL305) 16
void deleteFront();
void menu();
int main() {
int choice;
menu();
while (1) {
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
{
int n;
printf("Enter the number of students to create the list: ");
scanf("%d", &n);
createList(n);
}
break;
case 2:
displayList();
break;
case 3:
insertEnd();
break;
case 4:
deleteEnd();
break;
case 5:
insertFront();
break;
case 6:
deleteFront();
break;
case 7:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Data Structure and Applications (BCSL305) 17
// Function to display the singly linked list and count the number of nodes
void displayList() {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
newStudent->next = NULL;
if (head == NULL) {
head = newStudent;
} else {
Student *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newStudent;
}
}
void deleteEnd() {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
if (head->next == NULL) {
free(head);
head = NULL;
} else {
Student *temp = head;
Student *prev = NULL;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
}
// Function to delete a student from the front of the list (Demonstration of Stack)
Data Structure and Applications (BCSL305) 20
void deleteFront() {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
Sample Output:
Menu:
1. Create SLL of N Students Data (Front Insertion)
2. Display SLL and Count Nodes
3. Insert Student at End of SLL
4. Delete Student from End of SLL
5. Insert Student at Front of SLL
6. Delete Student from Front of SLL (Stack)
7. Exit
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
Data Structure and Applications (BCSL305) 22
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function prototypes
void createList(int n);
void displayList();
void insertEnd();
void deleteEnd();
void insertFront();
void deleteFront();
void demonstrateDEQueue();
void menu();
int main() {
int choice;
menu();
while (1) {
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
Data Structure and Applications (BCSL305) 23
{
int n;
printf("Enter the number of employees to create the list: ");
scanf("%d", &n);
createList(n);
}
break;
case 2:
displayList();
break;
case 3:
insertEnd();
break;
case 4:
deleteEnd();
break;
case 5:
insertFront();
break;
case 6:
deleteFront();
break;
case 7:
demonstrateDEQueue();
break;
case 8:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
newEmployee->prev = tail;
newEmployee->next = NULL;
if (tail == NULL) {
head = tail = newEmployee;
} else {
tail->next = newEmployee;
tail = newEmployee;
}
}
}
newEmployee->prev = tail;
newEmployee->next = NULL;
if (tail == NULL) {
head = tail = newEmployee;
} else {
tail->next = newEmployee;
tail = newEmployee;
}
if (head == tail) {
head = tail = NULL;
} else {
tail = tail->prev;
tail->next = NULL;
}
free(temp);
printf("Employee deleted from the end.\n");
}
newEmployee->prev = NULL;
newEmployee->next = head;
if (head == NULL) {
head = tail = newEmployee;
Data Structure and Applications (BCSL305) 27
} else {
head->prev = newEmployee;
head = newEmployee;
}
if (head == tail) {
head = tail = NULL;
} else {
head = head->next;
head->prev = NULL;
}
free(temp);
printf("Employee deleted from the front.\n");
}
switch (choice) {
case 1:
insertFront();
break;
case 2:
insertEnd();
Data Structure and Applications (BCSL305) 28
break;
case 3:
deleteFront();
break;
case 4:
deleteEnd();
break;
default:
printf("Invalid choice.\n");
}
}
Sample output:
Menu:
1. Create DLL of N Employee Data (End Insertion)
2. Display DLL and Count Nodes
3. Insert Employee at End of DLL
4. Delete Employee from End of DLL
5. Insert Employee at Front of DLL
6. Delete Employee from Front of DLL
7. Demonstrate DLL as Double Ended Queue
8. Exit
Program 9: Develop a Program in C for the following operationson 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
Program 9.a
#include<stdio.h>
Data Structure and Applications (BCSL305) 30
#include<stdlib.h>
#include<math.h>
//int coef,px,py,pz, x,y,z,i;
//int val;
struct node
{
int coef,px, py,pz;
struct node *next;
};
typedef struct node NODE;
NODE *first;
void insert(int coef,int px, int py, int pz)
{
NODE *temp,*cur;
temp= (NODE *)malloc(sizeof(NODE));
temp->coef=coef; temp->px=px; temp->py=py; temp->pz=pz;
if(first==NULL)
{
temp->next=temp; first=temp; return;
}
if(first->next==first)
{
first->next=temp; temp->next=first;
}
cur=first;
while(cur->next!=first)
{
cur=cur->next;
}
cur->next=temp;
temp->next=first;
return;
}
void display()
{
NODE *cur;
if(first==NULL)
{
printf("List is empty\n");return;
}
cur=first;
while(cur->next!=first)
{
printf("%d ",cur->coef);
printf(" x^%d",cur->px);
Data Structure and Applications (BCSL305) 31
printf(" y^%d",cur->py);
printf(" z^%d + ",cur->pz);
cur=cur->next;
}
printf("%d ",cur->coef);
printf(" x^%d",cur->px);
printf(" y^%d",cur->py);
printf(" z^%d\n",cur->pz);
return;
}
int evaluate(int x, int y, int z)
{
NODE *cur; int v,s=0, v1,v2,v3;
if(first==NULL)
{
printf("List is empty\n");return 0;
}
cur=first;
while(cur->next!=first)
{
v=cur->coef*pow(x, cur->px)*pow(y, cur->py)*pow(z,cur->pz);
s=s+v;
cur=cur->next;
}
v=cur->coef*pow(x, cur->px)*pow(y, cur->py)*pow(z,cur->pz);
s=s+v;
return s;
}
int main()
{
int coef,px,py,pz, x,y,z,i;
int val;
first=NULL;
while(1)
{
printf("1. Insert polynomial at end\n");
printf("2. Display\n");
printf("3. Evaluate\n");
printf("4. Exit\n");
printf("Enter Choice= \t");
scanf("%d",&i);
switch(i)
{
case 1 :printf("Enter Coefficient= \t");
scanf("%d",&coef);
Data Structure and Applications (BCSL305) 32
Output:
3. Evaluate
4. Exit
Enter Choice= 1
Enter Coefficient= 3
Enter powers of x y z values= 311
1. Insert polynomial at end
2. Display
3. Evaluate
4. Exit
Enter Choice= 1
Enter Coefficient= 2
Enter powers of x y z values= 151
1. Insert polynomial at end
2. Display
3. Evaluate
4. Exit
Enter Choice= 1
Enter Coefficient= -2
Enter powers of x y z values= 113
1. Insert polynomial at end
2. Display
3. Evaluate
4. Exit
Enter Choice= 2
6 x^2 y^2 z^1 + -4 x^0 y^1 z^5 + 3 x^3 y^1 z^1 + 2 x^1 y^5 z^1 + -2 x^1 y^1 z^3
1. Insert polynomial at end
2. Display
3. Evaluate
4. Exit
Enter Choice= 3
Value=4640
1. Insert polynomial at end
2. Display
3. Evaluate
4. Exit
Enter Choice= 2
6 x^2 y^2 z^1 + -4 x^0 y^1 z^5 + 3 x^3 y^1 z^1 + 2 x^1 y^5 z^1 + -2 x^1 y^1 z^3
1. Insert polynomial at end
2. Display
3. Evaluate
Data Structure and Applications (BCSL305) 34
4. Exit
Enter Choice= 4
Explanation:
Key Operations:
1. Insert Polynomial Terms: Each term of the polynomial is inserted into the linked list.
The term consists of a coefficient and the powers of x, y, and z.
2. Display Polynomial: The polynomial is displayed in the format coef * x^px * y^py *
z^pz for each term in the list.
3. Evaluate Polynomial: The program evaluates the polynomial for given values of x, y,
and z by calculating each term's value using the given powers and summing the results.
Functionality Breakdown:
● Struct Definition (struct node): Represents a term of the polynomial, including the
coefficient and powers of x, y, and z, as well as the next pointer to create the circular
linked list.
● insert() Function: This function inserts a polynomial term at the end of the circular
linked list.
● display() Function: It prints the entire polynomial in a readable format.
● evaluate() Function: Evaluates the polynomial for a given set of values for x, y, and z.
● You can input terms like 6x^2y^2z, -4yz^5, etc., by entering their coefficients and the
powers of x, y, and z when prompted.
If you want to add the sum of two polynomials, here’s a plan to extend the program:
Program 9.b
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node //Defining Polynomial fields
{
int coef, px, py, pz,flag;
struct node *link;
};
typedef struct node * NODE;
tmp->link=head;
}
NODE add_poly(NODE h1,NODE h2,NODE h3)
{
NODE cur1,cur2;
int scf;
cur1=h1->link;
cur2=h2->link;
while(cur1 != h1) //Till end of poly1
{
if(cur2 == h2)
cur2=h2->link;
while(cur2 != h2) //Till end of poly2
{
if(cur1->px == cur2->px && cur1->py == cur2->py &&
cur1->pz == cur2->pz)
{ //Add & insert if co-ef's of both poly is equal
scf = cur1->coef + cur2->coef;
insert(h3,scf,cur1->px,cur1->py,cur1->pz);
cur2->flag=1;
cur2=h2->link;
break;
}
cur2=cur2->link;
}
if(cur1 == h1)
break;
if(cur2 == h2) //If co-ef of poly1 is not matched, insert it to poly3
insert(h3,cur1->coef,cur1->px,cur1->py,cur1->pz);
cur1=cur1->link;
}
cur2=h2->link;
while(cur2 != h2) //remaining poly2 nodes inserted to poly3
{
if(cur2->flag==0)
insert(h3,cur2->coef,cur2->px,cur2->py,cur2->pz);
cur2=cur2->link;
}
return h3;
}
void display(NODE head)
{
NODE cur;
if(head->link==head) //if poly is empty
{
Data Structure and Applications (BCSL305) 37
printf("List is empty\n");
return;
}
cur=head->link;
while(cur != head) //display all terms till end
{
if(cur->coef > 0)
printf(" +%dx^%dy^%dz^%d ",cur->coef,cur->px,cur->py,cur->pz);
else if (cur->coef < 0)
printf(" %dx^%dy^%dz^%d
",cur->coef,cur->px,cur->py,cur->pz);
cur=cur->link;
}
printf("\n");
}/*End of display() */
void main()
{
int choice,data,item,pos;
NODE head1,head2,head3;
head1=(NODE)malloc(sizeof(struct node));
head1->link=head1; //poly1
head2=(NODE)malloc(sizeof(struct node));
head2->link=head2; //poly2
head3=(NODE)malloc(sizeof(struct node));
head3->link=head3; //poly3
printf("\n1.Create Polynomial 1\n");
head1=create_list(head1);
Program 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>
typedef struct tree
{
int data;
struct tree *rlink, *llink;
}TNODE;
TNODE * getnode();
TNODE * insert(int ele, TNODE * root);
void inorder(TNODE * root);
void preorder(TNODE * root);
Data Structure and Applications (BCSL305) 39
printf("Unsuccessful search!!!\n");
else
printf("Successful search!!!\n");
break;
case 6: exit(0);
}
}
}
TNODE * getnode()
{
TNODE *temp= (TNODE*)malloc(sizeof(TNODE));
if(temp==NULL)
{
printf("Out of memory!!!\n");
return NULL;
}
return temp;
}
TNODE * insert( int ele, TNODE * root)
{
TNODE *newN= getnode();
//TNODE *previous, *present;
newN->data= ele;
newN->rlink= newN->llink= NULL;
if(root==NULL)
return newN;
if(ele<root->data)
root->llink= insert(ele, root->llink);
if(ele>root->data)
root->rlink= insert(ele, root->rlink);
return root;
}
void inorder(TNODE * root)
{
if(root!=NULL)
{
inorder(root->llink);
printf("%d\n", root->data);
inorder(root->rlink);
}
}
void preorder(TNODE * root)
{
if(root!=NULL)
{
Data Structure and Applications (BCSL305) 41
printf("%d\n", root->data);
preorder(root->llink);
preorder(root->rlink);
}
}
void postorder(TNODE * root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\n", root->data);
}
}
int search( TNODE * root, int key)
{
if(root!=NULL)
{
if(root->data==key)
return key;
if(key < root->data)
return search(root->llink, key);
return search(root->rlink, key);
}
return -1;
}
Program 11: Develop a Program in C for the following operations on Graph(G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method
#include<stdio.h>
#include<stdlib.h>
int a[23][12],q[23],visited[23],n,front=0,rear=-1;
void bfs_search(int s)
{
int d;
for(d=1;d<=n;d++)
if(a[s][d] && visited[d]!=1)
{
rear=rear+1;
q[rear]=d;
Data Structure and Applications (BCSL305) 42
}
if(front<=rear)
{
visited[q[front]]=1;
bfs_search(q[front++]);
}
}
void main()
{
int s,col,row;
printf("Enter the number of vertices\n");
scanf("%d",&n);
for(col=1;col<=n;col++)
{
q[col]=0;
visited[col]=0;
}
printf("Enter the graph datain the matrix form\n");
for(row=1;row<=n;row++)
for(col=1;col<=n;col++)
scanf("%d",&a[row][col]);
printf("Enter the starting vertex\n\n");
scanf("%d",&s);
bfs_search(s);
printf("Node reachable are\n");
for(col=1;col<=n;col++)
if(visited[col])
printf("%d\t",col);
}
Program 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;
Data Structure and Applications (BCSL305) 43
void display()
{
int i;
if(count == 0)
{
printf("\nHash Table is empty");
return;
}
void main()
{
int i;
printf("\nEnter the number of employee records (N) : ");
scanf("%d", &n);
printf("\nEnter the two digit memory locations (m) for hash table: ");
scanf("%d", &m);
ht = (int *)malloc(m*sizeof(int));
for(i=0; i<m; i++)
ht[i] = -1;
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
for(i=0; i<n; i++)
scanf("%d", &key[i]);
for(i=0;i<n;i++)
{
if(count == m)
{
printf("\n~~~Hash table is full. Cannot insert the record %d key~~~",i+1);
break;
Data Structure and Applications (BCSL305) 44
}
insert(key[i]);
}
Output: