0% found this document useful (0 votes)
9 views44 pages

DSA Lab Programs

The document outlines multiple C programming tasks related to data structures, including stack operations, infix to postfix conversion, evaluation of postfix expressions, and circular queue management. Each program includes a menu-driven interface for user interaction, demonstrating key operations like push, pop, and overflow/underflow checks. Additionally, it provides sample inputs and expected outputs for clarity.

Uploaded by

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

DSA Lab Programs

The document outlines multiple C programming tasks related to data structures, including stack operations, infix to postfix conversion, evaluation of postfix expressions, and circular queue management. Each program includes a menu-driven interface for user interaction, demonstrating key operations like push, pop, and overflow/underflow checks. Additionally, it provides sample inputs and expected outputs for clarity.

Uploaded by

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

Data Structure and Applications (BCSL305) 1

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>

#define MAX 5 // Define the maximum size of the stack

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

// Function to push multiple elements onto the stack


void pushMultiple(int n) {
int element;
for (int i = 0; i < n; i++) {
if (isFull()) {
printf("Stack Overflow! Cannot push more elements.\n");
Data Structure and Applications (BCSL305) 3

return;
}
printf("Enter element %d: ", i + 1);
scanf("%d", &element);
stack[++top] = element;
printf("Pushed %d onto the stack.\n", element);
}
}

// Function to pop an element from the stack


int pop() {
if (isEmpty()) {
printf("Stack Underflow! Cannot pop an element.\n");
return -1;
} else {
return stack[top--];
}
}

// Function to check if the stack content forms a palindrome


int isPalindrome() {
if (isEmpty()) {
printf("Stack is empty. Cannot check for palindrome.\n");
return 0;
}

int i, palindrome = 1;
for (i = 0; i <= top / 2; i++) {
if (stack[i] != stack[top - i]) {
palindrome = 0;
break;
}
}
return palindrome;
}

// Function to check if the stack is full (Overflow demonstration)


void checkOverflow() {
if (isFull()) {
printf("Stack is full. Cannot push any more elements.\n");
} else {
printf("Stack is not full. You can push elements.\n");
}
}
Data Structure and Applications (BCSL305) 4

// Function to check if the stack is empty (Underflow demonstration)


void checkUnderflow() {
if (isEmpty()) {
printf("Stack is empty. Cannot pop elements.\n");
} else {
printf("Stack is not empty. You can pop elements.\n");
}
}

// Function to display the elements of the stack


void display() {
if (isEmpty()) {
printf("Stack is empty.\n");
} else {
printf("Stack elements:\n");
for (int i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
}

// Helper function to check if the stack is empty


int isEmpty() {
return top == -1;
}

// Helper function to check if the stack is full


int isFull() {
return top == MAX - 1;
}
Data Structure and Applications (BCSL305) 5

Program 4:Develop a Program in C for converting an Infix Expression to Postfix Expression.


Program should support for both parenthesized and free parenthesized expressions with the
operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric Operands.

#include <stdio.h>
#include <ctype.h> // For isalnum()
#include <string.h> // For strlen()

#define MAX 100

char stack[MAX];
int top = -1;

// Push function to add an operator to the stack


void push(char c) {
if (top < MAX - 1) {
stack[++top] = c;
}
}

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

// Function to get the precedence of operators


int precedence(char c) {
if (c == '^') return 3;
if (c == '*' || c == '/' || c == '%') return 2;
if (c == '+' || c == '-') return 1;
return 0;
}

// Function to check if the character is an operator


int isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^');
}

// Function to convert infix expression to postfix expression


void infixToPostfix(char* infix, char* postfix) {
int i, j = 0;
char ch;

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


ch = infix[i];

// If the character is an operand (alphanumeric), add it to the postfix expression


if (isalnum(ch)) {
postfix[j++] = ch;
}
// If the character is '(', push it onto the stack
else if (ch == '(') {
push(ch);
}
// If the character is ')', pop and output from the stack until '(' is encountered
else if (ch == ')') {
while (stack[top] != '(') {
postfix[j++] = pop();
}
pop(); // Remove '(' from the stack
}
// If the character is an operator
else if (isOperator(ch)) {
while (top != -1 && precedence(stack[top]) >= precedence(ch)) {
postfix[j++] = pop();
}
Data Structure and Applications (BCSL305) 7

push(ch);
}
}

// Pop all remaining operators from the stack


while (top != -1) {
postfix[j++] = pop();
}

postfix[j] = '\0'; // Null-terminate the postfix expression


}

