You are on page 1of 84

Institute’s Vision

To be an organisation with potential for excellence in engineering and management for


the advancement of society and human kind.

Institute’s Mission

To excel in academics, practical engineering, management and to commence research endeavours.

To prepare students for future opportunities.

To nurture students with social and ethical responsibilities.


Department’s Vision

To create IT graduates with ethical and employable skills.

Department’s Mission

To imbibe problem solving and analytical skills through teaching learning process.
To impart technical and managerial skills to meet the industry requirement. To encourage ethic
Excelssior’s Education Society

K. C. COLLEGE OF ENGINEERING
AND MANAGEMENT STUDIES AND
RESEARCH THANE (EAST).

Certificate

This is to certify that Mr. / Ms.


of Semester
Branch _ Roll No.
has performed and successfully completed all the practical’s in the subject
of __ for the
academic year 20_ to 20_ as prescribed by University of Mumbai.

DATE :- .

Practical Incharge Internal Examiner

_ _ _ _ _ _ _ _ _
Head of Department External Examiner

COLLEGE SEAL
SYLLABUS

Sr.no Module Detailed Content Hours LO Mapping


0 Prerequisite Introduction of C programming language
1. Implementations of stack menu
driven program
2. Implementation of multitask in
LO1
one array
LO2
I Stack 3. *Implementations of Infix to Postfix 04
LO3
Transformation and its evaluation
LO6
program. 4. Implementations of Infix to
Prefix Transformation and its
evaluation program
1. Implementations of circular
queue menu driven program
2. * Implementations of double LO1
ended queue menu driven program LO2
II Queue 04
3. Implementations of queue LO3
menu driven program LO6
4. Implementation of Priority
queue program using array.
1. Implementations of Linked Lists
menu driven program.
2. *Implementation of different
operations on linked list –copy,
LO1
concatenate, split, reverse, count no.
LO2
III Linked List of nodes etc 04
LO3
3. Implementation of polynomials
LO6
operations ( addition, subtraction)
using Linked List.
4. Implementations of Linked Lists menu
driven program (stack and queue)
1. Implementations of Binary Tree
menu driven program
2. Implementation of Binary
Tree Traversal program.
LO1
3. *Implementation of construction of
Tree & LO2
IV expression tree using postfix 04
Graph LO3
expression.
LO6
4. Implementations of BST program
5. Implementation of various operations
on tree like – copying tree, mirroring a
tree, counting the number of nodes in
the tree, counting only 04 LO1 LO2
LO3
LO6 University of Mumbai, B. E.
(Information Technology), Rev 2016 27
leaf nodes in the tree.
6. Implementation of Preorder
traversal of a threaded binary tree.
7. Implementations of Huffman
code construction
8. Implementations of Graph menu
driven program (DFS & BSF)
1. Implementations of Shell sort,
Radix sort and Insertion sort menu
driven program.
2. *Implementations of Quick Sort,
LO4
V Sorting Merge sort and Heap Sort menu 04
LO5
driven program
LO6
3. Implementations of Advanced
Bubble Sort, Insertion Sort and
Selection Sort menu driven program
1. Implementations of searching
methods (Index Sequential, Interpolation
Search) menu driven program LO4
VI Searching 02
2. *Implementation of hashing LO5
functions with different collision LO6
resolution techniques
Program Outcomes
Engineering Graduates will be able to:

1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.

2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.

3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.

4. Conduct investigations of complex problems: Use research-based knowledge and research


methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.

6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.

8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.

10. Communication: Communicate effectively on complex engineering activities with the


engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.

11. 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.
12. 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.
Department of Information Technology

Subject : DATA STRUCTURES LAB

Semester: III

Class : SEIT

Lab Outcomes

Lab Outcomes
Lab Code
At the end of experiment student will able to

Implement linear and non-linear data structures.


ITL302.1

Implement application of linear and non-linear data


ITL302.2 structure.

Implement operations on link list.


ITL302.3

Implement searching and sorting algorithms.


ITL302.4

Design advanced data structure using non-linear data


structure.
ITL302.5
Rubrics of Practical

Rubrics Maximum 15-12 12-9 9-6 6-0


Description Marks
Weight
Implementation 5 Successful Output Few errors Incorrect
(R1) completion correct but in the Output
with accurate not precise output (2-0)
output (5-4) (4-3) (3-2)
Understanding 5 Understanding Understand Improper No
(R2) Experiment Experiment Conclusion Conclusion
and drawn but (3-2) (2-0)
correct conclusion
conclusion less
(5-4) accurate
(4-3)
Punctuality and 5 Submission Submission Submission Submission
Discipline within a after week after two after three
(R3) week (5-4) (4-3) weeks weeks and
(3-2) more (2-0)
TABLE OF CONTENTS

Sr. Date of Date of Page Grade


Name of Experiment / Sign
No Conduction Submission No. Marks

1
2
3
4
5
6
7
8
9
10
Total Grade / Marks :-

Avg. marks of Experiments Avg. marks of Assignments


(A) (B) Total Marks
(A+B)
Obtained Out of Obtained Out of

Practical Incharge Date


EXPERIMENT NO. - 01

Aim of the experiment :-

Course Outcome :-

Date of Conduction : Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT NO 1

AIM: Implementation of Stack using Array and Link list.

ALGORITHM:
1. Stack using Array

Push ( )
Description: Here STACK is an array with MAX locations.
TOP points to the top most elements and ITEM are the value to be inserted.
1. If (TOP == MAX) Then [Check for overflow]
2. Print: Overflow
3. Else
4. Set TOP = TOP + 1 [Increment TOP by 1]
5. Set STACK[TOP] = ITEM [Assign ITEM to top of STACK]
6. Print: ITEM inserted [End of If]
7. Exit

Pop ( )
1. If (TOP == 0) Then [Check for underflow]
2. Print: Underflow
3. Else
4. Set ITEM = STACK[TOP] [Assign top of STACK to ITEM]
5. Set TOP = TOP - 1 [Decrement TOP by 1]
6. Print: ITEM deleted [End of If]
7. Exit

2. Stack using linked list

Stack empty
1. if L.head == NIL
return True
2. else
3. return False

Push(x)
1. x.next = L.head
2. if L.head != NIL
L.head.prev = x
3. L.head = x
4. x.prev = NIL

Pop()
1. x = L.head
2. L.head = x.next
3. x.next.prev = L.head
4. return x
Lab Outcomes:
Stack Using Array
1. Push()
#include <stdio.h>
#define MAX 100 // Maximum stack size
int STACK[MAX]; // Stack array
int TOP = -1; // Initialize TOP to -1
void push(int ITEM) {
if (TOP == MAX - 1) {
printf("Overflow\n");
} else {
TOP++; // Increment TOP
STACK[TOP] = ITEM; // Push ITEM onto the stack
printf("%d inserted\n", ITEM);
}
}
int main() {
int item;
printf("Enter the item to push onto the stack: ");
scanf("%d", &item);
push(item);
return 0;
}

