Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views92 pages

Data Structures Full Experiments

The document outlines a series of data structure lab experiments, including implementations of various search algorithms (binary and linear), sorting algorithms (selection, merge, quick, and insertion), and data structures (stacks, queues, linked lists, binary search trees, and graphs). It provides Python and C code examples for each experiment, demonstrating how to implement these algorithms and data structures. Additionally, it covers infix to postfix conversion and postfix expression evaluation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views92 pages

Data Structures Full Experiments

The document outlines a series of data structure lab experiments, including implementations of various search algorithms (binary and linear), sorting algorithms (selection, merge, quick, and insertion), and data structures (stacks, queues, linked lists, binary search trees, and graphs). It provides Python and C code examples for each experiment, demonstrating how to implement these algorithms and data structures. Additionally, it covers infix to postfix conversion and postfix expression evaluation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 92

Data Structures Lab Experiments

1. Implementation of Binary Search and Linear Search.

2. Implementation of Selection, Merge, Quick, and Insertion Sort.

3. Implementation of Stacks and Queues using Arrays.

4. Implementation of Infix to Postfix Conversion, Postfix Expression Evaluation.

5. Implementation of Circular Queue using Arrays.

6. Implementation of Singly Linked List.

7. Implementation of Doubly Linked List.

8. Implementation of Circular Linked List.

9. Implementation of Stacks, Queues using Linked Lists.

10. Implementation of Binary Search Tree (Insertion, Deletion, and Search operations).

11. Implementation of Tree Traversal on Binary Trees.

12. Implementation of AVL Trees.

13. Implementation of Traversal on Graphs.

14. Implementation of Prim’s and Kruskal’s Algorithm

Experiment 1: Implementation of Binary Search and Linear Search

Linear Search

Python:

def linear_search(arr, target):

for i in range(len(arr)):

if arr[i] == target:

return i

return -1
arr = [10, 20, 30, 40, 50]

target = 30

result = linear_search(arr, target)

print("Element found at index:" if result != -1 else "Element not found", result)

C:

#include <stdio.h>