int main() {
char infix[MAX], postfix[MAX];

printf("Enter an infix expression: ");


fgets(infix, MAX, stdin); // Reading input

infixToPostfix(infix, postfix); // Convert infix to postfix

printf("Postfix expression: %s\n", postfix); // Output the postfix expression

return 0;
}

Program 5: Develop a Program in C for the following Stack Applications

a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^


b. Solving Tower of Hanoi problem with n disks

#include <stdio.h>
#include <ctype.h>
#include <math.h>

#define MAX 100

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 evaluatePostfix(char* exp) {


int i;
for (i = 0; exp[i] != '\0'; i++) {
// If the character is a digit, push it to the stack
if (isdigit(exp[i])) {
push(exp[i] - '0');
} else {
// Operator encountered, pop two elements
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(val2 % val1); break;
case '^': push(pow(val2, val1)); break;
}
}
}
return pop();
}

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>

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {


if (n == 1) {
printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);
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);
}

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:

● The postfix expression 23*54*+ means:


1. 2 * 3 = 6 (First operation: multiplication of 2 and 3)
Data Structure and Applications (BCSL305) 10

2. 5 * 4 = 20 (Second operation: multiplication of 5 and 4)


3. 6 + 20 = 26 (Final operation: addition of 6 and 20)

Expected Output:

Result of evaluation: 26

Example Input 2:

Enter a postfix expression: 52+83-*

Explanation:

● The postfix expression 52+83-* means:


1. 5 + 2 = 7 (First operation: addition of 5 and 2)
2. 8 - 3 = 5 (Second operation: subtraction of 3 from 8)
3. 7 * 5 = 35 (Final operation: multiplication of 7 and 5)

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>

#define MAX 5 // Maximum size of Circular Queue

char queue[MAX];
int front = -1, rear = -1;

// Function to check if the Circular Queue is full


int isFull() {
return (front == (rear + 1) % MAX);
}

// Function to check if the Circular Queue is empty


int isEmpty() {
return (front == -1);
}

// Function to insert an element into the Circular Queue


void insertElement(char element) {
if (isFull()) {
printf("Queue Overflow! Cannot insert element '%c'.\n", element);
return;
}

if (isEmpty()) {
front = 0;
}

rear = (rear + 1) % MAX;


queue[rear] = element;
printf("Inserted '%c' into the queue.\n", element);
}

// Function to delete an element from the Circular Queue


void deleteElement() {
if (isEmpty()) {
printf("Queue Underflow! No element to delete.\n");
return;
}

printf("Deleted '%c' from the queue.\n", queue[front]);


Data Structure and Applications (BCSL305) 12

if (front == rear) {
// Queue has only one element, reset queue after deletion
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}

// Function to display the status of the Circular Queue


void displayQueue() {
if (isEmpty()) {
printf("Queue is Empty!\n");
return;
}

printf("Queue elements are: ");


int i = front;
while (1) {
printf("%c ", queue[i]);
if (i == rear) {
break;
}
i = (i + 1) % MAX;
}
printf("\n");
}

// Main menu-driven program


int main() {
int choice;
char element;

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

scanf(" %c", &element);


insertElement(element);
break;

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>

// Define the Student struct


typedef struct Student {
char usn[15];
char name[50];
char programme[50];
int sem;
char phNo[15];
struct Student *next;
} Student;

// Head pointer of the list


Student *head = NULL;

// 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 menu


void menu() {
printf("\nMenu:\n");
printf("1. Create SLL of N Students Data (Front Insertion)\n");
printf("2. Display SLL and Count Nodes\n");
printf("3. Insert Student at End of SLL\n");
printf("4. Delete Student from End of SLL\n");
printf("5. Insert Student at Front of SLL\n");
printf("6. Delete Student from Front of SLL (Stack)\n");
printf("7. Exit\n");
}

// Function to create a singly linked list of N students


void createList(int n) {
for (int i = 0; i < n; i++) {
Student *newStudent = (Student *)malloc(sizeof(Student));

printf("\nEnter details of student %d:\n", i + 1);


printf("USN: ");
scanf("%s", newStudent->usn);
printf("Name: ");
scanf(" %s", newStudent->name); // To allow spaces in name
printf("Programme: ");
scanf(" %s", newStudent->programme); // To allow spaces in programme
printf("Sem: ");
scanf("%d", &newStudent->sem);
printf("Phone No: ");
scanf("%s", newStudent->phNo);

newStudent->next = head; // Insert at front


head = newStudent;
}
}

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

Student *temp = head;


int count = 0;
Data Structure and Applications (BCSL305) 18

printf("\nStudent Data in the List:\n");


while (temp != NULL) {
count++;
printf("USN: %s\n", temp->usn);
printf("Name: %s\n", temp->name);
printf("Programme: %s\n", temp->programme);
printf("Sem: %d\n", temp->sem);
printf("Phone No: %s\n", temp->phNo);
printf("-----------------------------\n");
temp = temp->next;
}

printf("Total number of students in the list: %d\n", count);


}

// Function to insert a student at the end of the list


void insertEnd() {
Student *newStudent = (Student *)malloc(sizeof(Student));

printf("\nEnter details of the student:\n");


printf("USN: ");
scanf("%s", newStudent->usn);
printf("Name: ");
scanf(" %s", newStudent->name);
printf("Programme: ");
scanf(" %s", newStudent->programme);
printf("Sem: ");
scanf("%d", &newStudent->sem);
printf("Phone No: ");
scanf("%s", newStudent->phNo);

newStudent->next = NULL;

if (head == NULL) {
head = newStudent;
} else {
Student *temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newStudent;
}
}

// Function to delete a student from the end of the list


Data Structure and Applications (BCSL305) 19

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

printf("Student deleted from the end.\n");


}