Output:

2.Pop()
#include <stdio.h>
#define MAX 100 // Maximum stack size
int STACK[MAX]; // Stack array
int TOP = -1; // Initialize TOP to -1
int pop() {
int ITEM;
if (TOP == -1) {
printf("Underflow\n");
ITEM = -1; // Return -1 to indicate underflow
} else {
ITEM = STACK[TOP]; // Pop the top item
TOP--; // Decrement TOP
printf("%d deleted\n", ITEM);
}
return ITEM; // Return the popped item (or -1 for underflow)
}
int main() {
int item;
int poppedItem = pop();
if (poppedItem != -1) {
printf("Popped item: %d\n", poppedItem);
}
return 0;
}
Output:

Stack Using Linked list

Stack Empty
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int isEmpty(struct Node* head) {
return (head == NULL);
}
int main() {
struct Node* myList = NULL; // Initialize the linked list as empty
if (isEmpty(myList)) {
printf("The linked list is empty (True).\n");
} else {
printf("The linked list is not empty (False).\n");
}
return 0;
}

Output:

Push(x)
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct LinkedList {
struct Node* head;
};
void insertAtBeginning(struct LinkedList* L, int value) {
struct Node* x = malloc(sizeof(struct Node));
x->data = value;
x->next = L->head;
x->prev = NULL;
if (L->head) L->head->prev = x;
L->head = x;
}
int main() {
struct LinkedList myList = { .head = NULL };

// Insert nodes at the beginning


insertAtBeginning(&myList, 3);
insertAtBeginning(&myList, 2);
insertAtBeginning(&myList, 1);
// Print the linked list
for (struct Node* current = myList.head; current; current = current->next)
printf("%d -> ", current->data);
printf("NULL\n");
return 0;
}

Output:

Pop()

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Stack {
struct Node* head;
};
struct Node* pop(struct Stack* L) {
if (L->head == NULL) {
printf("Stack is empty. Cannot pop.\n");
return NULL;
}
struct Node* x = L->head;
L->head = x->next;
if (x->next != NULL) {
x->next->prev = NULL;
}
x->next = NULL;
return x;
}
void push(struct Stack* L, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = L->head;
if (L->head) {
L->head->prev = newNode;
}
newNode->prev = NULL;
L->head = newNode;
}

void displayStack(struct Stack* L) {


struct Node* current = L->head;
printf("Stack: ");
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Stack myStack;
myStack.head = NULL;
// Push some values onto the stack
push(&myStack, 3);
push(&myStack, 2);
push(&myStack, 1);
displayStack(&myStack);
// Pop an element from the stack
struct Node* poppedNode = pop(&myStack);
if (poppedNode != NULL) {
printf("Popped value: %d\n", poppedNode->data);
free(poppedNode); // Free the memory of the popped node
}
displayStack(&myStack);
return 0;
}

Output:
EXPERIMENT NO. - 02

Aim of the experiment :-

Course Outcome :-

Date of Conduction :Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT NO :2
AIM: Implementation of Queue using Array and Link list.

ALGORITHM:
1. Queue using Array

Insert ( )
1. If (REAR == N) Then [Check for overflow]
2. Print: Overflow
3. Else
4. If (FRONT and REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1
(b) Set REAR = 1 5. Else
6. Set REAR = REAR + 1 [Increment REAR by 1]
[End of Step 4 If]
7. QUEUE[REAR] = ITEM
8. Print: ITEM
inserted [End of Step 1
If]
9. Exit

Delete ( )
1. If (FRONT == 0) Then [Check for underflow]
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [Check if only one element is left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else
7. Set FRONT = FRONT + 1 [Increment FRONT by
1] [End of Step 5 If]
8. Print: ITEM
deleted [End of Step 1
If]
9. Exit

2. Queue using Link list


Queue Empty:
1. if L.head == NIL
return True
2. else return False

Insert(x)
1. if L.head == NIL:
x.prev = NIL
L.head = x
2. else
cur = L.head
3. while cur.next != NIL
cur = cur.next
4. cur.next = x
5. x.prev = cur
6. x.next = NIL

Delete()
1. x = L.head
2. L.head = x.next
3. x.next.prev = L.head
4. return x

Conclusion:

Lab Outcomes:
Queue using array
Insert()
#include <stdio.h>
#include <stdlib.h>
#define N 5 // Maximum size of the queue
int QUEUE[N];
int FRONT = 0;
int REAR = 0;
void enqueue(int ITEM) {
if (REAR == N) {
printf("Overflow: Queue is full.\n");
} else {
if (FRONT == 0 && REAR == 0) {
FRONT = 1;
REAR = 1;
} else {
REAR = REAR + 1;
}
QUEUE[REAR] = ITEM;
printf("%d inserted\n", ITEM);
}
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
enqueue(5);
// Attempting to insert one more item to cause overflow
enqueue(6);
return 0;
}

Output:
Delete()
#include <stdio.h>
#include <stdlib.h>
#define N 5 // Maximum size of the queue
int QUEUE[N];
int FRONT = 0;
int REAR = 0;
void enqueue(int ITEM) {
if (REAR == N) {
printf("Overflow: Queue is full.\n");
} else {
if (FRONT == 0 && REAR == 0) {
FRONT = 1;
REAR = 1;
} else {
REAR = REAR + 1;
}
QUEUE[REAR] = ITEM;
printf("%d inserted\n", ITEM);
}
}
void dequeue() {
if (FRONT == 0) {
printf("Underflow: Queue is empty.\n");
} else {
int ITEM = QUEUE[FRONT];
if (FRONT == REAR) {
FRONT = 0;
REAR = 0;
} else {
FRONT = FRONT + 1;
}
printf("%d deleted\n", ITEM);
}
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
enqueue(5);
dequeue(); // Dequeue one item
dequeue(); // Dequeue another item
dequeue(); // Dequeue another item
dequeue(); // Dequeue another item
dequeue(); // Dequeue the last item
// Attempting to dequeue when the queue is empty
dequeue();
return 0;
}
Output:

Queue using linked list

Queue Empty()
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node* head;
};
int isEmpty(struct Queue* L) {
if (L->head == NULL) {
return 1; // True, the queue is empty
} else {
return 0; // False, the queue is not empty
}
}
int main() {
struct Queue myQueue;
myQueue.head = NULL; // Initialize the queue as empty
if (isEmpty(&myQueue)) {
printf("The queue is empty (True).\n");
} else {
printf("The queue is not empty (False).\n");
}
return 0;
}

Output:
Insert()

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Queue {
struct Node* head;
};
void enqueue(struct Queue* L, int value) {
struct Node* x = (struct Node*)malloc(sizeof(struct Node));
x->data = value;
x->prev = NULL;
x->next = NULL;
if (L->head == NULL) {
L->head = x;
} else {
struct Node* cur = L->head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = x;
x->prev = cur;
}
}
void displayQueue(struct Queue* L) {
struct Node* current = L->head;
printf("Queue: ");
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Queue myQueue;
myQueue.head = NULL;
// Enqueue elements
enqueue(&myQueue, 1);
enqueue(&myQueue, 2);
enqueue(&myQueue, 3);
displayQueue(&myQueue);
return 0;
}

Output:
Delete()

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Queue {
struct Node* head;
};
struct Node* dequeue(struct Queue* L) {
if (L->head == NULL) {
printf("Underflow: Queue is empty.\n");
return NULL;
}
struct Node* x = L->head;
L->head = x->next;
if (x->next != NULL) {
x->next->prev = L->head;
}
x->next = NULL;
x->prev = NULL;
return x;
}
void displayQueue(struct Queue* L) {
struct Node* current = L->head;
printf("Queue: ");
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Queue myQueue;
myQueue.head = NULL;
// Enqueue elements
for (int i = 1; i <= 5; i++) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = i;
newNode->next = NULL;
newNode->prev = NULL;
if (myQueue.head == NULL) {
myQueue.head = newNode;
} else {
struct Node* cur = myQueue.head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = newNode;
newNode->prev = cur;
}
}
displayQueue(&myQueue);
// Dequeue elements
struct Node* dequeuedNode;
while ((dequeuedNode = dequeue(&myQueue)) != NULL) {
printf("Dequeued: %d\n", dequeuedNode->data);
free(dequeuedNode);
}
// Attempting to dequeue when the queue is empty
dequeue(&myQueue);
return 0;
}

