DSDA Lab Record
DSDA Lab Record
(CYBER SECURITY)
(REGULATION 2022)
NAME :
REGISTER NO. :
LABORATORY RECORD
BONAFIDE CERTIFICATE
Signature of Examiners:
Vision of JNNIE: Lead the transformation of engineering and management learning experience to educate the
next generation of innovators and entrepreneurs who want to make the world a better place.
Mission of JNNIE:
1. To develop the required resources and infrastructure and to establish a conducive ambience for the
teaching-learning process.
2. To nurture professional and ethical values in the students and to instils in them a spirit of innovation
and entrepreneurship.
3. To encourage a desire for higher learning and research in the students and to equip them to face global
challenges.
4. To provide opportunities for students to learn job-relevant skills to make them industry ready.
5. To interact with industries and other organizations to facilitate transfer of knowledge and know-how.
Department Vision: To produce globally competent, quality computer professionals and to inculcate the spirit
of moral values for the cause of development of our nation
Department of Mission:
M1. Establish closer and symbolic relationship with IT industries and expose the students to the cutting edge
technological advancements.
M2. Provide impetus and importance to beyond curriculum learning and thereby provide an opportunity for
the student community to keep them updated with latest and socially relevant technology.
PEO Statements M1 M2 M3
Our graduates to pursue higher education and research, or have a successful 2 3 3
career in industries associated with Computer Science and Engineering, or as
entrepreneurs.
Our graduates will have the ability and attitude to adapt to emerging 3 3 2
technological changes.
Our graduates shall adapt to the changing career opportunities, assimilate new 2 3 3
technologies and work in multi-disciplinary areas with strong focus on
innovation and entrepreneurship.
3- High; 2-Medium; 1-Low
PO11 Project management and finance: Demonstrate knowledge and understanding of the engineering
and management principles and apply these to one’s own work, as a member and leader in a team,
to manage projects and in multidisciplinary environments.
PO12 Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
PROGRAM SPECIFIC OUTCOMES:
PSO1 To apply software engineering principles and practices for developing quality software for scientific and
business applications.
PSO2 To adapt to emerging Information and Communication Technologies (ICT) to innovate ideas and solutions
to existing/novel problems.
COURSE OBJECTIVES:
COURSE OUTCOME:
PRACTICAL EXERCISES:
Aim:
To implement a stack using an array and perform stack operations such as push, pop,
peek, and display using C programming.
Algorithm:
1. Push Operation (Insert Element)
• Step 1: Check if the stack is full by comparing top with MAX - 1.
• Step 2: If full, display an overflow error message.
• Step 3: If not full, increment top and add the new element to the stack at stack[top].
2. Pop Operation (Remove Element)
• Step 1: Check if the stack is empty by checking if top == -1.
• Step 2: If empty, display an underflow error message.
• Step 3: If not empty, retrieve the element at stack[top] and decrement top.
3. Peek Operation (View Top Element)
• Step 1: Check if the stack is empty.
• Step 2: If empty, display a message indicating that the stack is empty.
• Step 3: If not empty, return stack[top] as the top element of the stack.
4. Display Operation (View all Stack Elements)
• Step 1: Check if the stack is empty.
• Step 2: If empty, display a message indicating that the stack is empty.
• Step 3: If not empty, loop through the stack from top to 0 and display each element.
Program
#include <stdio.h>
#define MAX 5 // Define maximum size of the stack
int stack[MAX]; // Stack array
int top = -1; // Initialize top as -1 (indicating an empty stack)
// Function to push an element into the stack
void push(int element)
{
if (top == MAX - 1)
{
printf("Stack Overflow! Cannot push %d\n", element);
}
else
{
top++;
stack[top] = element;
printf("Pushed %d into the stack.\n", element);
}
}
// Function to pop an element from the stack
void pop() {
if (top == -1)
{
printf("Stack Underflow! No element to pop.\n");
}
else
{
printf("Popped %d from the stack.\n", stack[top]);
top--;
}
}
// Function to peek (view the top element) in the stack
void peek()
{
if (top == -1)
{
printf("Stack is empty.\n");
} else
{
printf("Top element is %d.\n", stack[top]);
}
}
// Function to display the stack
void display()
{
if (top == -1)
{
printf("Stack is empty.\n");
} else {
printf("Stack elements are:\n");
for (int i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
// Main function
int main()
{
int choice, element;
do
{
printf("\nStack Operations:\n");
printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to push: ");
scanf("%d", &element);
push(element);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
Output:
1. Push Operation:
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter element to push: 10
Pushed 10 into the stack.
2. Display Operation:
Enter your choice: 4
Stack elements are:
10
3. Push Multiple Elements and Pop:
Enter your choice: 1
Enter element to push: 20
Pushed 20 into the stack.
Result:
The stack was successfully implemented using an array in C. The stack operations
(push, pop, peek, and display) were executed as per the given algorithm, demonstrating
correct functionality of the stack data structure.
Ex.No. 1b ARRAY IMPLEMENTATION OF QUEUE USING C
Date:
Aim:
To implement a queue using an array and perform queue operations such as enqueue,
dequeue, peek, and display using C programming.
Algorithm:
1. Enqueue Operation (Insert Element)
• Step 1: Check if the queue is full by comparing rear with MAX - 1.
• Step 2: If full, display an overflow error message.
• Step 3: If not full, increment rear and add the new element to the queue at
queue[rear].
2. Dequeue Operation (Remove Element)
• Step 1: Check if the queue is empty by checking if front > rear or front == -1.
• Step 2: If empty, display an underflow error message.
• Step 3: If not empty, retrieve the element at queue[front] and increment front.
3. Peek Operation (View Front Element)
• Step 1: Check if the queue is empty.
• Step 2: If empty, display a message indicating that the queue is empty.
• Step 3: If not empty, return queue[front] as the front element of the queue.
4. Display Operation (View all Queue Elements)
• Step 1: Check if the queue is empty.
• Step 2: If empty, display a message indicating that the queue is empty.
• Step 3: If not empty, loop through the queue from front to rear and display each
element.
Program
#include <stdio.h>
#define MAX 5 // Define maximum size of the queue
int queue[MAX]; // Queue array
int front = -1, rear = -1; // Initialize front and rear
switch (choice) {
case 1:
printf("Enter element to enqueue: ");
scanf("%d", &element);
enqueue(element);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
Output:
1. Enqueue Operation:
Queue Operations:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter element to enqueue: 10
Enqueued 10 into the queue.
2. Display Operation:
Enter your choice: 4
Queue elements are:
10
3. Enqueue Multiple Elements and Dequeue:
Enter your choice: 1
Enter element to enqueue: 20
Enqueued 20 into the queue.
Result:
The queue was successfully implemented using an array in C. The queue operations
(enqueue, dequeue, peek, and display) were executed as per the given algorithm,
demonstrating correct functionality of the queue data structure.
Ex.No. 1c ARRAY IMPLEMENTATION OF LIST USING C
Date:
Aim:
To implement a List ADT (Abstract Data Type) using an array and perform various list
operations such as insert, delete, search, and display using C programming.
Algorithm:
1. Insert Operation (Insert Element)
• Step 1: Check if the list is full by comparing size with MAX.
• Step 2: If full, display an overflow error message.
• Step 3: If not full, move elements to the right starting from the given position, then
insert the new element at the specified position and increase the size of the list.
2. Delete Operation (Remove Element)
• Step 1: Check if the list is empty by checking if size == 0.
• Step 2: If empty, display an underflow error message.
• Step 3: If not empty, remove the element at the specified position and move elements
to the left to fill the gap, then decrease the size of the list.
3. Search Operation (Find an Element)
• Step 1: Iterate through the list to find the element.
• Step 2: If the element is found, return the position of the element.
• Step 3: If not found, return a message indicating the element is not present.
4. Display Operation (View all List Elements)
• Step 1: Check if the list is empty.
• Step 2: If empty, display a message indicating the list is empty.
• Step 3: If not empty, loop through the list from 0 to size - 1 and display each element.
Program
#include <stdio.h>
#define MAX 10 // Define maximum size of the list
int list[MAX]; // List array
int size = 0; // Initialize size of the list as 0
do {
printf("\nList Operations:\n");
printf("1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to insert: ");
scanf("%d", &element);
printf("Enter position to insert at (0 to %d): ", size);
scanf("%d", &position);
insert(element, position);
break;
case 2:
printf("Enter position to delete from (0 to %d): ", size - 1);
scanf("%d", &position);
delete(position);
break;
case 3:
printf("Enter element to search for: ");
scanf("%d", &element);
search(element);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
Output:
1. Insert Operation:
List Operations:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 1
Enter element to insert: 10
Enter position to insert at (0 to 0): 0
Inserted 10 at position 0.
2. Display Operation:
Enter your choice: 4
List elements are:
10
3. Insert Multiple Elements:
Enter your choice: 1
Enter element to insert: 20
Enter position to insert at (0 to 1): 1
Inserted 20 at position 1.
Aim:
To implement a Singly Linked List (SLL) using C programming and perform various
operations such as insert, delete, search, and display.
Algorithm:
1. Insert Operation (Insert Element at End)
• Step 1: Create a new node and allocate memory.
• Step 2: If the list is empty (i.e., head == NULL), make the new node the head of the
list.
• Step 3: If the list is not empty, traverse to the last node and link the new node to the
last node's next.
• Step 4: Set the next of the new node to NULL.
2. Delete Operation (Delete Element by Value)
• Step 1: If the list is empty, display an underflow message.
• Step 2: If the element to be deleted is the head, update the head to the next node and
free the old head.
• Step 3: Otherwise, traverse the list to find the node to be deleted. Once found, unlink
the node from the list and free its memory.
3. Search Operation (Find an Element)
• Step 1: Traverse the list and check each node’s data.
• Step 2: If the element is found, return its position.
• Step 3: If the element is not found, return a message indicating the element is not
present.
4. Display Operation (View All List Elements)
• Step 1: Check if the list is empty.
• Step 2: If not empty, traverse the list and display the data in each node.
Program
#include <stdio.h>
#include <stdlib.h>
if (head == NULL) {
head = newNode;
printf("Inserted %d as the first node.\n", element);
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
printf("Inserted %d at the end of the list.\n", element);
}
}
// Function to delete a node with a specific value
void delete(int element) {
struct Node* temp = head;
struct Node* prev = NULL;
// Unlink the node from the list and free its memory
prev->next = temp->next;
free(temp);
printf("Deleted %d from the list.\n", element);
}
// Function to search for an element in the list
void search(int element) {
struct Node* temp = head;
int position = 0;
while (temp != NULL) {
if (temp->data == element) {
printf("Element %d found at position %d.\n", element, position);
return;
}
temp = temp->next;
position++;
}
printf("Element %d not found in the list.\n", element);
}
// Function to display the elements of the list
void display() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
printf("List elements are: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main function
int main() {
int choice, element;
do {
printf("\nSingly Linked List Operations:\n");
printf("1. Insert\n2. Delete\n3. Search\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to insert: ");
scanf("%d", &element);
insert(element);
break;
case 2:
printf("Enter element to delete: ");
scanf("%d", &element);
delete(element);
break;
case 3:
printf("Enter element to search: ");
scanf("%d", &element);
search(element);
break;
case 4:
display();
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
Output:
1. Insert Operation:
Singly Linked List Operations:
1. Insert
2. Delete
3. Search
4. Display
5. Exit
Enter your choice: 1
Enter element to insert: 10
Inserted 10 as the first node.
2. Display Operation:
Enter your choice: 4
List elements are: 10 -> 20 -> NULL
3. Delete Operation:
Enter your choice: 2
Enter element to delete: 10
Deleted 10 from the list.
4. Search Operation:
Enter your choice: 3
Enter element to search: 20
Element 20 found at position 0.
Enter your choice: 3
Enter element to search: 30
Element 30 not found in the list.
Result:
The Singly Linked List was successfully implemented using C. The list operations
(insert, delete, search, and display) were executed as per the algorithm, demonstrating correct
functionality of the Singly Linked List data structure.
Ex.No. 3a LINKED LIST IMPLEMENTATION OF STACK USING C
Date:
Aim:
To implement a Stack using a Linked List in C and perform stack operations such as
push, pop, and display.
Algorithm:
1. Push Operation
• Step 1: Create a new node and allocate memory.
• Step 2: Assign the element to the data part of the node.
• Step 3: Point the next of the new node to the current top of the stack (i.e., head).
• Step 4: Make the new node the new top of the stack (i.e., update the head).
2. Pop Operation
• Step 1: If the stack is empty (i.e., head is NULL), display an underflow message.
• Step 2: Otherwise, store the current top node’s data.
• Step 3: Update the head to the next node in the list.
• Step 4: Free the memory of the removed node.
• Step 5: Return the data of the removed node.
3. Display Operation
• Step 1: Check if the stack is empty.
• Step 2: If not, traverse the linked list and display each node’s data starting from the
top.
Program
#include <stdio.h>
#include <stdlib.h>
// Define the structure of a node
struct Node {
int data;
struct Node* next;
};
// Initialize the top of the stack (head) as NULL
struct Node* head = NULL;
// Main function
int main() {
int choice, element;
do {
printf("\nStack Operations Using Linked List:\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to push: ");
scanf("%d", &element);
push(element);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
Output:
1. Push Operation:
Stack Operations Using Linked List:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter element to push: 10
Pushed 10 onto the stack.
2. Display Operation:
Enter your choice: 3
Stack elements: 20 -> 10 -> NULL
3. Pop Operation:
Enter your choice: 2
Popped 20 from the stack.
Result:
The Stack was successfully implemented using a linked list in C. The stack operations
(push, pop, and display) were executed according to the algorithm, demonstrating the proper
functioning of a stack data structure using linked lists.
Ex.No. 3b LINKED LIST IMPLEMENTATION OF LINEAR QUEUE USING C
Date:
Aim:
To implement a Linear Queue using a Linked List in C and perform queue operations
such as enqueue, dequeue, and display.
Algorithm:
1. Enqueue Operation
• Step 1: Create a new node and allocate memory.
• Step 2: Assign the element to the data part of the node.
• Step 3: If the queue is empty (i.e., both front and rear are NULL), make the new node
both the front and the rear.
• Step 4: Otherwise, link the new node to the next of the rear and update the rear to the
new node.
2. Dequeue Operation
• Step 1: If the queue is empty (i.e., front == NULL), display an underflow message.
• Step 2: Otherwise, store the data of the front node.
• Step 3: Move the front pointer to the next node in the list.
• Step 4: If the front becomes NULL, set rear to NULL (queue becomes empty).
• Step 5: Free the memory of the removed node and return the data.
3. Display Operation
• Step 1: Check if the queue is empty.
• Step 2: If not, traverse the linked list from the front to the rear, displaying each node’s
data.
Program
#include <stdio.h>
#include <stdlib.h>
if (front == NULL) {
rear = NULL; // If the queue becomes empty, update rear to NULL
}
// Main function
int main() {
int choice, element;
do {
printf("\nQueue Operations Using Linked List:\n");
printf("1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter element to enqueue: ");
scanf("%d", &element);
enqueue(element);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
Output:
1. Enqueue Operation:
Queue Operations Using Linked List:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter element to enqueue: 10
Enqueued 10 into the queue.
2. Display Operation:
Result:
The Linear Queue was successfully implemented using a linked list in C. The queue
operations (enqueue, dequeue, and display) were executed according to the algorithm,
demonstrating the correct functionality of a queue data structure using linked lists.
Ex.No. 4a IMPLEMENTATION OF POLYNOMIAL ADDITION
Date: USING LINKED LIST
Aim:
To implement polynomial manipulation using a linked list in C, which includes adding
two polynomials and displaying the resultant polynomial.
Algorithm:
1. Representation of Polynomial:
• A polynomial can be represented as a linked list where each node contains the
coefficient, exponent (power of the variable), and a pointer to the next node.
2. Create Node for Polynomial Term:
• Step 1: Allocate memory for a new node.
• Step 2: Assign the coefficient and exponent to the node.
• Step 3: Set the next pointer of the node to NULL.
3. Insert Polynomial Terms:
• Step 1: Insert terms into the list in descending order of exponents.
• Step 2: If a term with the same exponent already exists, add the coefficients.
4. Addition of Polynomials:
• Step 1: Traverse both polynomials.
• Step 2: If the exponents are the same, add the coefficients and insert the result into the
result polynomial.
• Step 3: If the exponent of the first polynomial is larger, insert the corresponding term
into the result polynomial and move to the next term of the first polynomial.
• Step 4: If the exponent of the second polynomial is larger, insert the corresponding
term into the result polynomial and move to the next term of the second polynomial.
• Step 5: Append any remaining terms from both polynomials to the result.
5. Display Polynomial:
• Step 1: Traverse the linked list and print each term in the format: coefficient *
x^exponent.
• Step 2: If it is the last term, omit the + sign.
Program
#include <stdio.h>
#include <stdlib.h>
if (poly == NULL) {
return newNode;
}
return result;
}
// Main function
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* result = NULL;
// Polynomial 2: 4x^3 + 3x + 2
poly2 = insertTerm(poly2, 4, 3);
poly2 = insertTerm(poly2, 3, 1);
poly2 = insertTerm(poly2, 2, 0);
printf("Polynomial 1: ");
displayPolynomial(poly1);
printf("Polynomial 2: ");
displayPolynomial(poly2);
Result:
The polynomial manipulation using linked lists was successfully implemented in C.
The program correctly added two polynomials and displayed the resulting polynomial.
Ex.No. 4b IMPLEMENTATION OF POLYNOMIAL SUBTRACTION
Date: USING LINKED LIST
Aim:
To implement polynomial subtraction using a linked list in C, where two polynomials
are subtracted and the resultant polynomial is displayed.
Algorithm:
1. Representation of Polynomial:
• Each polynomial term can be represented as a linked list node that contains the
coefficient, exponent (power of the variable), and a pointer to the next node.
2. Create Node for Polynomial Term:
• Step 1: Allocate memory for a new node.
• Step 2: Assign the coefficient and exponent to the node.
• Step 3: Set the next pointer of the node to NULL.
3. Insert Polynomial Terms:
• Step 1: Insert terms into the list in descending order of exponents.
• Step 2: If a term with the same exponent already exists, add the coefficients or subtract
them during subtraction.
4. Subtraction of Polynomials:
• Step 1: Traverse both polynomials.
• Step 2: If the exponents are the same, subtract the coefficients and insert the result
into the result polynomial.
• Step 3: If the exponent of the first polynomial is larger, insert the corresponding term
into the result polynomial and move to the next term of the first polynomial.
• Step 4: If the exponent of the second polynomial is larger, insert the negation of the
term from the second polynomial into the result polynomial and move to the next term
of the second polynomial.
• Step 5: Append any remaining terms from both polynomials (subtracting the second
polynomial terms).
5. Display Polynomial:
• Step 1: Traverse the linked list and print each term in the format: coefficient *
x^exponent.
• Step 2: If it is the last term, omit the + sign.
Program
#include <stdio.h>
#include <stdlib.h>
if (poly == NULL) {
return newNode;
}
return result;
}
// Main function
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* result = NULL;
// Polynomial 2: 4x^3 + 3x + 2
poly2 = insertTerm(poly2, 4, 3);
poly2 = insertTerm(poly2, 3, 1);
poly2 = insertTerm(poly2, 2, 0);
printf("Polynomial 1: ");
displayPolynomial(poly1);
printf("Polynomial 2: ");
displayPolynomial(poly2);
// Subtract poly2 from poly1
result = subtractPolynomials(poly1, poly2);
return 0;
}
Output:
Polynomial 1: 3x^3 + 2x^2 + 5
Polynomial 2: 4x^3 + 3x^1 + 2
Result of Polynomial Subtraction (Polynomial 1 - Polynomial 2): -1x^3 + 2x^2 - 3x^1 + 3
Result:
The polynomial subtraction using linked lists was successfully implemented in C. The
program correctly subtracted the terms of the second polynomial from the first polynomial
and displayed the resulting polynomial.
Ex.No. 5a IMPLEMENTATION OF EVALUATING POSTFIX
Date: EXPRESSIONS USING STACK
Aim:
To implement the evaluation of postfix expressions using a stack data structure in C.
Algorithm:
1. Initialize Stack:
o Create an empty stack to store operands.
2. Process Each Character in the Expression:
o If the character is an operand (number): Push it onto the stack.
o *If the character is an operator (+, -, * /):
▪ Pop the top two operands from the stack.
▪ Apply the operator to these operands.
▪ Push the result back onto the stack.
3. Final Result:
o The final result of the postfix expression will be the only element left in the
stack after processing all characters.
Program
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Stack structure
typedef struct {
int top;
float items[MAX];
} Stack;
int main() {
char postfixExp[MAX];
// Output result
printf("The result of the postfix expression %s is: %.2f\n", postfixExp, result);
return 0;
}
Input/Output:
Input:
Enter a postfix expression: 53+82-*
Output:
Explanation:
• The postfix expression 53+82-* is evaluated as follows:
o 5 3 + yields 8
o 8 2 - yields 6
o 8 * 6 yields 48
Result:
The program successfully evaluates postfix expressions using the stack data structure. The
given postfix expression was correctly processed, and the result was computed as expected.
Ex.No. 5b IMPLEMENTATION OF INFIX TO POSTFIX CONVERSION
USING STACK IN C
Date:
Aim:
To implement the conversion of an infix expression to a postfix expression using a
stack data structure in C.
Algorithm:
1. Initialize Stack and Output:
o Create an empty stack for operators.
o Create an empty output string for the postfix expression.
2. Process Each Character in the Infix Expression:
o If the character is an operand (number/variable): Append it directly to the
output string.
o *If the character is an operator (+, -, * /):
▪ Pop from the stack to the output string until the top of the stack has an
operator of lower precedence or the stack is empty.
▪ Push the current operator onto the stack.
o If the character is an opening parenthesis (: Push it onto the stack.
o If the character is a closing parenthesis ): Pop from the stack to the output
string until an opening parenthesis is encountered. Discard the pair of
parentheses.
3. Pop Remaining Operators:
o After processing all characters, pop any remaining operators in the stack to the
output string.
4. Output:
o The resultant string will be the postfix expression.
Program
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main() {
char infix[MAX], postfix[MAX];
return 0;
}
Input/Output:
Input:
Enter an infix expression: A*(B+C)/D
Output:
Postfix expression: ABC+*D/
Result:
The infix to postfix conversion was successfully implemented using a stack. The
program correctly converted the provided infix expression into its postfix equivalent.
Ex.No. 6 IMPLEMENTATION OF BINARY SEARCH TREE(BST)
Date:
Aim:
To implement a Binary Search Tree (BST) and perform various operations including
insertion, deletion, finding the minimum and maximum values, and displaying the tree.
Algorithm:
1. Binary Search Tree Structure:
• Node Definition: Each node will have a value, a left child, and a right child.
2. Insertion:
• Step 1: Start from the root.
• Step 2: Compare the value to be inserted with the current node’s value.
o If smaller: Move to the left child.
o If larger: Move to the right child.
• Step 3: Insert the new node at the appropriate position.
3. Deletion:
• Step 1: Find the node to be deleted.
• Step 2: Handle three cases:
o No children (leaf node): Simply remove the node.
o One child: Replace the node with its child.
o Two children: Find the in-order successor (or predecessor), replace the node’s
value with this successor's value, and delete the successor node.
4. Find Minimum:
• Step 1: Traverse the leftmost path of the tree from the root to find the minimum value.
5. Find Maximum:
• Step 1: Traverse the rightmost path of the tree from the root to find the maximum
value.
6. Display Tree:
• Step 1: Perform in-order traversal (left subtree, root, right subtree) to display all nodes
in ascending order.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* root = NULL;
int choice, value;
while (1) {
printf("\n1. Insert\n2. Delete\n3. Find Min\n4. Find Max\n5. Display (In-Order)\n6.
Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;
case 3:
if (root != NULL) {
Node* minNode = findMin(root);
printf("Minimum value: %d\n", minNode->data);
} else {
printf("Tree is empty.\n");
}
break;
case 4:
if (root != NULL) {
Node* maxNode = findMax(root);
printf("Maximum value: %d\n", maxNode->data);
} else {
printf("Tree is empty.\n");
}
break;
case 5:
if (root != NULL) {
printf("BST In-Order Traversal: ");
inOrder(root);
printf("\n");
} else {
printf("Tree is empty.\n");
}
break;
case 6:
exit(0);
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}
Input/Output:
Input:
1. Insert
Enter value to insert: 50
1. Insert
Enter value to insert: 30
1. Insert
Enter value to insert: 70
1. Insert
Enter value to insert: 20
1. Insert
Enter value to insert: 40
1. Insert
Enter value to insert: 60
1. Insert
Enter value to insert: 80
5. Display (In-Order)
BST In-Order Traversal: 20 30 40 50 60 70 80
3. Find Min
Minimum value: 20
4. Find Max
Maximum value: 80
2. Delete
Enter value to delete: 20
5. Display (In-Order)
BST In-Order Traversal: 30 40 50 60 70 80
6. Exit
Explanation:
• The program demonstrates the basic operations of a binary search tree including
insertion, deletion, and traversal.
• The minimum value (20) and maximum value (80) are identified correctly.
• The in-order traversal displays the tree elements in ascending order.
Result:
The binary search tree operations including insertion, deletion, finding the minimum
and maximum values, and in-order display were successfully implemented.
Ex.No. 7 IMPLEMENTATION OF AVL TREE
Date:
Aim:
To implement an AVL (Adelson-Velsky and Landis) Tree in C, which is a self-
balancing binary search tree. The AVL tree maintains its balance by ensuring that the heights
of the two child subtrees of any node differ by no more than one. This implementation includes
operations for insertion, rotation, and displaying the tree.
Algorithm:
1. AVL Tree Structure:
• Node Definition: Each node has a value, left and right children, and a height value.
2. Insertion:
• Step 1: Insert the node in the same way as a binary search tree.
• Step 2: Update the height of the current node.
• Step 3: Check the balance factor (difference in heights between left and right
In subtree x.leftChild and x.rightAVL trees, the height balance of
every node must be -1, 0, or 1 insert/delete via standard algorithms
After insert/delete: load balance (node) might be changed to +2 or −2
for certain nodes → re-balance load after each step
• Step 4: Perform rotations to balance the tree if needed:
o Right Rotation: For Left-Left (LL) imbalance.
o Left Rotation: For Right-Right (RR) imbalance.
o Left-Right Rotation: For Left-Right (LR) imbalance.
o Right-Left Rotation: For Right-Left (RL) imbalance.
3. Rotation Functions:
• Right Rotation (Single Rotation)
• Left Rotation (Single Rotation)
• Left-Right Rotation (Double Rotation)
• Right-Left Rotation (Double Rotation)
4. Display Tree:
• Perform an in-order traversal to display the tree elements in ascending order.
Program :
#include <stdio.h>
#include <stdlib.h>
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = 1 + (height(y->left) > height(y->right) ? height(y->left) : height(y->right));
x->height = 1 + (height(x->left) > height(x->right) ? height(x->left) : height(x->right));
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = 1 + (height(x->left) > height(x->right) ? height(x->left) : height(x->right));
y->height = 1 + (height(y->left) > height(y->right) ? height(y->left) : height(y->right));
int main() {
Node* root = NULL;
int choice, value;
while (1) {
printf("\n1. Insert\n2. Display (In-Order)\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("AVL Tree In-Order Traversal: ");
inOrder(root);
printf("\n");
break;
case 3:
exit(0);
break;
default:
printf("Invalid choice.\n");
}
}
return 0;
}
Input/Output:
Input:
1. Insert
2. Display (In-Order)
3. Exit
Enter your choice: 1
Enter value to insert: 10
1. Insert
2. Display (In-Order)
3. Exit
Enter your choice: 1
Enter value to insert: 20
1. Insert
2. Display (In-Order)
3. Exit
Enter your choice: 1
Enter value to insert: 30
1. Insert
2. Display (In-Order)
3. Exit
Enter your choice: 1
Enter value to insert: 40
1. Insert
2. Display (In-Order)
3. Exit
Enter your choice: 1
Enter value to insert: 50
1. Insert
2. Display (In-Order)
3. Exit
Enter your choice: 1
Enter value to insert: 25
1. Insert
2. Display (In-Order)
3. Exit
Enter your choice: 2
AVL Tree In-Order Traversal: 10 20 25 30 40 50
20
/ \
10 40
/ \
30 50
/
25
1. Insert
2. Display (In-Order)
3. Exit
Enter your choice: 3
Result:
The AVL Tree implementation successfully supports insertion while maintaining
balance. The in-order traversal correctly displays the elements of the AVL Tree.
Ex.No. 8a IMPLEMENTATION OF LINEAR SEARCH
Date:
Aim:
To implement a linear search algorithm in C to find an element in a given array.
Algorithm:
1. Start
2. Input: Read the number of elements in the array (n) and the elements of the array.
3. Input: Read the element (key) to be searched.
4. Initialize a loop variable i = 0.
5. Repeat steps 6–8 until i < n:
o Compare array[i] with key.
o If array[i] == key, print the position and stop.
6. If key is found, print "Element found at position i + 1".
7. If the loop completes without finding key, print "Element not found".
8. End.
Program
#include <stdio.h>
int main() {
int n, key, index;
return 0;
}
Output
Result
The program successfully implements the linear search algorithm and finds the
position of the specified element in the array, or reports if the element is not present.
Ex.No. 8b IMPLEMENTATION OF BINARY SEARCH
Date:
Aim:
To implement a binary search algorithm in C to find an element in a given sorted array.
Algorithm:
1. Start
2. Input: Read the number of elements in the array (n) and the elements of the sorted
array.
3. Input: Read the element (key) to be searched.
4. Initialize two variables low = 0 and high = n - 1.
5. Repeat steps 6–8 until low <= high:
o Calculate the middle index: mid = (low + high) / 2.
o If arr[mid] == key, print the position and stop.
o If arr[mid] < key, set low = mid + 1 (search in the right half).
o If arr[mid] > key, set high = mid - 1 (search in the left half).
6. If key is found, print "Element found at position mid + 1".
7. If the loop completes without finding key, print "Element not found".
8. End.
Program:
#include <stdio.h>
if (arr[mid] == key) {
return mid; // Return index if element is found
} else if (arr[mid] < key) {
low = mid + 1; // Search in the right half
} else {
high = mid - 1; // Search in the left half
}
}
int main() {
int n, key, index;
return 0;
}
Output
Result
The program successfully implements the binary search algorithm and finds the
position of the specified element in the sorted array, or reports if the element is not present.
Ex.No. 9 IMPLEMENTATION OF INSERTION SORT AND SELECTION SORT
Date :
Aim
To implement Insertion Sort and Selection Sort algorithms in C for sorting a given
array of elements.
INSERTION SORT
Algorithm (Insertion Sort)
1. Start
2. Input: Read the number of elements in the array (n) and the array elements.
3. Loop: For each element in the array, starting from index 1 to n-1:
o Set the current element as key.
o Compare the key with the elements before it in the sorted part of the array.
o Shift all the larger elements one position to the right.
o Insert the key at the correct position in the sorted part.
4. Repeat until the array is fully sorted.
5. Output: Display the sorted array.
6. End.
#include <stdio.h>
int main() {
int i, j, k, n, temp, a[20], p = 0;
Output
(Insertion Sort)
Enter total elements (max 20): 6
Enter array elements: 34 8 64 51 32 21
After Pass 1: 8 34 64 51 32 21
After Pass 2: 8 34 64 51 32 21
After Pass 3: 8 34 51 64 32 21
After Pass 4: 8 32 34 51 64 21
After Pass 5: 8 21 32 34 51 64
Sorted List: 8 21 32 34 51 64
SELECTION SORT
Algorithm (Selection Sort)
1. Start
2. Input: Read the number of elements in the array (n) and the array elements.
3. Loop: For each element from index 0 to n-2:
o Find the minimum element in the unsorted part of the array.
o Swap the minimum element with the element at the current position.
4. Repeat until the array is fully sorted.
5. Output: Display the sorted array.
6. End.
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, min_idx, temp;
int main() {
int n, i;
return 0;
}
Output
(Selection Sort)
Enter number of elements: 5
Enter array elements:
64 25 12 22 11
Sorted array using Selection Sort:
11 12 22 25 64
Result
Both Insertion Sort and Selection Sort have been successfully implemented in C.
The programs efficiently sort the input array in ascending order and display the sorted array
as output.
Ex.No. 10 IMPLEMENTATION OF BUBBLE SORT AND MERGE SORT
Date :
Aim :
To implement Bubble Sort and Merge Sort algorithms in C to sort the given array of
elements.
BUBBLE SORT
Algorithm : (Bubble Sort)
1. Start
2. Initialize Variables:
Define an integer array a[20] to hold the elements.
Define integers n (number of elements), t (for swapping), i, j, k, and p (for pass
count).
3. Input Number of Elements:
Prompt the user to enter the total number of elements (up to 20).
Read the integer value into n.
4. Check Input Limit:
If n is greater than 20, display an error message and terminate the program.
5. Input Array Elements:
Prompt the user to enter n elements.
Use a loop to read n integers into the array a.
6. Bubble Sort Logic:
a. For i from n-1 down to 1:
i. For j from 0 to i-1:
1. If a[j] > a[j + 1], then:
2. Swap a[j] and a[j + 1] using a temporary variable t.
b. Increment the pass count p.
c. Display the array after the current pass.
7. Output Sorted Array:
After sorting is complete, print the sorted list of elements.
8. End
#include <stdio.h>
int main() {
int n, t, i, j, k, a[20], p = 0;
return 0;
}
After Pass 1: 15 7 25 10 36 54
After Pass 2: 7 15 10 25 36 54
After Pass 3: 7 10 15 25 36 54
After Pass 4: 7 10 15 25 36 54
After Pass 5: 7 10 15 25 36 54
Sorted List: 7 10 15 25 36 54
MERGE SORT
#include <stdio.h>
int main() {
int i, arr[30];
Result
Both Bubble Sort and Merge Sort algorithms were successfully implemented in C.
The programs efficiently sort the input array in ascending order and display the sorted array
as output.
Ex.No. 11 IMPLEMENTATION OF DIJKSTRA'S ALGORITHM IN C
Date :
Aim:
To implement Dijkstra's Algorithm in C to find the shortest path from a source vertex
to all other vertices in a given weighted graph.
Algorithm:
1. Start.
2. Input: Read the number of vertices and the graph represented as an adjacency matrix
(weights of edges).
3. Input: Read the source vertex from which the shortest path is to be calculated.
4. Initialize:
o Distance array dist[] to hold the shortest distance from the source to each
vertex.
o Boolean array sptSet[] to track vertices included in the shortest path tree.
5. Set the initial values:
o Set dist[source] = 0 (distance from the source to itself is 0) and all other dist[]
values to infinity.
o Mark all vertices in sptSet[] as false.
6. Repeat for all vertices:
o Find the vertex u not yet included in the shortest path tree that has the smallest
distance.
o Include u in the shortest path tree (sptSet[u] = true).
o For each adjacent vertex v of u, update the distance dist[v] if and only if:
▪ v is not yet included in sptSet[].
▪ There is an edge from u to v.
▪ The total weight of the path from the source to v through u is smaller
than the current value of dist[v].
7. Repeat step 6 until all vertices are included in the shortest path tree.
8. Output: Print the shortest distance from the source to all other vertices.
9. End.
C Program:
#include <stdio.h>
#include <limits.h>
// Function to find the vertex with the minimum distance value from
// the set of vertices not yet included in the shortest path tree
int minDistance(int dist[], int sptSet[]) {
int min = INT_MAX, min_index;
return min_index;
}
int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = {
{0, 10, 0, 30, 100},
{10, 0, 50, 0, 0},
{0, 50, 0, 20, 10},
{30, 0, 20, 0, 60},
{100, 0, 10, 60, 0}
};
int source;
printf("Enter the source vertex (0 to 4): ");
scanf("%d", &source);
return 0;
}
Explanation:
1. Graph Representation:
o The graph is represented as an adjacency matrix where each cell graph[i][j]
holds the weight of the edge between vertex i and vertex j.
o A value of 0 in the matrix means there is no direct edge between those two
vertices.
2. Algorithm Logic:
o The algorithm initializes all vertex distances to infinity (except the source
vertex) and iteratively finds the vertex with the smallest distance from the
source.
o For each vertex u selected, its adjacent vertices are checked, and their distances
are updated if the path through u offers a shorter route.
o This continues until all vertices have been processed and the shortest paths
from the source to all other vertices are found.
3. Functions:
o minDistance(): Finds the vertex with the minimum distance value that hasn't
been processed yet.
o printSolution(): Prints the shortest distances from the source to all other
vertices.
o dijkstra(): Implements the main logic for finding the shortest path using
Dijkstra's Algorithm.
Output:
Result:
The program successfully implements Dijkstra's Algorithm and calculates the shortest
path from the source vertex to all other vertices in the graph.
Ex.No. 12 IMPLEMENTATION OF PRIM'S ALGORITHM IN C
Date :
Aim:
To implement Prim's Algorithm for finding the Minimum Spanning Tree (MST) of a
weighted, undirected graph using C programming.
Algorithm:
1. Initialization:
o Select a starting vertex and initialize it as included in the MST.
o Create an array to track the included vertices.
o Create an array to store the minimum edge weights for each vertex.
2. Select the Minimum Edge:
o While there are vertices not included in the MST:
1. Find the vertex with the minimum edge weight that is not included in
the MST.
2. Include this vertex in the MST.
3. Update the edge weights of the adjacent vertices of the newly included
vertex.
3. Output the MST:
o Print the edges that form the Minimum Spanning Tree.
C Program Implementation
#include <stdio.h>
#include <limits.h>
int main() {
// Example graph represented as an adjacency matrix
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
Explanation
1. Graph Representation:
o The graph is represented as an adjacency matrix, where graph[i][j] holds the
weight of the edge connecting vertex i and vertex j. A weight of 0 means no
edge exists.
2. Functions:
o minKey: This function finds the vertex with the minimum key value from the
set of vertices not yet included in the MST.
o primMST: Implements Prim's Algorithm. It initializes necessary arrays,
iteratively finds the minimum edge to add to the MST, and updates the keys
and parents accordingly.
3. Output:
o After constructing the MST, the program prints the edges included in the MST
along with their weights.
Output
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
The output displays the edges and their respective weights that form the Minimum Spanning
Tree of the given graph. Each edge connects two vertices in the graph while ensuring the
minimum total edge weight for the spanning tree.
Results