DS Mannual Updated Shanti
DS Mannual Updated Shanti
DS Mannual Updated Shanti
ADMIN
[Pick the date]
[Type the abstract of the document here. The abstract is typically a short summary
of the contents of the document. Type the abstract of the document here. The
abstract is typically a short summary of the contents of the document.]
St.Mary’s Group of Institutions, Hyderabad
Near Ramoji Film City, Behind Mount Opera, Deshmukhi
Village, Pochampally Mandal, Yadadri Bhuvanagiri District
DATA STRUCTURES
LAB MANUAL
3
VISION AND MISSION OF AIML DEPARTMENT
VISION
To be recognized as a department of excellence by stimulating a
learning environment in which students and faculty will thrive
and grow to achieve their professional, institutional and societal
goals.
MISSION
To provide high quality technical education to students that will
enable life-long learning and buildexpertise in advanced
technologies in Computer Science and Engineering.
To promote research and development by providing
opportunities to solve complex engineeringproblems in
collaboration with industry and government agencies.
To encourage professional development of students that will
inculcate ethical values and leadership skills while working
with the community to address societal issues.
4
PROGRAM EDUCATIONAL OBJECTIVES (PEOS):
A graduate of the Computer Science and Engineering Program
should:
Program Educational Objective1: (PEO1)
The Graduates will provide solutions to difficult and challenging
PEO1 issues in their professionby applying computer science and
engineering theory and principles.
Program Educational Objective2 :( PEO2)
The Graduates have successful careers in computer science and
PEO2 engineering fields or will beable to successfully pursue advanced
degrees.
Program Educational Objective3: (PEO3)
The Graduates will communicate effectively, work collaboratively
PEO3 and exhibit high levels ofProfessionalism, moral and ethical
responsibility.
Program Educational Objective4 :( PEO4)
The Graduates will develop the ability to understand and analyze
PEO4 Engineering issues in a broader perspective with ethical
responsibility towards sustainable development.
5
Problem analysis: Identify, formulate, review research literature,
PO2 and analyze complete Engineering problems reaching
substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
Design/development of solutions: Design solutions for complex
engineering problems and design system components or
PO3 processes that meet the specified needs with appropriate
Consideration for the public health and safety, and the cultural,
societal, and environmental considerations.
Conduct investigations of complex problems: Use research-based
PO4 knowledge and research methods including design of experiments,
analysis and interpretation of data, and synthesis ofthe
information to provide valid conclusions.
6
Communication: Communicate effectively on complex
engineering activities with the engineering community and with
PO1 society at large, such as, being able to comprehend and write
0 effective reports and design documentation, make effective
presentations, and give and receiveclear instructions.
Project management and finance: Demonstrate knowledge and
PO1 understanding of the Engineering and management principles and
1 apply these to one’s own work, as a member andleader in a team,
to manage projects and in multidisciplinary environments.
Life-long learning: Recognize the need for, and have the
PO1 preparation and ability to engage in independent and life-long
2 learning in the broadest context of technological
change.
8
to sort a given list of integers in ascending order
i) Quick sort ii) Heap sort iii) Merge sort
7. Write
a program to implement the tree traversal methods(
Recursive and Non Recursive).
8. Write a program to implement
i) Binary Search tree ii) B Trees iii) B+ Trees iv) AVL
trees v) Red - Black trees
9. Write a program to implement the graph traversal methods.
10. Implement a Pattern matching algorithms using Boyer- Moore,
Knuth-Morris-Pratt
9
INDEX
S.No. Name of the Program Page
No.
Week1
Write a program that uses functions to perform the following operations on singly
linked list:
1
i) Creation ii) Insertion iii) Deletion iv)Traversal
Week 2
Write a program that uses functions to perform the following operations on doubly
linked list:
2
i) Creation ii) Insertion
iii) Deletion iv) Traversal
Week 3
Write a program that uses functions to perform the following operations on circular
linked list:
3
i) Creation ii) Insertioniii) Deletion iv)Traversal
Week 4
Write a program that implement stack (its operations) using
4 i) Arrays ii) Pointers
Week 5
Write a program that implement Queue (its operations) using
5 i) Arrays ii) Pointers
Week 6
Write a program that implements the following sorting methods to sort a given
6 list of integers in ascending order
i) Quick sort ii) Heap sort iii) Merge sort
Week 7
Write a program to implement the tree traversal methods (Recursive and Non
Recursive).
7
Week 8
Write a program to implement
8 i) Binary Search tree ii) B Trees iii) B+ Trees iv) AVL trees v) Red - Black trees
Week 9
Write a program to implement the graph traversal methods.
9
Week 10
10 Implement a Pattern matching algorithms using Boyer- Moore, Knuth-Morris-Pratt
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i;
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output: 1 2 3 4 5
1. Array Reverse
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i, temp;
printf("Original array: ");
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
for (i = 0; i < 5 / 2; i++) {
temp = arr[i];
arr[i] = arr[5 - i - 1];
arr[5 - i - 1] = temp;
}
printf("Reversed array: ");
for (i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Output:
Original array: 1 2 3 4 5
Reversed array: 5 4 3 2 1
1. Array Search
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i, target;
printf("Enter a number to search: ");
scanf("%d", &target);
1. Pointer Basics
#include <stdio.h>
int main() {
int var = 10;
int* ptr = &var;
printf("Value of var: %d\n", var);
printf("Address of var: %p\n", &var);
printf("Value of ptr: %p\n", ptr);
printf("Value at ptr: %d\n", *ptr);
return 0;
}
Output:
Value of var: 10
Address of var: 0x7fffcf4f4b4c
Value of ptr: 0x7fffcf4f4b4c
Value at ptr: 10
1. Pointer Arithmetic
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int* ptr = arr;
printf("Value at ptr: %d\n", *ptr);
printf("Value at ptr + 1: %d\n", *(ptr + 1));
printf("Value at ptr + 2: %d\n", *(ptr + 2));
return 0;
}
1. Pointer Comparison
#include <stdio.h>
int main() {
int var1 = 10;
int var2 = 20;
int* ptr1 = &var1;
int* ptr2 = &var2;
if (ptr1 > ptr2) {
printf("ptr1 is greater than ptr2\n");
} else {
printf("ptr1 is less than or equal to ptr2\n");
}
return 0;
}
Output:
ptr1 is less than or equal to ptr2
#include <stdio.h>
struct Person {
int age;
char name[20];
};
int main() {
struct Person person = {25, "John"};
struct Person* ptr = &person;
printf("Age: %d\n", ptr->age);
printf("Name: %s\n", ptr->name);
return 0;
}
Output:
Age: 25
Name: John
int main() {
Node* head = NULL;
createList(&head);
printf("Linked List: ");
traverse(head);
insertAtBeginning(&head, 0);
insertAtEnd(&head, 4);
printf("Linked List after insertion: ");
traverse(head);
deleteNode(&head, 2);
printf("Linked List after deletion: ");
traverse(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* head = NULL;
createList(&head);
traverse(head);
insertAtBeginning(&head, 0);
insertAtEnd(&head, 4);
printf("After insertion:\n");
traverse(head);
deleteNode(&head, 2);
printf("After deletion:\n");
traverse(head);
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* head = NULL;
createList(&head);
traverse(head);
insertAtBeginning(&head, 0);
insertAtEnd(&head, 4);
printf("After insertion:\n");
traverse(head);
deleteNode(&head, 2);
printf("After deletion:\n");
traverse(head);
#include <stdio.h>
// Array Implementation
#define MAX_SIZE 5
int stackArray[MAX_SIZE];
int topArray = -1;
void pushArray(int data) {
if (topArray == MAX_SIZE - 1) {
printf("Stack overflow\n");
return;
}
stackArray[++topArray] = data;
}
void popArray() {
if (topArray == -1) {
printf("Stack underflow\n");
return;
}
topArray--;
}
void displayArray() {
if (topArray == -1) {
printf("Stack is empty\n");
return;
}
printf("Stack (Array): ");
for (int i = 0; i <= topArray; i++) {
printf("%d ", stackArray[i]);
}
printf("\n");
}
// Pointer Implementation
typedef struct Node {
int data;
struct Node* next;
Stack (Array): 1 2 3
Stack (Array): 1 2
Stack (Pointer): 3 2 1
Stack (Pointer): 2 1
Week:5
#include <stdio.h>
// Array Implementation
#define MAX_SIZE 5
int queueArray[MAX_SIZE];
int frontArray = 0;
int rearArray = -1;
int countArray = 0;
void enqueueArray(int data) {
if (countArray == MAX_SIZE) {
printf("Queue overflow\n");
return;
}
rearArray = (rearArray + 1) % MAX_SIZE;
queueArray[rearArray] = data;
countArray++;
}
void dequeueArray() {
if (countArray == 0) {
printf("Queue underflow\n");
return;
}
frontArray = (frontArray + 1) % MAX_SIZE;
countArray--;
}
void displayArray() {
if (countArray == 0) {
printf("Queue is empty\n");
return;
}
printf("Queue (Array): ");
int i = frontArray;
Queue (Array): 1 2 3
Queue (Array): 2 3
Queue (Pointer): 1 2 3
Queue (Pointer): 2 3
Program:
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Quick Sort
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[high];
int i = (low - 1);
// Heap Sort
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
// Merge Sort
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
// Driver code
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
// Heap Sort
heapSort(arr, n);
printf("Sorted array using Heap Sort: \n");
printArray(arr, n);
// Merge Sort
mergeSort(arr, 0, n - 1);
printf("Sorted array using Merge Sort: \n");
printArray(arr, n);
return 0;
}
Original array:
64 25 12 22 11
Sorted array using Quick Sort:
11 12 22 25 64
Sorted array using Heap Sort:
11 12 22 25 64
Sorted array using Merge Sort:
11
int main() {
// Create a sample binary tree
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
return 0;
}
Output:
Recursive Inorder: 4 2 5 1 3
Recursive Preorder: 1 2 4 5 3
Recursive Postorder: 4 5 2 3 1
Non-Recursive Inorder: 4 2 5 1 3
Non-Recursive Preorder: 1 2 4 5 3
Non-Recursive Postorder: 4 5 2 3 1
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* root = createNode(1);
return 0;
}
Output:
B-Tree:
10
57
6
12
17
20
30
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* root = createNode(1);
return 0;
}
Output:
B+ Tree:
10
57
6
12
17
20
30
45
int main() {
Node* root = NULL;
return 0;
}
Output:
AVL Tree:
20
12
10
6
5
7
17
30
46
9)Write a program to implement the graph traversal methods.
#include <stdio.h>
#include <stdlib.h>
int main() {
Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 5);
49
printf("BFS Traversal: ");
BFS(graph, 0);
printf("\n");
printf("DFS Traversal: ");
DFS(graph, 0);
printf("\n");
return 0;
}
Output:
BFS Traversal: 0 1 2 3 4 5
DFS Traversal: 0 1 3 4 2 5
50
10) Implement a Pattern matching algorithms using Boyer- Moore,
Knuth-Morris-Pratt
#include <stdio.h>
#include <string.h>
// Boyer-Moore Algorithm
void boyerMoore(char* text, char* pattern) {
int n = strlen(text);
int m = strlen(pattern);
int i = m - 1;
int j = m - 1;
while (i < n) {
if (text[i] == pattern[j]) {
if (j == 0) {
printf("Pattern found at position %d\n", i);
return;
}
i--;
j--;
} else {
i = i + m - min(j, 1 + i - m);
j = m - 1;
}
}
printf("Pattern not found\n");
}
// Knuth-Morris-Pratt Algorithm
void knuthMorrisPratt(char* text, char* pattern) {
int n = strlen(text);
int m = strlen(pattern);
int lps[m];
int j = 0;
51
computeLPSArray(pattern, m, lps);
int i = 0;
while (i < n) {
if (pattern[j] == text[i]) {
j++;
i++;
}
if (j == m) {
printf("Pattern found at position %d\n", i - j);
j = lps[j - 1];
} else if (i < n && pattern[j] != text[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
printf("Pattern not found\n");
}
while (i < m) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
52
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
// Driver code
int main() {
char text[] = "ABABDABACDABABCABAB";
char pattern[] = "ABABCABAB";
printf("Boyer-Moore Algorithm:\n");
boyerMoore(text, pattern);
printf("Knuth-Morris-Pratt Algorithm:\n");
knuthMorrisPratt(text, pattern);
return 0;
}
Output:
Boyer-Moore Algorithm:
Pattern found at position 10
Knuth-Morris-Pratt Algorithm:
Pattern found at position 10
53
1) What is data structure?
Data structure refers to the way data is organized and
manipulated. It seeks to find ways to make data access more
efficient. When dealing with the data structure, we not only focus
on one piece of data but the different set of data and how they can
relate to one another in an organized manner.
54
4) What is a linked list?
A linked list is a sequence of nodes in which each node is
connected to the node following it. This forms a chain-like link
for data storage.
55
extracted.
8) What is a queue?
A queue is a data structure that can simulate a list or stream of
data. In this structure, new elements are inserted at one end, and
existing elements are removed from the other end.
10) Which data structures are applied when dealing with a recursive
function?
Recursion, is a function that calls itself based on a terminating
condition, makes use of the stack. Using LIFO, a call to a
recursive function saves the return address so that it knows how to
return to the calling function after the call terminates.
58
how many elements are in the list.
30) What is the minimum number of nodes that a binary tree can
have?
A binary tree can have a minimum of zero nodes, which occurs
61
when the nodes have NULL values. Furthermore, a binary tree
can also have 1 or 2 nodes.
63
1. if the tree is not empty, the target is in the tree
2. check if the target is in the root item
3. if a target is not in the root item, check if a target is smaller
than the root’s value
4. if a target is smaller than the root’s value, search the left
subtree
5. else, search the right subtree
64