Output:
EXPERIMENT NO :03

Aim of the experiment :-

Course Outcome :-

Date of Conduction : Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT NO :3

AIM: Implementation Circular Queue using Array.

ALGORITHM:

Insert( )
1. If (FRONT == 1 and REAR == N) or (FRONT == REAR + 1) Then
2. Print: Overflow
3. Else
4. If (REAR == 0) Then [Check if QUEUE is empty]
(a) Set FRONT = 1
(b) Set REAR = 1
5. Else If (REAR == N) Then [If REAR reaches end if QUEUE]
6. Set REAR = 1
7. Else
8. Set REAR = REAR + 1 [Increment REAR by
1] [End of Step 4 If]
9. Set QUEUE[REAR] = ITEM
10. Print: ITEM
inserted [End of Step 1
If]
11. Exit
Delete()
1. If (FRONT == 0) Then [Check for Underflow]
2. Print: Underflow
3. Else
4. ITEM = QUEUE[FRONT]
5. If (FRONT == REAR) Then [If only element is left]
(a) Set FRONT = 0
(b) Set REAR = 0
6. Else If (FRONT == N) Then [If FRONT reaches end if QUEUE]
7. Set FRONT = 1
8. Else
9. Set FRONT = FRONT + 1 [Increment FRONT by
1] [End of Step 5 If]
10. Print: ITEM
deleted [End of Step 1
If]
11. Exit

Conclusion :

Lab Outcomes
Circular queue using array
Insert()
#include <stdio.h>
#define N 5 // Maximum size of the circular queue
int QUEUE[N];
int FRONT = -1;
int REAR = -1;
int isFull() {
return ((FRONT == 0 && REAR == N - 1) || (FRONT == REAR + 1));
}
int isEmpty() {
return (FRONT == -1);
}
void enqueue(int ITEM) {
if (isFull()) {
printf("Overflow: Circular Queue is full.\n");
} else {
if (FRONT == -1) {
FRONT = 0;
REAR = 0;
} else if (REAR == N - 1) {
REAR = 0;
} else {
REAR++;
}
QUEUE[REAR] = ITEM;
printf("%d inserted\n", ITEM);
}
}
void displayQueue() {
int i = FRONT;
if (FRONT == -1) {
printf("Circular Queue is empty.\n");
return;
}
printf("Circular Queue: ");
if (FRONT <= REAR) {
while (i <= REAR) {
printf("%d -> ", QUEUE[i]);
i++;
}
} else {
while (i < N) {
printf("%d -> ", QUEUE[i]);
i++;
}
i = 0;
while (i <= REAR) {
printf("%d -> ", QUEUE[i]);
i++;
}
}
printf("NULL\n");
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
enqueue(5);
displayQueue();
// Attempting to insert one more item to cause overflow
enqueue(6);
return 0;
}

Output:

Delete()
#include <stdio.h>
#define N 5 // Maximum size of the circular queue
int QUEUE[N];
int FRONT = -1;
int REAR = -1;
int isFull() {
return ((FRONT == 0 && REAR == N - 1) || (FRONT == REAR + 1));
}
int isEmpty() {
return (FRONT == -1);
}
void enqueue(int ITEM) {
if (isFull()) {
printf("Overflow: Circular Queue is full.\n");
} else {
if (FRONT == -1) {
FRONT = 0;
REAR = 0;
} else if (REAR == N - 1) {
REAR = 0;
} else {
REAR++;
}
QUEUE[REAR] = ITEM;
printf("%d inserted\n", ITEM);
}
}
int dequeue() {
if (isEmpty()) {
printf("Underflow: Circular Queue is empty.\n");
return -1; // Return -1 to indicate underflow
} else {
int ITEM = QUEUE[FRONT];
if (FRONT == REAR) {
FRONT = -1;
REAR = -1;
} else if (FRONT == N - 1) {
FRONT = 0;
} else {
FRONT++;
}
printf("%d deleted\n", ITEM);
return ITEM;
}
}
void displayQueue() {
int i = FRONT;

if (FRONT == -1) {
printf("Circular Queue is empty.\n");
return;
}
printf("Circular Queue: ");
if (FRONT <= REAR) {
while (i <= REAR) {
printf("%d -> ", QUEUE[i]);
i++;
}
} else {
while (i < N) {
printf("%d -> ", QUEUE[i]);
i++;
}
i = 0;
while (i <= REAR) {
printf("%d -> ", QUEUE[i]);
i++;
}
}
printf("NULL\n");
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
enqueue(4);
enqueue(5);
displayQueue();
int dequeuedItem;
// Dequeue elements
while ((dequeuedItem = dequeue()) != -1) {
// Process dequeued items
}
// Attempting to dequeue when the queue is empty
dequeue();
return 0;
}

Output:
EXPERIMENT NO. - 04

Aim of the experiment :-

Course Outcome :-

Date of Conduction : Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT NO :4

AIM: Implementation of Deque using Array.

ALGORITHM:

Insert_rear()
1. [Check for overflow]
2. if(rear==MAX)
Print("Queue is Overflow”);
return;
3. [Insert element]
4. else
rear=rear+1;
q[rear]=no;
5. [Set rear and front pointer]
6. if rear=0
rear=1;
7. if front=0
front=1;
8. Return

Insert_front()
1. [Check for the front position]
2. if(front<=1)
Print (“Cannot add item at front
end”); Return;

3. [Insert at front]
4. Else
front=front-1;
q[front]=no;
5. Return

Delete_front()
1. [ Check for front pointer]
2. if front=0
print(" Queue is Underflow”);
return;
3. [Perform deletion]
4. else
no=q[front];
print(“Deleted element is”,no);
5. [Set front and rear pointer]
6. if front=rear

front=0;
rear=0;
7. else
front=front+1;
8. Return

Delete_rear()
1. [Check for the rear pointer]
2. if rear=0
print(“Cannot delete value at rear end”);
return;
3. [perform deletion]
4. else
no=q[rear];
5. [Check for the front and rear pointer]
6.if front= rear
front=0;
rear=0;
7.else
rear=rear-1;
print(“Deleted element is”,no);
8. Return

Conclusion:

Lab Outcomes:
Insert_rear
#include <stdio.h>
#define MAX 5 // Maximum size of the deque
int deque[MAX];
int front = -1;
int rear = -1;
void insertRear(int no) {
if (rear == MAX - 1) {
printf("Queue is Overflow\n");
return;
} else {
rear = rear + 1;
deque[rear] = no;
if (rear == 0) {
rear = 1;
}
if (front == 0) {
front = 1;
}
}
}
void displayDeque() {
if (front == -1) {
printf("Deque is empty.\n");
return;
}
printf("Deque: ");
for (int i = front; i <= rear; i++) {
printf("%d -> ", deque[i]);
}
printf("NULL\n");
}
int main() {
insertRear(1);
insertRear(2);
insertRear(3);
insertRear(4);
insertRear(5);
displayDeque();
return 0;
}

Output:
Insert_front
#include <stdio.h>
#define MAX 5 // Maximum size of the deque
int deque[MAX];
int front = -1;
int rear = -1;
void insertFront(int no) {
if (front <= 0) {
printf("Cannot add item at front end\n");
return;
} else {
front = front - 1;
deque[front] = no;
}
}
void displayDeque() {
if (front == -1) {
printf("Deque is empty.\n");
return;
}
printf("Deque: ");
for (int i = front; i <= rear; i++) {
printf("%d -> ", deque[i]);
}
printf("NULL\n");
}
int main() {
front = MAX / 2; // Initialize front to the middle of the deque
rear = MAX / 2;
insertFront(3);
insertFront(2);
insertFront(1);
displayDeque();
return 0;
}

Output:
Delete_front

#define MAX 5 // Maximum size of the deque


int deque[MAX];
int front = 0;
int rear = 0;
void deleteFront() {
if (front == 0) {
printf("Queue is Underflow\n");
return;
} else {
int no = deque[front];
printf("Deleted element is %d\n", no);
if (front == rear) {
front = 0;
rear = 0;
} else {
if (front == MAX - 1) {
front = 0;
} else {
front = front + 1;
}
}
}
}
void displayDeque() {
if (front == 0) {
printf("Deque is empty.\n");
return;
}
printf("Deque: ");
for (int i = front; i <= rear; i++) {
printf("%d -> ", deque[i]);
}
printf("NULL\n");
}
int main() {
rear = MAX / 2; // Initialize rear to the middle of the deque
front = MAX / 2;
deleteFront();
deleteFront();
deleteFront();
displayDeque();
return 0;
}
Output:

Delete_rear
#include <stdio.h>
#define MAX 5 // Maximum size of the deque
int deque[MAX];
int front = 0;
int rear = 0;
void deleteRear() {
if (rear == 0) {
printf("Cannot delete value at rear end\n");
return;
} else {
int no = deque[rear];
if (front == rear) {
front = 0;
rear = 0;
} else {
if (rear == 1) {
rear = 0;
} else {
rear = rear - 1;
}
}
printf("Deleted element is %d\n", no);
}
}
void displayDeque() {
if (front == 0) {
printf("Deque is empty.\n");
return;
}
printf("Deque: ");
for (int i = front; i <= rear; i++) {
printf("%d -> ", deque[i]);
}
printf("NULL\n");
}
int main() {
rear = MAX / 2; // Initialize rear to the middle of the deque
front = MAX / 2;
deleteRear();
deleteRear();
deleteRear();
displayDeque();
return 0;
}

Output:
EXPERIMENT NO. - 05

Aim of the experiment :-

Course Outcome :-

Date of Conduction : Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT NO :5

AIM: Program to convert Infix to postfix expression. (Application of Stack)

ALGORITHM:

Transform ( )
Description: Here I is an arithmetic expression written in infix notation and P is the
equivalent postfix expression generated by this algorithm.

1. Push “(“ left parenthesis onto stack.


2. Add “)” right parenthesis to the end of expression I.
3. Scan I from left to right and repeat step 4 for each element of I until the
stack becomes empty.
4. If the scanned element is:
(a) an operand then add it to P.
(b) a left parenthesis then push it onto stack.
(c) an operator then:
(i) Pop from stack and add to P each operator which has the same or higher
precedence
then the scanned operator.
(ii) Add newly scanned operator to stack.
(d) a right parenthesis then:
(i) Pop from stack and add to P each operator until a left parenthesis is
encountered.
(ii) Remove the left parenthesis.
[End of Step 4 If]
[End of step 3 For Loop]
5. Exit.

Conclusion:

Lab Outcomes:
Infix to postfix

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
int precedence(char operator) {
if (operator == '^')
return 3;
else if (operator == '*' || operator == '/')
return 2;
else if (operator == '+' || operator == '-')
return 1;
else
return 0;
}
int isOperator(char ch) {
return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^');
}
void infixToPostfix(char infix[], char postfix[]) {
char stack[MAX];
int top = -1;
stack[++top] = '('; // Push a left parenthesis onto the stack
strcat(infix, ")"); // Add a right parenthesis to the end of the infix expression
int i = 0, j = 0;
while (infix[i] != '\0') {
if (infix[i] == ' ' || infix[i] == '\t') {
i++;
continue; // Skip whitespace
}
if (infix[i] == '(') {
stack[++top] = '(';
i++;
} else if (isOperator(infix[i])) {
while (precedence(stack[top]) >= precedence(infix[i])) {
postfix[j++] = stack[top--];
}
stack[++top] = infix[i];
i++;
} else if (infix[i] == ')') {
while (stack[top] != '(') {
postfix[j++] = stack[top--];
}
top--; // Remove the left parenthesis from the stack
i++;
} else {
while (infix[i] != ' ' && infix[i] != '\t' && infix[i] != '\0' &&
!isOperator(infix[i]) && infix[i] != '(' && infix[i] != ')') {
postfix[j++] = infix[i++];
}
postfix[j++] = ' '; // Separate operands with a space
}
}
while (stack[top] != '(') {
postfix[j++] = stack[top--];
}
postfix[j] = '\0'; // Null-terminate the postfix expression
}
int main() {
char infix[MAX];
char postfix[MAX];
printf("Enter an infix expression: ");
fgets(infix, sizeof(infix), stdin);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}

Output:
EXPERIMENT NO. - 06

Aim of the experiment :-

Course Outcome :-

Date of Conduction : Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT NO : 6

AIM: Program to perform Link list operations.

ALGORITHM:

Traverse ( )
Description: Here START is a pointer variable which contains the address of first node.
PROCESS is any operation that is to be performed on the node.
1. Set PTR = START
2. Repeat While (PTR != NULL)
3. Apply PROCESS to PTR->INFO
4. PTR = PTR->LINK
[End of While Loop]
5. Exit
Create()
1. first=new node;{create the 1st node of the list pointed by first};
2. Read(Data(first));
3. NEXT(First)=NULL;
4. Far a First; [point Far to the First]
5. For I=1 to N-1 repeat steps 6 to 10
6. X=new node;
7. Read(Data(X))
8. NEXT(X)=NULL;
9. NEXT(Far)=X; {connect the nodes}
10. Far=X;[shift the pointer to the last node of the list]
[end of For Loop]
11. END
Traverse()
1. If First=NULL then {print “List empty” STOP};
2. count=0;
3. ptr=First; {point ptr to the 1st node}
4. While ptr<> NULL repeat Steps 5 to 6
5. count=count+1;
6. ptr=NEXT(ptr) [shift ptr to the next node]
7. print (‘Number of nodes=’, count)
8. END

Search()
1. If first=NULL then{
Print “List empty”; STOP;}
2. ptr=First; [point ptr to the 1st node]
3. while (ptr<>NULL) repeat steps 4 to 5
4. If (DATA (ptr)= ‘X’)
Then {print “item found”;
STOP
}
5. ptr=NEXT (ptr); [shift ptr to the next node]
[end of while]
6. Print “item not found”;
7. END
Insert_first()
1. X=new node;
2. Read(DATA(X);
3. If (FIRST=NULL) then
{
First=X;
NEXT(X)=NULL;
}
Els
e
{ NEXT(X)=First;
First=X;

}
4. END
Insert_afternode()
1. Ptr=List;
2. While (ptr<>NULL) repeat steps 3 to 4
3. If (DATA(ptr)=’VAL’) then
{
X=new node
Read (DATA(X);
NEXT(X)=NEXT(ptr)
NEXT(ptr)=X;
BREAK;}
4. ptr=NEXT(ptr)
[end of while]
5. END.
Insert_beforenode()
1. ptr=LIST
[check if the first node is the desired one]
2. If(data(ptr)=’VAL’) then
{
X=new node;
Read DATA(X);
NEXT(X)=LIST;
LIST=X;
STOP;
}
3. While(ptr<>NULL ) repeat step 4 to 6
4. back=ptr;
5. ptr=NEXT(ptr);
6. If(DATA(ptr)=’VAL’)then
{
X=new node;
Read DATA(X);
NEXT(X)=ptr;
NEXT(back)=X;
EXIT;
}
[end of while loop]
7. END
Delete()
[CHECK IF THE FIRST NODE IS THE DESIRED ONE]
1. If (DATA(list)=’VAL’)then
{
Ptr=LIST;
LIST=NEXT(list);
Delete ptr;
Stop;
}
Back=list;
Ptr=list;
2. while(ptr<>NULL) repeat step 3 to 5
3. If(DATA(ptr)=’VAL’) then
{
NEXT(back)=NEXT(ptr);
Delete ptr;
Exit;}
4. back=ptr;
5. ptr=next(ptr);
[end of while loop]
6. END

Conclusion:

Lab Outcomes:
Traverse()

#include <stdio.h>
#include <stdlib.h>
// Define a structure for a linked list node
struct Node {
int INFO;
struct Node* LINK;
};
// Function to process a node
void processNode(int data) {
printf("Processing node with data: %d\n", data);
// You can perform any operation on the data here
}
int main() {
// Initialize the linked list
struct Node* START = NULL;
struct Node* PTR;
// Create nodes and add them to the linked list
for (int i = 1; i <= 5; i++) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->INFO = i;
newNode->LINK = NULL;
if (START == NULL) {
START = newNode;
} else {
PTR = START;
while (PTR->LINK != NULL) {
PTR = PTR->LINK;
}
PTR->LINK = newNode;
}
}
// Process each node in the linked list
PTR = START;
while (PTR != NULL) {
processNode(PTR->INFO);
PTR = PTR->LINK;
}
// Free memory by deallocating the linked list
PTR = START;
while (PTR != NULL) {
struct Node* temp = PTR;
PTR = PTR->LINK;
free(temp);
}
return 0;
}

Output:

Create()
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* first = NULL;
struct Node* last = NULL;
int N;
printf("Enter the number of nodes (N): ");
scanf("%d", &N);
// Create the first node
first = (struct Node*)malloc(sizeof(struct Node));
if (first == NULL) {
printf("Memory allocation failed.");
return 1;
}
printf("Enter data for the first node: ");
scanf("%d", &(first->data));
first->next = NULL;
last = first; // Point 'last' to the first node
// Create and connect the rest of the nodes
for (int i = 1; i < N; i++) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.");
return 1;
}
printf("Enter data for node %d: ", i + 1);
scanf("%d", &(newNode->data));
newNode->next = NULL;
last->next = newNode; // Connect the last node to the new node
last = newNode; // Update 'last' to the new node
}
// Print the linked list
struct Node* current = first;
printf("Linked List: ");
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
// Free allocated memory
current = first;
while (current != NULL) {
struct Node* temp = current;
current = current->next;
free(temp);
}
return 0;
}