// Function to insert a student at the front of the list


void insertFront() {
Student *newStudent = (Student *)malloc(sizeof(Student));

printf("\nEnter details of the student:\n");


printf("USN: ");
scanf("%s", newStudent->usn);
printf("Name: ");
scanf(" %s", newStudent->name);
printf("Programme: ");
scanf(" %s", newStudent->programme);
printf("Sem: ");
scanf("%d", &newStudent->sem);
printf("Phone No: ");
scanf("%s", newStudent->phNo);

newStudent->next = head; // Insert at front


head = newStudent;
printf("Student inserted at the front.\n");
}

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

Student *temp = head;


head = head->next;
free(temp);
printf("Student deleted from the front.\n");
}

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

Enter your choice: 1


Enter the number of students to create the list: 2

Enter details of student 1:


USN: 3PG23CS001
Name: John Doe
Programme: CSE
Sem: 4
Phone No: 9876543210

Enter details of student 2:


USN: 3PG23CS002
Name: Alice Smith
Programme: CSE
Sem: 4
Phone No: 1234567890

Enter your choice: 2


Data Structure and Applications (BCSL305) 21

Student Data in the List:


USN: 3PG23CS002
Name: Alice Smith
Programme: CSE
Sem: 4
Phone No: 1234567890
-----------------------------
USN:3PG23CS001
Name: John Doe
Programme: CSE
Sem: 4

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

e. Demonstrate how this DLL can be used as Double Ended Queue.


f. Exit

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define the Employee struct for DLL


typedef struct Employee {
char ssn[20];
char name[50];
char dept[50];
char designation[50];
float sal;
char phNo[20];
struct Employee* prev;
struct Employee* next;
} Employee;

// Head and tail pointers for DLL


Employee* head = NULL;
Employee* tail = NULL;

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

// Function to display the menu


void menu() {
printf("\nMenu:\n");
printf("1. Create DLL of N Employee Data (End Insertion)\n");
printf("2. Display DLL and Count Nodes\n");
printf("3. Insert Employee at End of DLL\n");
printf("4. Delete Employee from End of DLL\n");
printf("5. Insert Employee at Front of DLL\n");
printf("6. Delete Employee from Front of DLL\n");
Data Structure and Applications (BCSL305) 24

printf("7. Demonstrate DLL as Double Ended Queue\n");


printf("8. Exit\n");
}

// Function to create a doubly linked list of N employees using end insertion


void createList(int n) {
for (int i = 0; i < n; i++) {
Employee* newEmployee = (Employee*)malloc(sizeof(Employee));

printf("\nEnter details of employee %d:\n", i + 1);


printf("SSN: ");
scanf("%s", newEmployee->ssn);
printf("Name: ");
scanf(" %s", newEmployee->name); // To allow spaces in name
printf("Department: ");
scanf(" %s", newEmployee->dept);
printf("Designation: ");
scanf(" %s", newEmployee->designation);
printf("Salary: ");
scanf("%f", &newEmployee->sal);
printf("Phone No: ");
scanf("%s", newEmployee->phNo);

newEmployee->prev = tail;
newEmployee->next = NULL;

if (tail == NULL) {
head = tail = newEmployee;
} else {
tail->next = newEmployee;
tail = newEmployee;
}
}
}

