Data Structures EEE Lab Cycle Programs
Data Structures EEE Lab Cycle Programs
Pre-requisite:
Course Objectives:
To provide the knowledge of basic data structures and their implementations.
To understand importance of data structures in context of writing efficient
programs.
To develop skills to apply appropriate data structures in problem solving.
UNIT I
Introduction to Data Structures: Definition and importance of Data structures,
Abstract data types (ADTs) and its specifications Arrays: Introduction, 1-D, 2-D
Arrays, accessing elements of array, Row Major and Column Major storage of
Arrays, Searching Techniques: Linear & Binary Search, Sorting Techniques:
Bubble sort, Selection sort, Quick sort.
UNIT II
Linked Lists: Singly linked lists: representation and operations, doubly linked lists
and circular linked lists, Comparing arrays and linked lists, Applications of linked
lists.
UNIT III
1
Stacks: Introduction to stacks: properties and operations, implementing stacks
using arrays and linked lists, Applications of stacks in expression evaluation,
backtracking, reversing list etc.
UNIT IV
Queues: Introduction to queues: properties and operations, Circular queues,
implementing queues using arrays and linked lists, Applications of queues
scheduling, etc.
Deques: Introduction to deques (double-ended queues), Operations on deques
and their applications.
UNIT V
Trees: Introduction to Trees, Binary trees and traversals, Binary Search Tree–
Insertion, Deletion & Traversal
int main() {
int arr[100], n, i, min, max;
return 0;
Output:
Enter number of elements: 5
Enter array elements:
34
56
8
2
34
Minimum = 2
Maximum = 56
int main() {
int a[10][10], b[10][10], c[10][10];
int i, j, k, r1, c1, r2, c2;
if(c1 != r2) {
printf("Multiplication not possible.\n");
return 0;
}
4
printf("Enter first matrix:\n");
for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Result matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
Output:
Enter rows and columns of first matrix: 2
2
Enter rows and columns of second matrix: 2
5
2
Enter first matrix:
1
1
1
1
Enter second matrix:
1
2
3
4
Result matrix:
46
46
int main() {
int arr[100], n, key, i, low, high, mid;
if(arr[mid] == key) {
printf("Element found at position %d\n", mid + 1);
return 0;
}
else if(arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
int main() {
int arr[100], n, i, j, min, temp;
printf("Enter elements:\n");
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Sorted array:\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
8
Enter number of elements: 5
Enter elements:
7
5
0
10
1
Sorted array:
0 1 5 7 10
// Swap function
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// QuickSort function
void quickSort(int arr[], int lb, int ub) {
if (lb < ub) {
int loc = partition(arr, lb, ub); // loc is pivot index
quickSort(arr, lb, loc - 1);
quickSort(arr, loc + 1, ub);
}
}
// Print array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Main function
int main() {
int arr[] = {7, 2, 1, 6, 8, 5, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array:\n");
printArray(arr, n);
10
quickSort(arr, 0, n - 1);
printf("Sorted array:\n");
printArray(arr, n);
return 0;
}
Output:
Original array:
72168534
Sorted array:
12345678
struct Node {
int data;
struct Node* next;
};
// Insert at end
void insert(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
11
if(head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
// Delete from front
void deleteFront() {
if(head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
head = head->next;
free(temp);
printf("First node deleted.\n");
}
// Traverse
void display() {
struct Node* temp = head;
if(temp == NULL) {
printf("List is empty.\n");
return;
}
printf("List elements: ");
while(temp != NULL) {
printf("%d ", temp->data);
12
temp = temp->next;
}
printf("\n");
}
int main() {
int choice, val;
while(1) {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &val);
insert(val);
break;
case 2:
deleteFront();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output:
13
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter value to insert: 10
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter value to insert: 20
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter value to insert: 50
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 1
Enter value to insert: 32
1. Insert
2. Delete
3. Display
4. Exit
14
Enter choice: 3
List elements: 10 20 50 32
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 2
First node deleted.
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 2
First node deleted.
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 20
Invalid choice!
1. Insert
2. Delete
3. Display
4. Exit
Enter choice: 3
List elements: 50 32
1. Insert
2. Delete
15
3. Display
4. Exit
Enter choice: 4
// Insert at end
void insert(int roll, char name[], float marks) {
16
struct Student* newNode = createNode(roll, name, marks);
if(head == NULL) {
head = newNode;
head->next = head;
head->prev = head;
} else {
struct Student* last = head->prev;
last->next = newNode;
newNode->prev = last;
newNode->next = head;
head->prev = newNode;
}
}
do {
printf("Roll No: %d\n", temp->roll);
printf("Name : %s\n", temp->name);
printf("Marks : %.2f\n", temp->marks);
printf("-------------------\n");
temp = temp->next;
17
} while(temp != head);
}
int main() {
int choice, roll;
float marks;
char name[50];
while(1) {
printf("\n1. Insert Student\n2. Display All\n3. Exit\nEnter choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter Roll No: ");
scanf("%d", &roll);
printf("Enter Name: ");
scanf(" %[^\n]", name); // To read full name with spaces
printf("Enter Marks: ");
scanf("%f", &marks);
insert(roll, name, marks);
break;
case 2:
display();
break;
case 3:
return 0;
default:
printf("Invalid choice!\n");
}
}
}
Output:
18
1. Insert Student
2. Display All
3. Exit
Enter choice: 1
Enter Roll No: 1
Enter Name: Alice
Enter Marks: 80
1. Insert Student
2. Display All
3. Exit
Enter choice: 1
Enter Roll No: 2
Enter Name: Bob
Enter Marks: 76
1. Insert Student
2. Display All
3. Exit
Enter choice: 1
Enter Roll No: 3
Enter Name: Risy
Enter Marks: 89
1. Insert Student
2. Display All
3. Exit
Enter choice: 2
Student Records:
Roll No: 1
Name : Alice
19
Marks : 80.00
-------------------
Roll No: 2
Name : Bob
Marks : 76.00
-------------------
Roll No: 3
Name : Risy
Marks : 89.00
-------------------
1. Insert Student
2. Display All
3. Exit
Enter choice: 3
int main()
{
// Create the first polynomial: 3x^3 + 5x^2 + 6x + 2
struct Node* poly1 = createNode(3, 3);
poly1->next = createNode(5, 2);
poly1->next->next = createNode(6, 1);
poly1->next->next->next = createNode(2, 0);
if (isEmpty())
printf("Stack is empty now.\n");
else
printf("Stack is not empty.\n");
return 0;
}
Output:
10 pushed to stack
20 pushed to stack
30 pushed to stack
Top element is 30
Popped element is 30
25
Popped element is 20
Stack is not empty.
Stack Implementation Using Linked List
#include <stdio.h>
#include <stdlib.h>
return 0;
}
Output:
10 pushed to stack
20 pushed to stack
30 pushed to stack
Top element is 30
Popped element is 30
Popped element is 20
Stack is not empty.
9. Convert given infix expression into post fix expression using stacks.
Program:
#include <stdio.h>
#include <ctype.h>
int pop()
{
return stack[top--];
}
int precedence(char c)
{
return (c == '+' || c == '-') ? 1 : (c == '*' || c == '/') ? 2 : 0;
}
int main()
{
char infix[MAX], postfix[MAX];
printf("Enter infix: ");
30
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix: %s\n", postfix);
printf("Result: %d\n", evaluatePostfix(postfix));
return 0;
}
Output:
Enter infix: ((3+4)/(5-2))
Postfix: 34+52-/
Result: 2
// Node structure
struct Node {
int data;
struct Node* next;
};
// Stack operations
void push(struct StackNode** top, struct Node* node) {
31
struct StackNode* newStackNode = (struct
StackNode*)malloc(sizeof(struct StackNode));
newStackNode->listNode = node;
newStackNode->next = *top;
*top = newStackNode;
}
head = pop(&stack);
temp = head;
return head;
}
// Main function
int main() {
// Creating a sample linked list: 10 -> 20 -> 30 -> 40
33
struct Node* head = createNode(10);
head->next = createNode(20);
head->next->next = createNode(30);
head->next->next->next = createNode(40);
printf("Original List:\n");
printList(head);
head = reverseUsingStack(head);
printf("Reversed List:\n");
printList(head);
return 0;
}
Output:
Original List:
10 20 30 40
Reversed List:
40 30 20 10
// Node structure
struct Node
{
int data;
struct Node* next;
};
// Queue front and rear pointers
struct Node* front = NULL;
struct Node* rear = NULL;
if (isEmpty())
{
front = rear = newNode;
}
else
{
rear->next = newNode;
rear = newNode;
}
printf("Enqueued: %d\n", value);
}
if (front == NULL)
{ // If queue becomes empty, reset rear
rear = NULL;
}
39
}
dequeue();
dequeue();
display(); // Display remaining queue contents
return 0;
}
Output:
Enqueued: 10
Enqueued: 20
Enqueued: 30
Enqueued: 40
Enqueued: 50
10 20 30 40 50
Output:Dequeued: 10
Dequeued: 20
30 40 50
Enqueued: 60
30 40 50 60
Front item: 30
Enqueued: 70
44
30 40 50 60 70
// Enqueue operation
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
if (front == NULL) {
front = rear = newNode;
rear->next = front; // Circular link
} else {
rear->next = newNode;
rear = newNode;
rear->next = front;
}
printf("\nEnqueued: %d", value);
}
45
// Dequeue operation
void dequeue() {
if (front == NULL) {
printf("\nQueue is empty. Cannot dequeue.\n");
return;
}
if (front == rear) {
printf("\nDequeued: %d", front->data);
free(front);
front = rear = NULL;
} else {
struct Node* temp = front;
printf("\nDequeued: %d", temp->data);
front = front->next;
rear->next = front;
free(temp);
}
}
// Peek operation
void peek() {
if (front == NULL) {
printf("\nQueue is empty.");
} else {
printf("\nFront item: %d", front->data);
}
}
// Display operation
void display() {
if (front == NULL) {
46
printf("\nQueue is empty.");
return;
}
display();
dequeue();
dequeue();
display();
enqueue(60);
display();
peek();
47
enqueue(70);
display();
return 0;
}
Output:
Enqueued: 10
Enqueued: 20
Enqueued: 30
Enqueued: 40
Enqueued: 50
Queue contents: 10 20 30 40 50
Dequeued: 10
Dequeued: 20
Queue contents: 30 40 50
Enqueued: 60
Queue contents: 30 40 50 60
Front item: 30
Enqueued: 70
Queue contents: 30 40 50 60 70
// Insert at front
void insertFront(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->prev = NULL;
newNode->next = front;
if (front == NULL) {
front = rear = newNode;
} else {
front->prev = newNode;
front = newNode;
}
printf("\nInserted at front: %d", value);
}
// Insert at rear
void insertRear(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
newNode->prev = rear;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
49
}
printf("\nInserted at rear: %d", value);
}
50
if (front == rear) {
front = rear = NULL;
} else {
rear = rear->prev;
rear->next = NULL;
}
free(temp);
}
// Display Deque
void display() {
if (front == NULL) {
printf("\nDeque is empty.");
return;
}
// Main function
int main() {
insertRear(10);
insertRear(20);
insertFront(5);
insertFront(2);
display();
51
deleteFront();
deleteRear();
display();
insertRear(30);
insertFront(1);
display();
return 0;
}
Output:
Inserted at rear: 10
Inserted at rear: 20
Inserted at front: 5
Inserted at front: 2
Deque contents: 2 5 10 20
Deleted from front: 2
Deleted from rear: 20
Deque contents: 5 10
Inserted at rear: 30
Inserted at front: 1
Deque contents: 1 5 10 30
// Main function
int main() {
/*
1
/\
2 3
/\
4 5
*/
return 0;
}
54
Output:
Inorder Traversal: 4 2 5 1 3
Preorder Traversal: 1 2 4 5 3
Postorder Traversal: 4 5 2 3 1
16. Write program to create binary search tree for given list of
integers. Perform traversal of the tree. Implement insertion and
deletion operations
Program:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* left;
struct Node* right;
};
55
if (value < root->data)
root->left = insert(root->left, value);
else if (value > root->data)
root->right = insert(root->right, value);
return root;
}
// Creating BST
for (int i = 0; i < n; i++) {
root = insert(root, values[i]);
}
printf("\n\nInserting 25...");
root = insert(root, 25);
printf("\nInorder after insertion: ");
inorder(root);
printf("\n\nDeleting 70...");
root = deleteNode(root, 70);
printf("\nInorder after deletion: ");
inorder(root);
58
return 0;
}
Output:
Inorder Traversal: 20 30 40 50 60 70 80
Preorder Traversal: 50 30 20 40 70 60 80
Postorder Traversal: 20 40 30 60 80 70 50
Inserting 25...
Inorder after insertion: 20 25 30 40 50 60 70 80
Deleting 70...
Inorder after deletion: 20 25 30 40 50 60 80
59