Output:

Traverse()
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* first = NULL;
int count = 0;
// Create and populate the linked list
for (int i = 1; i <= 5; i++) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = i;
newNode->next = first;
first = newNode;
}
// Traverse the linked list and count the nodes
struct Node* ptr = first;
while (ptr != NULL) {
count++;
ptr = ptr->next;
}
if (first == NULL) {
printf("List is empty.\n");
} else {
printf("Number of nodes = %d\n", count);
}
// Free allocated memory
ptr = first;
while (ptr != NULL) {
struct Node* temp = ptr;
ptr = ptr->next;
free(temp);
}
return 0;
}

Output:

Search()

#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node* next;
};
int main() {
struct Node* first = NULL;
// Create and populate the linked list
for (char c = 'A'; c <= 'Z'; ++) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = c;
newNode->next = first;
first = newNode;
}
char searchItem = 'X';
struct Node* ptr = first;
int itemFound = 0;
while (ptr != NULL) {
if (ptr->data == searchItem) {
itemFound = 1;
printf("Item found\n");
break;
}
ptr = ptr->next;
}
if (!itemFound) {
printf("Item not found\n");
}
// Free allocated memory
ptr = first;
while (ptr != NULL) {
struct Node* temp = ptr;
ptr = ptr->next;
free(temp);
}
return 0;
}