// Function to display the DLL and count the number of nodes


void displayList() {
if (head == NULL) {
printf("The list is empty.\n");
return;
}

Employee* temp = head;


int count = 0;
Data Structure and Applications (BCSL305) 25

printf("\nEmployee Data in the DLL:\n");


while (temp != NULL) {
count++;
printf("SSN: %s\n", temp->ssn);
printf("Name: %s\n", temp->name);
printf("Dept: %s\n", temp->dept);
printf("Designation: %s\n", temp->designation);
printf("Salary: %.2f\n", temp->sal);
printf("Phone No: %s\n", temp->phNo);
printf("-----------------------------\n");
temp = temp->next;
}

printf("Total number of employees in the list: %d\n", count);


}

// Function to insert a new employee at the end of DLL


void insertEnd() {
Employee* newEmployee = (Employee*)malloc(sizeof(Employee));

printf("\nEnter details of the employee:\n");


printf("SSN: ");
scanf("%s", newEmployee->ssn);
printf("Name: ");
scanf(" %s", newEmployee->name);
printf("Department: ");
scanf(" %s", newEmployee->dept);
printf("Designation: ");
scanf(" %s", newEmployee->designation);
printf("Salary: ");
scanf("%f", &newEmployee->sal);
printf("Phone No: ");
scanf("%s", newEmployee->phNo);

newEmployee->prev = tail;
newEmployee->next = NULL;

if (tail == NULL) {
head = tail = newEmployee;
} else {
tail->next = newEmployee;
tail = newEmployee;
}

printf("Employee inserted at the end.\n");


Data Structure and Applications (BCSL305) 26

// Function to delete an employee from the end of DLL


void deleteEnd() {
if (tail == NULL) {
printf("The list is empty.\n");
return;
}

Employee* temp = tail;

if (head == tail) {
head = tail = NULL;
} else {
tail = tail->prev;
tail->next = NULL;
}

free(temp);
printf("Employee deleted from the end.\n");
}

// Function to insert an employee at the front of DLL


void insertFront() {
Employee* newEmployee = (Employee*)malloc(sizeof(Employee));

printf("\nEnter details of the employee:\n");


printf("SSN: ");
scanf("%s", newEmployee->ssn);
printf("Name: ");
scanf(" %s", newEmployee->name);
printf("Department: ");
scanf(" %s", newEmployee->dept);
printf("Designation: ");
scanf(" %s", newEmployee->designation);
printf("Salary: ");
scanf("%f", &newEmployee->sal);
printf("Phone No: ");
scanf("%s", newEmployee->phNo);

newEmployee->prev = NULL;
newEmployee->next = head;

if (head == NULL) {
head = tail = newEmployee;
Data Structure and Applications (BCSL305) 27

} else {
head->prev = newEmployee;
head = newEmployee;
}

printf("Employee inserted at the front.\n");


}

// Function to delete an employee from the front of DLL


void deleteFront() {
if (head == NULL) {
printf("The list is empty.\n");
return;
}

Employee* temp = head;

if (head == tail) {
head = tail = NULL;
} else {
head = head->next;
head->prev = NULL;
}

free(temp);
printf("Employee deleted from the front.\n");
}

// Function to demonstrate the DLL as a double-ended queue (DEQueue)


void demonstrateDEQueue() {
int choice;
printf("\nDemonstrating DLL as Double Ended Queue (DEQueue)\n");
printf("1. Insert at Front\n");
printf("2. Insert at End\n");
printf("3. Delete from Front\n");
printf("4. Delete from End\n");
printf("Enter your choice: ");
scanf("%d", &choice);

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

Enter your choice: 1


Enter the number of employees to create the list: 2

Enter details of employee 1:


SSN: 12345
Name: John Doe
Department: HR
Designation: Manager
Salary: 50000
Phone No: 9876543210

Enter details of employee 2:


SSN: 67890
Name: Alice Smith
Department: Finance
Designation: Accountant
Salary: 40000
Phone No: 1234567890

Enter your choice: 2


Data Structure and Applications (BCSL305) 29

Employee Data in the DLL:


SSN: 12345
Name: John Doe
Dept: HR
Designation: Manager
Salary: 50000.00
Phone No: 9876543210
-----------------------------
SSN: 67890
Name: Alice Smith
Dept: Finance
Designation: Accountant
Salary: 40000.00
Phone No: 1234567890
-----------------------------
Total number of employees in the list: 2

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

printf("Enter powers of x y z values= \t");


scanf("%d%d%d",&px, &py,&pz);
insert(coef,px,py,pz);
break;
case 2 : display();
break;
case 3 : printf("\n Enter x y & z values for evaluation: \t");
scanf("%d%d%d",&x,&y,&z);
val=evaluate(x,y,z);
printf("\nValue=%d\n",val);
break;
case 4 : return 0;
default : printf(" Wrong choice. Enter 1,2 3\n"); break;
}
}
}