int linear_search(int arr[], int size, int target) {

for (int i = 0; i < size; i++) {

if (arr[i] == target)

return i;

return -1;

int main() {

int arr[] = {10, 20, 30, 40, 50};

int target = 30;

int result = linear_search(arr, 5, target);

if (result != -1)

printf("Element found at index: %d\n", result);

else

printf("Element not found\n");

return 0;

}
Binary Search (works on sorted arrays)

Python:

def binary_search(arr, target):

low, high = 0, len(arr) - 1

while low <= high:

mid = (low + high) // 2

if arr[mid] == target:

return mid

elif arr[mid] < target:

low = mid + 1

else:

high = mid - 1

return -1

arr = [10, 20, 30, 40, 50]

target = 40

result = binary_search(arr, target)

print("Element found at index:" if result != -1 else "Element not found", result)

C:

#include <stdio.h>

int binary_search(int arr[], int size, int target) {

int low = 0, high = size - 1;

while (low <= high) {

int mid = (low + high) / 2;

if (arr[mid] == target)
return mid;

else if (arr[mid] < target)

low = mid + 1;

else

high = mid - 1;

return -1;

int main() {

int arr[] = {10, 20, 30, 40, 50};

int target = 40;

int result = binary_search(arr, 5, target);

if (result != -1)

printf("Element found at index: %d\n", result);

else

printf("Element not found\n");

return 0;

✅ Experiment 2: Implementation of Selection, Merge, Quick, and Insertion Sort

1. Selection Sort

Python:

def selection_sort(arr):

for i in range(len(arr)):
min_idx = i

for j in range(i+1, len(arr)):

if arr[j] < arr[min_idx]:

min_idx = j

arr[i], arr[min_idx] = arr[min_idx], arr[i]

return arr

arr = [64, 25, 12, 22, 11]

print("Sorted array:", selection_sort(arr))

C:

#include <stdio.h>

void selection_sort(int arr[], int n) {

for (int i = 0; i < n-1; i++) {

int min_idx = i;

for (int j = i+1; j < n; j++) {

if (arr[j] < arr[min_idx])

min_idx = j;

int temp = arr[i];

arr[i] = arr[min_idx];

arr[min_idx] = temp;

}
int main() {

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr) / sizeof(arr[0]);

selection_sort(arr, n);

printf("Sorted array: ");

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

return 0;

2. Insertion Sort

Python:

def insertion_sort(arr):

for i in range(1, len(arr)):

key = arr[i]

j=i-1

while j >= 0 and arr[j] > key:

arr[j + 1] = arr[j]

j -= 1

arr[j + 1] = key

return arr

arr = [12, 11, 13, 5, 6]

print("Sorted array:", insertion_sort(arr))


C:

#include <stdio.h>

void insertion_sort(int arr[], int n) {

for (int i = 1; i < n; i++) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = key;

int main() {

int arr[] = {12, 11, 13, 5, 6};

int n = sizeof(arr) / sizeof(arr[0]);

insertion_sort(arr, n);

printf("Sorted array: ");

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

return 0;

3. Merge Sort

Python:
def merge_sort(arr):

if len(arr) > 1:

mid = len(arr)//2

L = arr[:mid]

R = arr[mid:]

merge_sort(L)

merge_sort(R)

i=j=k=0

while i < len(L) and j < len(R):

if L[i] < R[j]:

arr[k] = L[i]

i += 1

else:

arr[k] = R[j]

j += 1

k += 1

while i < len(L):

arr[k] = L[i]

i += 1

k += 1

while j < len(R):


arr[k] = R[j]

j += 1

k += 1

arr = [38, 27, 43, 3, 9, 82, 10]

merge_sort(arr)

print("Sorted array:", arr)

C:

#include <stdio.h>

void merge(int arr[], int l, int m, int r) {

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

int L[n1], R[n2];

for (i = 0; i < n1; i++)

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

i = 0; j = 0; k = l;

while (i < n1 && j < n2) {

if (L[i] <= R[j])


arr[k++] = L[i++];

else

arr[k++] = R[j++];

while (i < n1) arr[k++] = L[i++];

while (j < n2) arr[k++] = R[j++];

void merge_sort(int arr[], int l, int r) {

if (l < r) {

int m = l + (r - l) / 2;

merge_sort(arr, l, m);

merge_sort(arr, m + 1, r);

merge(arr, l, m, r);

int main() {

int arr[] = {38, 27, 43, 3, 9, 82, 10};

int n = sizeof(arr) / sizeof(arr[0]);

merge_sort(arr, 0, n - 1);

printf("Sorted array: ");

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

return 0;
}

4. Quick Sort

Python:

def quick_sort(arr):

if len(arr) <= 1:

return arr

pivot = arr[0]

left = [x for x in arr[1:] if x < pivot]

right = [x for x in arr[1:] if x >= pivot]

return quick_sort(left) + [pivot] + quick_sort(right)

arr = [10, 7, 8, 9, 1, 5]

print("Sorted array:", quick_sort(arr))

C:

#include <stdio.h>

void swap(int* a, int* b) {

int t = *a;

*a = *b;

*b = t;

int partition(int arr[], int low, int high) {

int pivot = arr[high];

int i = (low - 1);


for (int j = low; j <= high - 1; j++) {

if (arr[j] < pivot) {

i++;

swap(&arr[i], &arr[j]);

swap(&arr[i + 1], &arr[high]);

return (i + 1);

void quick_sort(int arr[], int low, int high) {

if (low < high) {

int pi = partition(arr, low, high);

quick_sort(arr, low, pi - 1);

quick_sort(arr, pi + 1, high);

int main() {

int arr[] = {10, 7, 8, 9, 1, 5};

int n = sizeof(arr) / sizeof(arr[0]);

quick_sort(arr, 0, n - 1);

printf("Sorted array: ");

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);


return 0;

Experiment 3: Implementation of Stacks and Queues using Arrays

A. Stack using Array

Python:

class Stack:

def __init__(self, size):

self.stack = []

self.size = size

def push(self, item):

if len(self.stack) == self.size:

print("Stack Overflow")

else:

self.stack.append(item)

def pop(self):

if not self.stack:

print("Stack Underflow")

else:

print("Popped:", self.stack.pop())

def display(self):
print("Stack:", self.stack)

# Demo

s = Stack(5)

s.push(10)

s.push(20)

s.push(30)

s.display()

s.pop()

s.display()

C:

#include <stdio.h>

#define SIZE 5

int stack[SIZE];

int top = -1;

void push(int value) {

if (top == SIZE - 1)

printf("Stack Overflow\n");

else

stack[++top] = value;

}
void pop() {

if (top == -1)

printf("Stack Underflow\n");

else

printf("Popped: %d\n", stack[top--]);

void display() {

if (top == -1)

printf("Stack is empty\n");

else {

printf("Stack: ");

for (int i = 0; i <= top; i++)

printf("%d ", stack[i]);

printf("\n");

int main() {

push(10);

push(20);

push(30);

display();

pop();

display();

return 0;
}

B. Queue using Array

Python:

class Queue:

def __init__(self, size):

self.queue = [None]*size

self.front = self.rear = -1

self.size = size

def enqueue(self, item):

if self.rear == self.size - 1:

print("Queue Overflow")

else:

if self.front == -1:

self.front = 0

self.rear += 1

self.queue[self.rear] = item

def dequeue(self):

if self.front == -1 or self.front > self.rear:

print("Queue Underflow")

else:

print("Dequeued:", self.queue[self.front])

self.front += 1
def display(self):

if self.front == -1 or self.front > self.rear:

print("Queue is empty")

else:

print("Queue:", self.queue[self.front:self.rear + 1])

# Demo

q = Queue(5)

q.enqueue(10)

q.enqueue(20)

q.enqueue(30)

q.display()

q.dequeue()

q.display()

C:

#include <stdio.h>

#define SIZE 5

int queue[SIZE];

int front = -1, rear = -1;

void enqueue(int value) {

if (rear == SIZE - 1)

printf("Queue Overflow\n");
else {

if (front == -1)

front = 0;

queue[++rear] = value;

void dequeue() {

if (front == -1 || front > rear)

printf("Queue Underflow\n");

else

printf("Dequeued: %d\n", queue[front++]);

void display() {

if (front == -1 || front > rear)

printf("Queue is empty\n");

else {

printf("Queue: ");

for (int i = front; i <= rear; i++)

printf("%d ", queue[i]);

printf("\n");

int main() {
enqueue(10);

enqueue(20);

enqueue(30);

display();

dequeue();

display();

return 0;

Experiment 4: Infix to Postfix Conversion and Postfix Expression Evaluation

We’ll break this into two parts:

 A. Infix to Postfix Conversion (using stack and precedence rules)

 B. Postfix Evaluation (evaluate the result of postfix expression)

A. Infix to Postfix Conversion

Python:

def precedence(op):

if op == '+' or op == '-':

return 1

if op == '*' or op == '/':

return 2

return 0

def infix_to_postfix(expression):

stack = []

postfix = ""
for char in expression:

if char.isalnum(): # operand

postfix += char

elif char == '(':

stack.append(char)

elif char == ')':

while stack and stack[-1] != '(':

postfix += stack.pop()

stack.pop()

else: # operator

while stack and precedence(char) <= precedence(stack[-1]):

postfix += stack.pop()

stack.append(char)

while stack:

postfix += stack.pop()

return postfix

exp = "a+b*(c-d)"

print("Postfix Expression:", infix_to_postfix(exp))

C:

#include <stdio.h>

#include <ctype.h>

#define SIZE 100

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

void push(char c) {

stack[++top] = c;

char pop() {

return stack[top--];

int precedence(char op) {

if (op == '+' || op == '-') return 1;

if (op == '*' || op == '/') return 2;

return 0;

void infix_to_postfix(char *exp) {

char ch;

for (int i = 0; (ch = exp[i]) != '\0'; i++) {

if (isalnum(ch))

printf("%c", ch);

else if (ch == '(')

push(ch);

else if (ch == ')') {

while (stack[top] != '(')

printf("%c", pop());
pop(); // pop '('

} else {

while (top != -1 && precedence(ch) <= precedence(stack[top]))

printf("%c", pop());

push(ch);

while (top != -1)

printf("%c", pop());

int main() {

char exp[] = "a+b*(c-d)";

printf("Postfix Expression: ");

infix_to_postfix(exp);

return 0;

B. Postfix Expression Evaluation

We'll evaluate only numeric postfix expressions.

Python:

def evaluate_postfix(expression):

stack = []

for ch in expression:

if ch.isdigit():

stack.append(int(ch))
else:

b = stack.pop()

a = stack.pop()

if ch == '+':

stack.append(a + b)

elif ch == '-':

stack.append(a - b)

elif ch == '*':

stack.append(a * b)

elif ch == '/':

stack.append(a // b)

return stack[0]

exp = "231*+9-"

print("Result:", evaluate_postfix(exp)) # (2 + (3 * 1)) - 9 = -4

C:

#include <stdio.h>

#include <ctype.h>

#define SIZE 100

int stack[SIZE];

int top = -1;

void push(int val) {

stack[++top] = val;
}

int pop() {

return stack[top--];

int evaluate_postfix(char *exp) {

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

char ch = exp[i];

if (isdigit(ch)) {

push(ch - '0'); // convert char to int

} else {

int b = pop();

int a = pop();

switch (ch) {

case '+': push(a + b); break;

case '-': push(a - b); break;

case '*': push(a * b); break;

case '/': push(a / b); break;

return pop();

int main() {
char exp[] = "231*+9-"; // (2 + (3 * 1)) - 9 = -4

printf("Result: %d\n", evaluate_postfix(exp));

return 0;

Experiment 5: Implementation of Circular Queue using Arrays

In a circular queue, the last position is connected back to the first position to make a circle.
It uses modulo arithmetic to wrap around.

Circular Queue Using Arrays

Python:

class CircularQueue:

def __init__(self, size):

self.queue = [None]*size

self.max_size = size

self.front = -1

self.rear = -1

def enqueue(self, data):

if (self.rear + 1) % self.max_size == self.front:

print("Queue Overflow")

return

if self.front == -1:

self.front = self.rear = 0
else:

self.rear = (self.rear + 1) % self.max_size

self.queue[self.rear] = data

def dequeue(self):

if self.front == -1:

print("Queue Underflow")

return

print("Dequeued:", self.queue[self.front])

if self.front == self.rear:

self.front = self.rear = -1

else:

self.front = (self.front + 1) % self.max_size

def display(self):

if self.front == -1:

print("Queue is empty")

return

print("Queue:", end=" ")

i = self.front

while True:

print(self.queue[i], end=" ")

if i == self.rear:

break

i = (i + 1) % self.max_size

print()
# Demo

cq = CircularQueue(5)

cq.enqueue(10)

cq.enqueue(20)

cq.enqueue(30)

cq.enqueue(40)

cq.display()

cq.dequeue()

cq.enqueue(50)

cq.enqueue(60) # Should trigger overflow

cq.display()

C:

#include <stdio.h>

#define SIZE 5

int queue[SIZE];

int front = -1, rear = -1;

void enqueue(int value) {

if ((rear + 1) % SIZE == front) {

printf("Queue Overflow\n");

return;

}
if (front == -1)

front = rear = 0;

else

rear = (rear + 1) % SIZE;

queue[rear] = value;

void dequeue() {

if (front == -1) {

printf("Queue Underflow\n");

return;

printf("Dequeued: %d\n", queue[front]);

if (front == rear)

front = rear = -1;

else

front = (front + 1) % SIZE;

void display() {

if (front == -1) {

printf("Queue is empty\n");

return;

printf("Queue: ");

int i = front;
while (1) {

printf("%d ", queue[i]);

if (i == rear)

break;

i = (i + 1) % SIZE;

printf("\n");

int main() {

enqueue(10);

enqueue(20);

enqueue(30);

enqueue(40);

display();

dequeue();

enqueue(50);

enqueue(60); // Should trigger overflow

display();

return 0;

Experiment 6: Implementation of Singly Linked List

A Singly Linked List is a linear data structure where each node contains:

 data
 a pointer to the next node

We'll implement:

 Insertion (at beginning, end)

 Deletion (from beginning, end)

 Display

Singly Linked List

Python:

class Node:

def __init__(self, data):

self.data = data

self.next = None

class SinglyLinkedList:

def __init__(self):

self.head = None

def insert_end(self, data):

new_node = Node(data)

if not self.head:

self.head = new_node

return

temp = self.head

while temp.next:

temp = temp.next

temp.next = new_node
def insert_beginning(self, data):

new_node = Node(data)

new_node.next = self.head

self.head = new_node

def delete_beginning(self):

if not self.head:

print("List is empty")

else:

print("Deleted:", self.head.data)

self.head = self.head.next

def delete_end(self):

if not self.head:

print("List is empty")

elif not self.head.next:

print("Deleted:", self.head.data)

self.head = None

else:

temp = self.head

while temp.next.next:

temp = temp.next

print("Deleted:", temp.next.data)

temp.next = None
def display(self):

temp = self.head

print("List:", end=" ")

while temp:

print(temp.data, end=" -> ")

temp = temp.next

print("None")

# Demo

ll = SinglyLinkedList()

ll.insert_end(10)

ll.insert_end(20)

ll.insert_beginning(5)

ll.display()

ll.delete_beginning()

ll.delete_end()

ll.display()

C:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;


};

struct Node* head = NULL;

void insert_end(int data) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

return;

struct Node* temp = head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

void insert_beginning(int data) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = head;

head = newNode;

void delete_beginning() {
if (head == NULL)

printf("List is empty\n");

else {

struct Node* temp = head;

printf("Deleted: %d\n", temp->data);

head = head->next;

free(temp);

void delete_end() {

if (head == NULL)

printf("List is empty\n");

else if (head->next == NULL) {

printf("Deleted: %d\n", head->data);

free(head);

head = NULL;

} else {

struct Node* temp = head;

while (temp->next->next != NULL)

temp = temp->next;

printf("Deleted: %d\n", temp->next->data);

free(temp->next);

temp->next = NULL;

}
void display() {

struct Node* temp = head;

printf("List: ");

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

int main() {

insert_end(10);

insert_end(20);

insert_beginning(5);

display();

delete_beginning();

delete_end();

display();

return 0;

Experiment 7: Implementation of Doubly Linked List

A Doubly Linked List has nodes containing:

 data
 prev pointer (to the previous node)

 next pointer (to the next node)

We’ll implement:

 Insertion at beginning and end

 Deletion from beginning and end

 Display in forward and reverse

Doubly Linked List

Python:

class Node:

def __init__(self, data):

self.data = data

self.prev = None

self.next = None

class DoublyLinkedList:

def __init__(self):

self.head = None

def insert_end(self, data):

new_node = Node(data)

if self.head is None:

self.head = new_node

return

temp = self.head

while temp.next:
temp = temp.next

temp.next = new_node

new_node.prev = temp

def insert_beginning(self, data):

new_node = Node(data)

new_node.next = self.head

if self.head:

self.head.prev = new_node

self.head = new_node

def delete_beginning(self):

if not self.head:

print("List is empty")

return

print("Deleted:", self.head.data)

self.head = self.head.next

if self.head:

self.head.prev = None

def delete_end(self):

if not self.head:

print("List is empty")

return

temp = self.head

if not temp.next:
print("Deleted:", temp.data)

self.head = None

return

while temp.next:

temp = temp.next

print("Deleted:", temp.data)

temp.prev.next = None

def display_forward(self):

temp = self.head

print("Forward:", end=" ")

while temp:

print(temp.data, end=" <-> ")

temp = temp.next

print("None")

def display_reverse(self):

temp = self.head

if not temp:

print("Reverse: None")

return

while temp.next:

temp = temp.next

print("Reverse:", end=" ")

while temp:

print(temp.data, end=" <-> ")


temp = temp.prev

print("None")

# Demo

dll = DoublyLinkedList()

dll.insert_end(10)

dll.insert_end(20)

dll.insert_beginning(5)

dll.display_forward()

dll.display_reverse()

dll.delete_beginning()

dll.delete_end()

dll.display_forward()

C:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *prev;

struct Node *next;

};

struct Node* head = NULL;


void insert_end(int data) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = data;

newNode->prev = newNode->next = NULL;

if (head == NULL) {

head = newNode;

return;

struct Node* temp = head;

while (temp->next)

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

void insert_beginning(int data) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = data;

newNode->prev = NULL;

newNode->next = head;

if (head)

head->prev = newNode;

head = newNode;

}
void delete_beginning() {

if (!head) {

printf("List is empty\n");

return;

struct Node* temp = head;

printf("Deleted: %d\n", temp->data);

head = head->next;

if (head)

head->prev = NULL;

free(temp);

void delete_end() {

if (!head) {

printf("List is empty\n");

return;

struct Node* temp = head;

if (!temp->next) {

printf("Deleted: %d\n", temp->data);

head = NULL;

free(temp);

return;

while (temp->next)
temp = temp->next;

printf("Deleted: %d\n", temp->data);

temp->prev->next = NULL;

free(temp);

void display_forward() {

struct Node* temp = head;

printf("Forward: ");

while (temp) {

printf("%d <-> ", temp->data);

temp = temp->next;

printf("NULL\n");

void display_reverse() {

struct Node* temp = head;

if (!temp) {

printf("Reverse: NULL\n");

return;

while (temp->next)

temp = temp->next;

printf("Reverse: ");

while (temp) {
printf("%d <-> ", temp->data);

temp = temp->prev;

printf("NULL\n");

int main() {

insert_end(10);

insert_end(20);

insert_beginning(5);

display_forward();

display_reverse();

delete_beginning();

delete_end();

display_forward();

return 0;

Experiment 8: Implementation of Circular Linked List

A Circular Linked List is a variation of a singly linked list where the last node points back
to the first node, forming a circle.

We’ll implement:

 Insertion at end

 Deletion at beginning

 Display
Circular Linked List

Python:

class Node:

def __init__(self, data):

self.data = data

self.next = None

class CircularLinkedList:

def __init__(self):

self.last = None

def insert_end(self, data):

new_node = Node(data)

if not self.last:

self.last = new_node

self.last.next = new_node

else:

new_node.next = self.last.next

self.last.next = new_node

self.last = new_node

def delete_beginning(self):

if not self.last:

print("List is empty")

return

first = self.last.next
if self.last == first:

print("Deleted:", first.data)

self.last = None

else:

print("Deleted:", first.data)

self.last.next = first.next

def display(self):

if not self.last:

print("List is empty")

return

temp = self.last.next

print("Circular List:", end=" ")

while True:

print(temp.data, end=" -> ")

temp = temp.next

if temp == self.last.next:

break

print("(back to start)")

# Demo

cll = CircularLinkedList()

cll.insert_end(10)

cll.insert_end(20)

cll.insert_end(30)

cll.display()
cll.delete_beginning()

cll.display()

C:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* last = NULL;

void insert_end(int data) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = data;

if (last == NULL) {

last = newNode;

last->next = last;

} else {

newNode->next = last->next;

last->next = newNode;

last = newNode;
}

void delete_beginning() {

if (last == NULL) {

printf("List is empty\n");

return;

struct Node* temp = last->next;

if (last == temp) {

printf("Deleted: %d\n", temp->data);

free(temp);

last = NULL;

} else {

printf("Deleted: %d\n", temp->data);

last->next = temp->next;

free(temp);

void display() {

if (last == NULL) {

printf("List is empty\n");

return;
}

struct Node* temp = last->next;

printf("Circular List: ");

do {

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

temp = temp->next;

} while (temp != last->next);

printf("(back to start)\n");

int main() {

insert_end(10);

insert_end(20);

insert_end(30);

display();

delete_beginning();

display();

return 0;

Experiment 9: Implementation of Stacks and Queues using Linked Lists

We’ll implement:

 Stack (LL-based) with: push, pop, display

 Queue (LL-based) with: enqueue, dequeue, display

A. Stack using Linked List


Python:

class Node:

def __init__(self, data):

self.data = data

self.next = None

class StackLL:

def __init__(self):

self.top = None

def push(self, data):

new_node = Node(data)

new_node.next = self.top

self.top = new_node

def pop(self):

if not self.top:

print("Stack Underflow")

else:

print("Popped:", self.top.data)

self.top = self.top.next

def display(self):

temp = self.top

print("Stack (top to bottom):", end=" ")


while temp:

print(temp.data, end=" -> ")

temp = temp.next

print("None")

# Demo

s = StackLL()

s.push(10)

s.push(20)

s.push(30)

s.display()

s.pop()

s.display()

C:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* top = NULL;


void push(int value) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = top;

top = newNode;

void pop() {

if (top == NULL) {

printf("Stack Underflow\n");

return;

printf("Popped: %d\n", top->data);

struct Node* temp = top;

top = top->next;

free(temp);

void display() {

struct Node* temp = top;

printf("Stack (top to bottom): ");

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");
}

int main() {

push(10);

push(20);

push(30);

display();

pop();

display();

return 0;

B. Queue using Linked List

Python:

class Node:

def __init__(self, data):

self.data = data

self.next = None

class QueueLL:

def __init__(self):

self.front = None

self.rear = None

def enqueue(self, data):

new_node = Node(data)
if not self.rear:

self.front = self.rear = new_node

else:

self.rear.next = new_node

self.rear = new_node

def dequeue(self):

if not self.front:

print("Queue Underflow")

else:

print("Dequeued:", self.front.data)

self.front = self.front.next

if self.front is None:

self.rear = None

def display(self):

temp = self.front

print("Queue (front to rear):", end=" ")

while temp:

print(temp.data, end=" -> ")

temp = temp.next

print("None")

# Demo

q = QueueLL()

q.enqueue(10)
q.enqueue(20)

q.enqueue(30)

q.display()

q.dequeue()

q.display()

C:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node *front = NULL, *rear = NULL;

void enqueue(int value) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = value;

newNode->next = NULL;

if (rear == NULL) {

front = rear = newNode;

} else {

rear->next = newNode;
rear = newNode;

void dequeue() {

if (front == NULL) {

printf("Queue Underflow\n");

return;

struct Node* temp = front;

printf("Dequeued: %d\n", front->data);

front = front->next;

if (front == NULL)

rear = NULL;

free(temp);

void display() {

struct Node* temp = front;

printf("Queue (front to rear): ");

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

}
int main() {

enqueue(10);

enqueue(20);

enqueue(30);

display();

dequeue();

display();

return 0;

Experiment 10: Binary Search Tree (BST) Implementation

We’ll implement a Binary Search Tree with the following operations:

 Insertion

 Deletion

 Search

 Inorder traversal (to show sorted elements)

Binary Search Tree (BST)

Python:

class Node:

def __init__(self, data):

self.data = data

self.left = self.right = None

class BST:
def insert(self, root, data):

if root is None:

return Node(data)

if data < root.data:

root.left = self.insert(root.left, data)

else:

root.right = self.insert(root.right, data)

return root

def inorder(self, root):

if root:

self.inorder(root.left)

print(root.data, end=" ")

self.inorder(root.right)

def search(self, root, key):

if root is None or root.data == key:

return root

if key < root.data:

return self.search(root.left, key)

else:

return self.search(root.right, key)

def delete(self, root, key):

if root is None:

return root
if key < root.data:

root.left = self.delete(root.left, key)

elif key > root.data:

root.right = self.delete(root.right, key)

else:

# Node with 1 or 0 children

if root.left is None:

return root.right

elif root.right is None:

return root.left

# Node with 2 children: Get inorder successor

temp = self.min_value_node(root.right)

root.data = temp.data

root.right = self.delete(root.right, temp.data)

return root

def min_value_node(self, node):

current = node

while current.left:

current = current.left

return current

# Demo

bst = BST()

root = None

for val in [50, 30, 70, 20, 40, 60, 80]:


root = bst.insert(root, val)

print("Inorder traversal:")

bst.inorder(root)

print("\n\nSearch for 60:")

found = bst.search(root, 60)

print("Found" if found else "Not Found")

print("\nDelete 30 and show inorder:")

root = bst.delete(root, 30)

bst.inorder(root)

C:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *left, *right;

};

struct Node* create(int data) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = newNode->right = NULL;


return newNode;

struct Node* insert(struct Node* root, int data) {

if (root == NULL) return create(data);

if (data < root->data)

root->left = insert(root->left, data);

else

root->right = insert(root->right, data);

return root;

void inorder(struct Node* root) {

if (root != NULL) {

inorder(root->left);

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

inorder(root->right);

struct Node* search(struct Node* root, int key) {

if (root == NULL || root->data == key) return root;

if (key < root->data)

return search(root->left, key);

else

return search(root->right, key);


}

struct Node* minValueNode(struct Node* node) {

struct Node* current = node;

while (current && current->left != NULL)

current = current->left;

return current;

struct Node* deleteNode(struct Node* root, int key) {

if (root == NULL) return root;

if (key < root->data)

root->left = deleteNode(root->left, key);

else if (key > root->data)

root->right = deleteNode(root->right, key);

else {

if (root->left == NULL) {

struct Node* temp = root->right;

free(root);

return temp;

else if (root->right == NULL) {

struct Node* temp = root->left;

free(root);

return temp;

}
struct Node* temp = minValueNode(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

return root;

int main() {

struct Node* root = NULL;

root = insert(root, 50);

insert(root, 30);

insert(root, 70);

insert(root, 20);

insert(root, 40);

insert(root, 60);

insert(root, 80);

printf("Inorder traversal:\n");

inorder(root);

printf("\n\nSearching for 60:\n");

struct Node* found = search(root, 60);

printf(found ? "Found\n" : "Not Found\n");

printf("\nDelete 30 and show inorder:\n");

root = deleteNode(root, 30);


inorder(root);

return 0;

Experiment 11: Tree Traversal on Binary Trees

We'll implement the three standard binary tree traversals:

1. Inorder Traversal (Left → Root → Right)

2. Preorder Traversal (Root → Left → Right)

3. Postorder Traversal (Left → Right → Root)

Binary Tree Traversals

Python:

class Node:

def __init__(self, data):

self.data = data

self.left = self.right = None

class BinaryTree:

def inorder(self, root):

if root:

self.inorder(root.left)

print(root.data, end=" ")

self.inorder(root.right)

def preorder(self, root):


if root:

print(root.data, end=" ")

self.preorder(root.left)

self.preorder(root.right)

def postorder(self, root):

if root:

self.postorder(root.left)

self.postorder(root.right)

print(root.data, end=" ")

# Demo

bt = BinaryTree()

root = Node(1)

root.left = Node(2)

root.right = Node(3)

root.left.left = Node(4)

root.left.right = Node(5)

print("Inorder Traversal:")

bt.inorder(root)

print("\nPreorder Traversal:")

bt.preorder(root)

print("\nPostorder Traversal:")
bt.postorder(root)

C:

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

struct Node* create(int data) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

void inorder(struct Node* root) {

if (root) {

inorder(root->left);

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

inorder(root->right);

}
}

void preorder(struct Node* root) {

if (root) {

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

preorder(root->left);

preorder(root->right);

void postorder(struct Node* root) {

if (root) {

postorder(root->left);

postorder(root->right);

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

int main() {

struct Node* root = create(1);

root->left = create(2);

root->right = create(3);

root->left->left = create(4);

root->left->right = create(5);

printf("Inorder Traversal:\n");
inorder(root);

printf("\nPreorder Traversal:\n");

preorder(root);

printf("\nPostorder Traversal:\n");

postorder(root);

return 0;

Experiment 12: Implementation of AVL Trees

An AVL Tree is a self-balancing Binary Search Tree (BST) where the difference in heights
of left and right subtrees (the balance factor) is at most 1 for every node.

We'll implement:

 Insertion with balancing

 Rotation operations (LL, RR, LR, RL)

 Inorder traversal

AVL Tree

Python:

class Node:

def __init__(self, data):

self.data = data

self.left = self.right = None

self.height = 1
class AVLTree:

def insert(self, root, key):

if not root:

return Node(key)

elif key < root.data:

root.left = self.insert(root.left, key)

else:

root.right = self.insert(root.right, key)

root.height = 1 + max(self.get_height(root.left), self.get_height(root.right))

balance = self.get_balance(root)

# Left Left

if balance > 1 and key < root.left.data:

return self.right_rotate(root)

# Right Right

if balance < -1 and key > root.right.data:

return self.left_rotate(root)

# Left Right

if balance > 1 and key > root.left.data:

root.left = self.left_rotate(root.left)

return self.right_rotate(root)

# Right Left

if balance < -1 and key < root.right.data:

root.right = self.right_rotate(root.right)
return self.left_rotate(root)

return root

def left_rotate(self, z):

y = z.right

T2 = y.left

y.left = z

z.right = T2

z.height = 1 + max(self.get_height(z.left), self.get_height(z.right))

y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))

return y

def right_rotate(self, z):

y = z.left

T3 = y.right

y.right = z

z.left = T3

z.height = 1 + max(self.get_height(z.left), self.get_height(z.right))

y.height = 1 + max(self.get_height(y.left), self.get_height(y.right))

return y

def get_height(self, node):

if not node:

return 0

return node.height
def get_balance(self, node):

if not node:

return 0

return self.get_height(node.left) - self.get_height(node.right)

def inorder(self, root):

if root:

self.inorder(root.left)

print(root.data, end=" ")

self.inorder(root.right)

# Demo

tree = AVLTree()

root = None

values = [10, 20, 30, 40, 50, 25]

for val in values:

root = tree.insert(root, val)

print("Inorder Traversal of AVL Tree:")

tree.inorder(root)

C:

#include <stdio.h>

#include <stdlib.h>
struct Node {

int data, height;

struct Node* left;

struct Node* right;

};

int max(int a, int b) {

return (a > b) ? a : b;

int height(struct Node* N) {

return N ? N->height : 0;

int getBalance(struct Node* N) {

return N ? height(N->left) - height(N->right) : 0;

struct Node* newNode(int data) {

struct Node* node = (struct Node*)malloc(sizeof(struct Node));

node->data = data;

node->left = node->right = NULL;

node->height = 1;

return node;

}
struct Node* rightRotate(struct Node* y) {

struct Node* x = y->left;

struct Node* T2 = x->right;

x->right = y;

y->left = T2;

y->height = max(height(y->left), height(y->right)) + 1;

x->height = max(height(x->left), height(x->right)) + 1;

return x;

struct Node* leftRotate(struct Node* x) {

struct Node* y = x->right;

struct Node* T2 = y->left;

y->left = x;

x->right = T2;

x->height = max(height(x->left), height(x->right)) + 1;

y->height = max(height(y->left), height(y->right)) + 1;

return y;

struct Node* insert(struct Node* node, int key) {

if (!node) return newNode(key);

if (key < node->data)

node->left = insert(node->left, key);

else if (key > node->data)

node->right = insert(node->right, key);


else

return node;

node->height = 1 + max(height(node->left), height(node->right));

int balance = getBalance(node);

// LL

if (balance > 1 && key < node->left->data)

return rightRotate(node);

// RR

if (balance < -1 && key > node->right->data)

return leftRotate(node);

// LR

if (balance > 1 && key > node->left->data) {

node->left = leftRotate(node->left);

return rightRotate(node);

// RL

if (balance < -1 && key < node->right->data) {

node->right = rightRotate(node->right);

return leftRotate(node);

return node;

}
void inorder(struct Node* root) {

if (root != NULL) {

inorder(root->left);

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

inorder(root->right);

int main() {

struct Node* root = NULL;

int values[] = {10, 20, 30, 40, 50, 25};

int n = sizeof(values) / sizeof(values[0]);

for (int i = 0; i < n; i++)

root = insert(root, values[i]);

printf("Inorder Traversal of AVL Tree:\n");

inorder(root);

return 0;

Experiment 13: Implementation of Graph Traversals

We'll implement:

 Breadth-First Search (BFS)

 Depth-First Search (DFS)


Using adjacency list representation for simplicity and efficiency.
Graph Traversal using Adjacency List

Python:

from collections import defaultdict, deque

class Graph:

def __init__(self):

self.graph = defaultdict(list)

def add_edge(self, u, v):

self.graph[u].append(v)

# self.graph[v].append(u) # Uncomment for undirected graph

def bfs(self, start):

visited = set()

queue = deque([start])

print("BFS:", end=" ")

while queue:

node = queue.popleft()

if node not in visited:

print(node, end=" ")

visited.add(node)

for neighbor in self.graph[node]:

if neighbor not in visited:

queue.append(neighbor)

print()
def dfs_util(self, node, visited):

visited.add(node)

print(node, end=" ")

for neighbor in self.graph[node]:

if neighbor not in visited:

self.dfs_util(neighbor, visited)

def dfs(self, start):

visited = set()

print("DFS:", end=" ")

self.dfs_util(start, visited)

print()

# Demo

g = Graph()

g.add_edge(0, 1)

g.add_edge(0, 2)

g.add_edge(1, 3)

g.add_edge(2, 4)

g.add_edge(3, 5)

g.bfs(0)

g.dfs(0)
C:

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

struct Node {

int vertex;

struct Node* next;

};

struct Graph {

int numVertices;

struct Node** adjLists;

int* visited;

};

struct Queue {

int items[MAX];

int front, rear;

};

struct Node* createNode(int v) {

struct Node* newNode = malloc(sizeof(struct Node));

newNode->vertex = v;

newNode->next = NULL;
return newNode;

struct Graph* createGraph(int vertices) {

struct Graph* graph = malloc(sizeof(struct Graph));

graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct Node*));

graph->visited = malloc(vertices * sizeof(int));

for (int i = 0; i < vertices; i++) {

graph->adjLists[i] = NULL;

graph->visited[i] = 0;

return graph;

void addEdge(struct Graph* graph, int src, int dest) {

struct Node* newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

// For undirected graph:

// newNode = createNode(src);

// newNode->next = graph->adjLists[dest];

// graph->adjLists[dest] = newNode;
}

struct Queue* createQueue() {

struct Queue* q = malloc(sizeof(struct Queue));

q->front = q->rear = -1;

return q;

int isEmpty(struct Queue* q) {

return q->rear == -1;

void enqueue(struct Queue* q, int value) {

if (q->rear == MAX - 1)

return;

if (q->front == -1) q->front = 0;

q->rear++;

q->items[q->rear] = value;

int dequeue(struct Queue* q) {

int item;

if (isEmpty(q)) return -1;

item = q->items[q->front];

q->front++;

if (q->front > q->rear) q->front = q->rear = -1;


return item;

void bfs(struct Graph* graph, int startVertex) {

struct Queue* q = createQueue();

graph->visited[startVertex] = 1;

enqueue(q, startVertex);

printf("BFS: ");

while (!isEmpty(q)) {

int current = dequeue(q);

printf("%d ", current);

struct Node* temp = graph->adjLists[current];

while (temp) {

int adjVertex = temp->vertex;

if (!graph->visited[adjVertex]) {

graph->visited[adjVertex] = 1;

enqueue(q, adjVertex);

temp = temp->next;

printf("\n");

}
void dfs(struct Graph* graph, int vertex) {

graph->visited[vertex] = 1;

printf("%d ", vertex);

struct Node* temp = graph->adjLists[vertex];

while (temp) {

int adjVertex = temp->vertex;

if (!graph->visited[adjVertex]) {

dfs(graph, adjVertex);

temp = temp->next;

void resetVisited(struct Graph* graph) {

for (int i = 0; i < graph->numVertices; i++)

graph->visited[i] = 0;

int main() {

struct Graph* g = createGraph(6);

addEdge(g, 0, 1);

addEdge(g, 0, 2);

addEdge(g, 1, 3);

addEdge(g, 2, 4);

addEdge(g, 3, 5);
bfs(g, 0);

resetVisited(g);

printf("DFS: ");

dfs(g, 0);

printf("\n");

return 0;

Experiment 14: Implementation of Prim’s and Kruskal’s Algorithm

These two algorithms are used to find the Minimum Spanning Tree (MST) of a connected,
weighted, undirected graph.

A. Prim’s Algorithm (Using Adjacency Matrix)

Python:

import sys

class Prims:

def __init__(self, graph):

self.graph = graph

self.V = len(graph)

def min_key(self, key, mst_set):

min_val = sys.maxsize
min_index = -1

for v in range(self.V):

if key[v] < min_val and not mst_set[v]:

min_val = key[v]

min_index = v

return min_index

def prim_mst(self):

key = [sys.maxsize] * self.V

key[0] = 0

parent = [None] * self.V

mst_set = [False] * self.V

for _ in range(self.V):

u = self.min_key(key, mst_set)

mst_set[u] = True

for v in range(self.V):

if self.graph[u][v] and not mst_set[v] and self.graph[u][v] < key[v]:

key[v] = self.graph[u][v]

parent[v] = u

print("Edge \tWeight")

for i in range(1, self.V):

print(f"{parent[i]} - {i} \t{self.graph[i][parent[i]]}")


# Sample graph as adjacency matrix

G = [[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]]

p = Prims(G)

p.prim_mst()

C (Prim’s Algorithm):

#include <stdio.h>

#include <limits.h>

#define V 5

int minKey(int key[], int mstSet[]) {

int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)

if (mstSet[v] == 0 && key[v] < min)

min = key[v], min_index = v;

return min_index;

}
void printMST(int parent[], int graph[V][V]) {

printf("Edge \tWeight\n");

for (int i = 1; i < V; i++)

printf("%d - %d \t%d\n", parent[i], i, graph[i][parent[i]]);

void primMST(int graph[V][V]) {

int parent[V];

int key[V];

int mstSet[V];

for (int i = 0; i < V; i++)

key[i] = INT_MAX, mstSet[i] = 0;

key[0] = 0;

parent[0] = -1;

for (int count = 0; count < V - 1; count++) {

int u = minKey(key, mstSet);

mstSet[u] = 1;

for (int v = 0; v < V; v++)

if (graph[u][v] && mstSet[v] == 0 && graph[u][v] < key[v])

parent[v] = u, key[v] = graph[u][v];

printMST(parent, graph);
}

int main() {

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}

};

primMST(graph);

return 0;

B. Kruskal’s Algorithm (Edge List + Union-Find)

Python:

class Kruskal:

def __init__(self, vertices):

self.V = vertices

self.graph = []

def add_edge(self, u, v, w):

self.graph.append((w, u, v))

def find(self, parent, i):


if parent[i] != i:

parent[i] = self.find(parent, parent[i])

return parent[i]

def union(self, parent, rank, xroot, yroot):

if rank[xroot] < rank[yroot]:

parent[xroot] = yroot

elif rank[yroot] < rank[xroot]:

parent[yroot] = xroot

else:

parent[yroot] = xroot

rank[xroot] += 1

def kruskal_mst(self):

self.graph.sort()

parent = [i for i in range(self.V)]

rank = [0] * self.V

mst = []

for weight, u, v in self.graph:

x = self.find(parent, u)

y = self.find(parent, v)

if x != y:

mst.append((u, v, weight))

self.union(parent, rank, x, y)
print("Edge \tWeight")

for u, v, w in mst:

print(f"{u} - {v} \t{w}")

# Demo

k = Kruskal(5)

k.add_edge(0, 1, 2)

k.add_edge(0, 3, 6)

k.add_edge(1, 2, 3)

k.add_edge(1, 3, 8)

k.add_edge(1, 4, 5)

k.add_edge(2, 4, 7)

k.add_edge(3, 4, 9)

k.kruskal_mst()

Kruskal’s Algorithm in C

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

struct Edge {

int src, dest, weight;

};
struct Graph {

int V, E;

struct Edge* edge;

};

struct Subset {

int parent;

int rank;

};

int find(struct Subset subsets[], int i) {

if (subsets[i].parent != i)

subsets[i].parent = find(subsets, subsets[i].parent);

return subsets[i].parent;

void Union(struct Subset subsets[], int x, int y) {

int xroot = find(subsets, x);

int yroot = find(subsets, y);

if (subsets[xroot].rank < subsets[yroot].rank)

subsets[xroot].parent = yroot;

else if (subsets[xroot].rank > subsets[yroot].rank)

subsets[yroot].parent = xroot;

else {

subsets[yroot].parent = xroot;
subsets[xroot].rank++;

int cmp(const void* a, const void* b) {

struct Edge* a1 = (struct Edge*)a;

struct Edge* b1 = (struct Edge*)b;

return a1->weight > b1->weight;

void KruskalMST(struct Graph* graph) {

int V = graph->V;

struct Edge result[MAX];

int e = 0;

int i = 0;

qsort(graph->edge, graph->E, sizeof(graph->edge[0]), cmp);

struct Subset* subsets = (struct Subset*)malloc(V * sizeof(struct Subset));

for (int v = 0; v < V; ++v) {

subsets[v].parent = v;

subsets[v].rank = 0;

while (e < V - 1 && i < graph->E) {


struct Edge next_edge = graph->edge[i++];

int x = find(subsets, next_edge.src);

int y = find(subsets, next_edge.dest);

if (x != y) {

result[e++] = next_edge;

Union(subsets, x, y);

printf("Edges in MST:\n");

for (i = 0; i < e; ++i)

printf("%d - %d \tWeight: %d\n", result[i].src, result[i].dest, result[i].weight);

struct Graph* createGraph(int V, int E) {

struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));

graph->V = V;

graph->E = E;

graph->edge = (struct Edge*)malloc(E * sizeof(struct Edge));

return graph;

int main() {

int V = 4;

int E = 5;

struct Graph* graph = createGraph(V, E);


graph->edge[0].src = 0;

graph->edge[0].dest = 1;

graph->edge[0].weight = 10;

graph->edge[1].src = 0;

graph->edge[1].dest = 2;

graph->edge[1].weight = 6;

graph->edge[2].src = 0;

graph->edge[2].dest = 3;

graph->edge[2].weight = 5;

graph->edge[3].src = 1;

graph->edge[3].dest = 3;

graph->edge[3].weight = 15;

graph->edge[4].src = 2;

graph->edge[4].dest = 3;

graph->edge[4].weight = 4;

KruskalMST(graph);

return 0;

You might also like