Output:

Insert_first()

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
int main() {
struct Node* first = NULL;
struct Node* X = (struct Node*)malloc(sizeof(struct Node));
if (X == NULL) {
printf("Memory allocation failed.");
return 1;
}
printf("Enter data for the new node: ");
scanf("%d", &(X->data));
X->next = NULL;
if (first == NULL) {
first = X;
} else {
X->next = first;
first = X;
}
// Print the linked list
struct Node* current = first;
printf("Linked List: ");
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
// Free allocated memory
current = first;
while (current != NULL) {
struct Node* temp = current;
current = current->next;
free(temp);
}
return 0;
}

Output:

Insert_afternode()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char data[50];
struct Node* next;
};
int main() {
struct Node* List = NULL;
struct Node* ptr;
// Create and populate the linked list
for (int i = 1; i <= 5; i++) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
sprintf(newNode->data, "Item%d", i);
newNode->next = List;
List = newNode;
}
char searchValue[50];
printf("Enter a value to search for: ");
scanf("%s", searchValue);
ptr = List;
while (ptr != NULL) {
if (strcmp(ptr->data, searchValue) == 0) {
struct Node* X = (struct Node*)malloc(sizeof(struct Node));
printf("Enter data for the new node: ");
scanf("%s", X->data);
X->next = ptr->next;
ptr->next = X;
break;
}
ptr = ptr->next;
}
// Print the linked list
ptr = List;
printf("Linked List: ");
while (ptr != NULL) {
printf("%s -> ", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
// Free allocated memory
ptr = List;
while (ptr != NULL) {
struct Node* temp = ptr;
ptr = ptr->next;
free(temp);
}
return 0;
}

Output:
Insert_beforenode()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char data[50];
struct Node* next;};
int main() {
struct Node* LIST = NULL;
struct Node* ptr;
struct Node* back;
// Create and populate the linked list
for (int i = 1; i <= 5; i++) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
sprintf(newNode->data, "Item%d", i);
newNode->next = LIST;
LIST = newNode;
}
char searchValue[50];
printf("Enter a value to search for: ");
scanf("%s", searchValue);
ptr = LIST;
back = NULL;
while (ptr != NULL) {
if (strcmp(ptr->data, searchValue) == 0) {
struct Node* X = (struct Node*)malloc(sizeof(struct Node));
printf("Enter data for the new node: ");
scanf("%s", X->data);
X->next = LIST;
LIST = X;
break;
}
back = ptr;
ptr = ptr->next;
}
// Print the linked list
ptr = LIST;
printf("Linked List: ");
while (ptr != NULL) {
printf("%s -> ", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
// Free allocated memory
ptr = LIST;
while (ptr != NULL) {
struct Node* temp = ptr;
ptr = ptr->next;
free(temp);
}
return 0;
}
Output:

Delete()

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
char data[50];
struct Node* next;
};
int main() {
struct Node* LIST = NULL;
struct Node* ptr;
struct Node* back;
// Create and popuate the linked list
for (int i = 1; i <= 5; i++) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
sprintf(newNode->data, "Item%d", i);
newNode->next = LIST;
LIST = newNode;
}
char searchValue[50];
printf("Enter a value to search for: ");
scanf("%s", searchValue);
ptr = LIST;
back = NULL;
if (strcmp(LIST->data, searchValue) == 0) {
ptr = LIST;
LIST = LIST->next;
free(ptr);
printf("Deleted the first node with the desired value.\n");
} else {
while (ptr != NULL) {
if (strcmp(ptr->data, searchValue) == 0) {
back->next = ptr->next;
free(ptr);
printf("Deleted a node with the desired value.\n");
break;
}
back = ptr;
ptr = ptr->next;
}
}

// Print the linked list


ptr = LIST;
printf("Linked List: ");
while (ptr != NULL) {
printf("%s -> ", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
// Free allocated memory
ptr = LIST;
while (ptr != NULL) {
struct Node* temp = ptr;
ptr = ptr->next;
free(temp);
}
return 0;
}

Output:
EXPERIMENT NO. - 7

Aim of the experiment :-

Course Outcome :-

Date of Conduction : Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT NO : 7
AIM: Implementation of binary search tree.

ALGORITHM:
Insert()
1. If node == NULL
return createNode(data)
2. if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
3. return node;

Inorder_traversal()
1. inorder(root->left)
2. display(root->data)
3. inorder(root->right)

Preorder_traversal()
1. display(root->data)
2. inorder(root->left)
3. inorder(root->right)

Postorder_traversal()
1. inorder(root->left)
2. inorder(root->right)
3. display(root->data)
Insert()
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a binary tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to insert a node into the binary search tree
struct Node* insert(struct Node* node, int data) {
if (node == NULL) {
return createNode(data);
}
if (data < node->data) {
node->left = insert(node->left, data);
} else if (data > node->data) {
node->right = insert(node->right, data);
}
return node;
}
// Function to traverse the binary search tree in-order
void inOrderTraversal(struct Node* node) {
if (node == NULL) {
return;
}
inOrderTraversal(node->left);
printf("%d -> ", node->data);
inOrderTraversal(node->right);
}
int main() {
struct Node* root = NULL;
// Insert nodes into the binary search tree
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 70);
root = insert(root, 20);
root = insert(root, 40);
// Perform in-order traversal to display the tree
printf("In-order traversal: ");
inOrderTraversal(root);
printf("\n");
// Free allocated memory (optional)
// This should be done after you're done with the tree
return 0;
}
Output:

Inorder_traversal()
#include <stdio.h>
#include <stdlib.h>

// Define a structure for a binary tree node


struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

// Function to perform in-order traversal


void inOrderTraversal(struct Node* root) {
if (root == NULL) {
return;
}

// Traverse the left subtree


inOrderTraversal(root->left);

// Display the data of the current node


printf("%d -> ", root->data);

// Traverse the right subtree


inOrderTraversal(root->right);
}

int main() {
// Create a binary tree
struct Node* root = createNode(50);
root->left = createNode(30);
root->right = createNode(70);
root->left->left = createNode(20);
root->left->right = createNode(40);

// Perform in-order traversal to display the tree


printf("In-order traversal: ");
inOrderTraversal(root);
printf("\n");

// Free allocated memory (optional)


// This should be done after you're done with the tree
// and you want to deallocate the nodes.

return 0;
}

Output:

Preorder_traversal()
#include <stdio.h>
#include <stdlib.h>
// Define a structure for a binary tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to perform pre-order traversal
void preOrderTraversal(struct Node* root) {
if (root == NULL) {
return;
}
// Display the data of the current node
printf("%d -> ", root->data);
// Traverse the left subtree
preOrderTraversal(root->eft);
// Traverse the right subtree
preOrderTraversal(root->right);
}
int main() {
// Create a binary tree
struct Node* root = createNode(50);
root->left = createNode(30);
root->right = createNode(70);
root->left->left = createNode(20);
root->left->right = createNode(40);
// Perform pre-order traversal to display the tree
printf("Pre-order traversal: ");
preOrderTraversal(root);
printf("\n");
// This should be done after you're done with the tree
// and you want to deallocate the nodes.

return 0;
}
Output:

Postorder_traversal()

#include <stdio.h>
#include <stdlib.h>
// Define a structure for a binary tree node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to perform post-order traversal
void postOrderTraversal(struct Node* root) {
if (root == NULL) {
return;
}
// Traverse the left subtree
postOrderTraversal(root->left);
// Traverse the right subtree
postOrderTraversal(root->right);
// Display the data of the current node
printf("%d -> ", root->data);
}

int main() {
// Create a binary tree
struct Node* root = createNode(50);
root->left = createNode(30);
root->right = createNode(70);
root->left->left = createNode(20);
root->left->right = createNode(40);
// Perform post-order traversal to display the tree
printf("Post-order traversal: ");
postOrderTraversal(root);
printf("\n");
// Free allocated memory (optional)
// This should be done after you're done with the tree
// and you want to deallocate the nodes.
return 0;
}
Output:
EXPERIMENT NO. - 08

Aim of the experiment :-

Course Outcome :-

Date of Conduction : Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT NO : 8

AIM: Construction of expression tree using postfix expression.

ALGORITHM:

1. Read the expression one symbol at a time.


2. If the symbol is an operand, create a one node tree and push a pointer to it into stack.
3. If symbol is an operator, pop pointer to two trees T2 and T1 from the stack and form
a new tree whose left and right children point to T1 and T2 respectively.
4. A pointer to this new tree is then pushed onto the stack.

Conclusion

Laboutcomes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// Define a structure for the tree node
struct TreeNode {
char value;
struct TreeNode* left;
struct TreeNode* right;
};
// Function to create a new tree node
struct TreeNode* createNode(char value) {
struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
// Function to check if a character is an operator
bool isOperator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}
// Function to construct an expression tree from a postfix expression
struct TreeNode* constructExpressionTree(char postfix[]) {
struct TreeNode* stack[100]; // Stack to hold tree nodes
int top = -1; // Initialize the stack top
for (int i = 0; postfix[i]; i++) {
struct TreeNode* newNode = createNode(postfix[i]);
if (!isOperator(postfix[i]))
stack[++top] = newNode; // Operand, push it to the stack
} else {
// Operator, pop two nodes from the stack
struct TreeNode* right = stack[top--];
struct TreeNode* left = stack[top--];
// Create a new node with the operator and attach left and right as children
newNode->left = left;
newNode->right = right;
// Push the new node back to the stack
stack[++top] = newNode;
}
}
// The final node in the stack is the root of the expression tree
return stack[0];
}
// Function to print the infix expression
void printInfix(struct TreeNode* root) {
if (root) {
if (isOperator(root->value))
printf("(");
printInfix(root->left);
printf("%c", root->value);
printInfix(root->right);
if (isOperator(root->value))
printf(")");
}
}

int main() {
char postfix[] = "ab+c-";
struct TreeNode* root = constructExpressionTree(postfix);

printf("Infix Expression: ");


printInfix(root);
printf("\n");

return 0;
}

Output:
EXPERIMENT NO. - 09

Aim of the experiment :-

Course Outcome :-

Date of Conduction : Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT NO:9
AIM: Implementation of Quick sort, Merge sort, Heap sort.

ALGORITHM:

Quick Sort:
1. Choose the highest index value has pivot.
2. Take two variables to point left and right of the list excluding pivot.
3. left points to the low index.
4. Right points to the high.
5. while value at left is less than pivot move right.
6. while value at right is greater than pivot move left.
7. if both step 5 and step 6 does not match swap left and right.
8. if left ≥ right, the point where they met is new pivot.

Merge Sort:
1. procedure mergesort( var a as array )
2. if ( n == 1 ) return a
3. var l1 as array = a[0] ... a[n/2]
4. var l2 as array = a[n/2+1] ... a[n]
5. l1 = mergesort( l1 )
6. l2 = mergesort( l2 )
7. return merge( l1, l2 )
8. end procedure
9. procedure merge( var a as array, var b as array )
10. var c as array
11. while ( a and b have elements
) 12. if ( a[0] > b[0] )
add b[0] to the end of c
remove b[0] from b
13. else
add a[0] to the end of c
remove a[0] from a
14. end if
15. end while

16. while ( a has elements )


add a[0] to the end of c
remove a[0] from a
17. end while
18. while ( b has elements )
add b[0] to the end of c
remove b[0] from b
19. end while
20. return c
21. end procedure

Heap sort:
a. MaxHeapify(A, i)
b. l = left(i)
c. r = right(i)
d. if l <= heap-size[A] and A[l] >
A[i] then largest = l
else largest = i
e. if r <= heap-size[A] and A[r] > A[largest]
then largest = r
8. if largest != i
then swap A[i] with A[largest]
MaxHeapify(A, largest)
9. end func
10. BuildMaxHeap(A)
11. heap-size[A] = length[A]
12. for i = |length[A]/2| downto 1
do MaxHeapify(A, i)
13. end func

14. HeapSort(A)
15. BuildMaxHeap(A)
16. for i = length[A] downto 2
do swap A[1] with
A[i]
heap-size[A] = heap-size[A] – 1
MaxHeapify(A, 1)
17. end func

Conclusion:

Lab Outcomes:
Quick Sort