Output:

1. Insert polynomial at end


2. Display
3. Evaluate
4. Exit
Enter Choice= 1
Enter Coefficient= 6
Enter powers of x y z values= 2
2
1
1. Insert polynomial at end
2. Display
3. Evaluate
4. Exit
Enter Choice= 2
6 x^2 y^2 z^1
1. Insert polynomial at end
2. Display
3. Evaluate
4. Exit
Enter Choice= 1
Enter Coefficient= -4
Enter powers of x y z values= 015
1. Insert polynomial at end
2. Display
Data Structure and Applications (BCSL305) 33

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

Enter x y &amp; z values for evaluation: 6


2
2

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.

Example Input and Output:

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

Enhancements for Summing Two Polynomials:

If you want to add the sum of two polynomials, here’s a plan to extend the program:

1. Create two separate lists for POLY1 and POLY2.


2. Function for summing the polynomials:
○ Traverse both lists term by term.
○ If the powers of x, y, and z are the same, sum the coefficients.
Data Structure and Applications (BCSL305) 35

○ Store the result in a new list POLYSUM.

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;

void insert(NODE head,int cof,int x,int y, int z);


NODE create_list(NODE head) //For creating poly1 & poly2
{
int i,n,cf,px,py,pz;
printf("Enter the number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the Co-ef, px, py, pz : ");
scanf("%d %d %d %d",&cf,&px,&py,&pz);
insert(head,cf,px,py,pz);
}
return head;
}/*End of create_list()*/
void insert(NODE head,int cof,int x,int y, int z) //inserting term to poly
{
NODE cur,tmp;
tmp = (NODE)malloc(sizeof(struct node)); //Allocates memory
//int cf,px,py,pz;
cur=head->link;
tmp->coef=cof;
tmp->px=x;
tmp->py=y;
tmp->pz=z;
tmp->flag=0;
while(cur->link!=head) //Identifying last node
cur=cur->link;
cur->link=tmp;
Data Structure and Applications (BCSL305) 36

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

printf("\n2.Create Polynomial 2\n");


head2=create_list(head2);
printf("\nPolynomial 1 is :");
display(head1);
printf("\nPolynomial 2 is :");
display(head2);
head3=add_poly(head1,head2,head3); //Add both polynomials
printf("\nAddition of two Polynomial is :");
display(head3);
}
Data Structure and Applications (BCSL305) 38

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

void postorder(TNODE * root);


int search(TNODE * root, int key);
void main()
{
TNODE *root= NULL;
int choice, ele, key, flag;
for(;;)
{
printf("Enter\n1. Insert\n2. Inorder\n3. Preorder\n4.Postorder\n5. Search\n6.
Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter element to be inserted:\n");
scanf("%d", &ele);
root= insert(ele, root);
break;
case 2: if(root==NULL)
printf("Tree is empty\n");
else
{
printf("The contents are:\n");
inorder(root);
}
break;
case 3: if(root==NULL)
printf("Tree is empty\n");
else
{
printf("The contents are:\n");
preorder(root);
}
break;
case 4: if(root==NULL)
printf("Tree is empty\n");
else
{
printf("The contents are:\n");
postorder(root);
}
break;
case 5: printf("Enter the node to be searched:\n");
scanf("%d", &key);
flag= search(root, key);
if(flag==-1)
Data Structure and Applications (BCSL305) 40

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 insert(int key)


{
index = key % m;
while(ht[index] != -1)
{
index = (index+1)%m;
}
ht[index] = key;
count++;
}

void display()
{
int i;
if(count == 0)
{
printf("\nHash Table is empty");
return;
}

printf("\nHash Table contents are:\n ");


for(i=0; i<m; i++)
printf("\n T[%d] --> %d ", i, ht[i]);
}

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

//Displaying Keys inserted into hash table


display();
}

Output:

You might also like