#include <stdio.h>
// Function to swap two elements in an array
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to partition the array and return the pivot index
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choose the highest index value as the pivot
int left = low - 1;
int right = high;
while (1) {
while (arr[++left] < pivot); // Move left to the right while value at left is less than the pivot
while (arr[--right] > pivot); // Move right to the left while value at right is greater than the
pivot
if (left >= right) {
break;
} else {
swap(&arr[left], &arr[right]); // Swap values at left and right
}
}
swap(&arr[left], &arr[high]); // Swap the pivot with the element at the left
return left;
}
// Function to perform Quick Sort
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1); // Sort the elements before the pivot
quickSort(arr, pivotIndex + 1, high); // Sort the elements after the pivot
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
quickSort(arr, 0, n - 1);
printf("Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:

Merge Sort

#include <stdio.h>
#include <stdlib.h>
// Function to merge two sorted arrays
void merge(int arr[], int left[], int right[], int leftSize, int rightSize) {
int i = 0, j = 0, k = 0;
while (i < leftSize && j < rightSize) {
if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
while (i < leftSize) {
arr[k++] = left[i++];
}
while (j < rightSize) {
arr[k++] = right[j++];
}
}
// Function to perform Merge Sort
void mergeSort(int arr[], int n) {
if (n > 1) {
int mid = n / 2;
int* left = (int*)malloc(mid * sizeof(int));
int* right = (int*)malloc((n - mid) * sizeof(int));
for (int i = 0; i < mid; i++) {
left[i] = arr[i];
}
for (int i = mid; i < n; i++) {
right[i - mid] = arr[i];
}
mergeSort(left, mid);
mergeSort(right, n - mid);
merge(arr, left, right, mid, n - mid);
free(left);
free(right);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
mergeSort(arr, n);
printf("Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:

#include <stdio.h>
#include <stdlib.h>
// Function to merge two sorted arrays
void merge(int arr[], int left[], int right[], int leftSize, int rightSize) {
int i = 0, j = 0, k = 0;
while (i < leftSize && j < rightSize) {
if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
while (i < leftSize) {
arr[k++] = left[i++];
}
while (j < rightSize) {
arr[k++] = right[j++];
}
}
// Function to perform Merge Sort
void mergeSort(int arr[], int n) {
if (n > 1) {
int mid = n / 2;
int* left = (int*)malloc(mid * sizeof(int));
int* right = (int*)malloc((n - mid) * sizeof(int));
for (int i = 0; i < mid; i++) {
left[i] = arr[i];
}
for (int i = mid; i < n; i++) {
right[i - mid] = arr[i];
}
mergeSort(left, mid);
mergeSort(right, n - mid);
merge(arr, left, right, mid, n - mid);
free(left);
free(right);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
mergeSort(arr, n);
printf("Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:

Heap Sort

#include <stdio.h>
// Function to swap two elements in an array
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to maintain the max-heap property
void MaxHeapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
MaxHeapify(arr, n, largest);
}
}
// Function to build a max heap from an array
void BuildMaxHeap(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
MaxHeapify(arr, n, i);
}
}

// Function to perform Heap Sort


void HeapSort(int arr[], int n) {
BuildMaxHeap(arr, n);
for (int i = n - 1; i > 0; i--) {
swap(&arr[0], &arr[i]);
MaxHeapify(arr, i, 0);
}
}
int main() {
int arr[] = {4, 10, 3, 5, 1};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
HeapSort(arr, n);
printf("Sorted Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Output:
EXPERIMENT NO. - 10

Aim of the experiment :-

Course Outcome :-

Date of Conduction : Date of Submission :

Punctuality &
Implementation Understanding (5) Discipline (5) Total
(5) (15)

Practical Incharge
EXPERIMENT 10

AIM: Study and/or implementation of hash tables with separate chaining (open hashing)
and/or Linear probing (open addressing or closed hashing)

ALGORITHM:

Assumption:
Hash function will return an integer from 0 to 19.

vector <string> hashTable[20];


int hashTableSize=20;

Insert()

void insert(string s)
{
// Compute the index using Hash
Function int index = hashFunc(s);
// Insert the element in the linked list at the particular index
hashTable[index].push_back(s);
}

Search()

void search(string s)
{
//Compute the index by using the hash
function int index = hashFunc(s);
//Search the linked list at that specific index
for(int i = 0;i < hashTable[index].size();i++)
{
if(hashTable[index][i] == s)
{
cout << s << " is found!" <<
endl; return;
}
}
cout << s << " is not found!" << endl;
}

Insert()
void insert(string s)
{
// Compute the index using Hash
Function int index = hashFunc(s);
// Insert the element in the linked list at the particular index
hashTable[index].push_back(s);
}
Search()
void search(string s)
{
//Compute the index by using the hash
function int index = hashFunc(s);
//Search the linked list at that specific index
for(int i = 0;i < hashTable[index].size();i++)
{
if(hashTable[index][i] == s)
{
cout << s << " is found!" <<
endl; return;
}
}
cout << s << " is not found!" << endl;
}

Conclusion:

Lab Outcomes:
Insert()

#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> hashTable[20]; // Hash table with 20 buckets
// Hash Function that returns an integer from 0 to 19
int hashFunc(string s) {
int hash = 0;
for (char c : s) {
hash += c;
}
return hash % 20; // Ensure the result is between 0 and 19
}
// Function to insert a string into the hash table
void insert(string s) {
int index = hashFunc(s);
hashTable[index].push_back(s);
}
// Function to search for a string in the hash table
bool search(string s) {
int index = hashFunc(s);
for (const string& str : hashTable[index]) {
if (str == s) {
return true;
}
}
return false;
}
int main() {
insert("apple");
insert("banana");
insert("cherry");
string searchStr = "banana";
if (search(searchStr)) {
cout << searchStr << " found in the hash table." << endl;
} else {
cout << searchStr << " not found in the hash table." << endl;
}
searchStr = "grape";
if (search(searchStr)) {
cout << searchStr << " found in the hash table." << endl;
} else {
cout << searchStr << " not found in the hash table." << endl;
}
return 0;
}
Output:

Search()
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> hashTable[20]; // Hash table with 20 buckets
int hashFunc(string s) {
int hash = 0;
for (char c : s) {
hash += c;
}
return hash % 20;
}
void insert(string s) {
int index = hashFunc(s);
hashTable[index].push_back(s);
}
void search(string s) {
int index = hashFunc(s);
for (int i = 0; i < hashTable[index].size(); i++) {
if (hashTable[index][i] == s) {
cout << s << " is found!" << endl;
return;
}
}
cout << s << " is not found!" << endl;
}
int main() {
insert("apple");
insert("banana");
insert("cherry");
string searchStr = "banana";
search(searchStr);
searchStr = "grape";
search(searchStr);
return 0;
}

Output:

Insert()
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> hashTable[20]; // Hash table with 20 buckets
// Hash Function that returns an integer from 0 to 19
int hashFunc(string s) {
int hash = 0;
for (char c : s) {
hash += c;
}
return hash % 20;
}
// Function to insert a string into the hash table
void insert(string s) {
int index = hashFunc(s);
hashTable[index].push_back(s);
cout << s << " inserted at index " << index << endl;
}
int main() {
insert("apple");
insert("banana");
insert("cherry");
insert("date");
insert("grape");
return 0;
}

Output:

Search()
#include <iostream>
#include <vector>
#include <string>
using namespace std;
vector<string> hashTable[20]; // Hash table with 20 bucket
// Hash Function that returns an integer from 0 to 19
int hashFunc(string s) {
int hash = 0;
for (char c : s) {
hash += c;
}
return hash % 20;
}
// Function to search for a string in the hash table
void search(string s) {
int index = hashFunc(s);
for (int i = 0; i < hashTable[index].size(); i++) {
if (hashTable[index][i] == s) {
cout << s << " is found at index " << index << " and position " << i << endl;
return;
}
}
cout << s << " is not found!" << endl;
}
int main() {
hashTable[0].push_back("apple");
hashTable[1].push_back("banana");
hashTable[2].push_back("cherry");
hashTable[2].push_back("date");
hashTable[3].push_back("grape");
search("banana");
search("mango");
return 0;
}

Output:

You might also like