Data Structures - Lab Manual1
Data Structures - Lab Manual1
(Autonomous)
RAMAPURAM, CHENNAI-89
LAB MANUAL
B.Tech – AIADS
Prepared By Approved By
3 02 0 4
COURSE OBJECTIVES
3. To introduce various techniques for representation of the data in the real world.
COURSE OUTCOMES
Course Name: 2311CSC302J – Data Structures and Algorithms Year / Sem: II / III
Upon completion of the course, the students will be able to:
CO1 Infer the concepts of abstract data types for linear data structures.
CO3 Develop solutions using linear and non- linear data structures.
CO4 Illustrate various searching and sorting techniques to solve the complex problem.
CO/PO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
CO1 1 2 2 1 - - - - - 2 1 1
CO2 1 2 1 1 - - - - - 1 - 1
CO3 2 2 1 1 - - - - - 1 - 1
CO4 2 2 1 1 - - - - - 1 - 1
CO5 1 3 2 2 - 2 1 - - 1 1 1
LIST OF EXPERIMENTS
TOTAL: 30 PERIODS
TABLE OF CONTENTS
Ex. No. Date Title of the Experiment Page No. Mark Signature
AIM:
To implement the List ADT using arrays in C, supporting the following operations:
Insertion
Deletion
Display
Search
ALGORITHM:
Initialization
1. Insert Operation
2. Delete Operation
3. Display Operation
Steps:
4. Search Operation
5. Exit Operation
PROGRAM
#include <stdio.h>
#define MAX 100
int list[MAX];
int size = 0;
int main() {
int choice, pos, value, key;
while (1) {
printf("\nList Operations:\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Search\n5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter position (0 to %d): ", size);
scanf("%d", &pos);
printf("Enter value: ");
scanf("%d", &value);
insert(pos, value);
break;
case 2:
printf("Enter position to delete (0 to %d): ", size - 1);
scanf("%d", &pos);
delete(pos);
break;
case 3:
display();
break;
case 4:
printf("Enter value to search:
"); scanf("%d", &key);
pos = search(key);
if (pos != -1)
printf("Element found at position %d\n", pos);
else
printf("Element not found\n");
break;
case 5:
return 0;
default:
printf("Invalid choice!\n");
}
}
return 0;
}
list[pos] = value;
size++;
printf("Inserted successfully.\n");
}
size--;
printf("Deleted successfully.\n");
}
void display()
{ if (size ==
0) {
printf("List is empty.\n");
return;
}
SAMPLE OUTPUT
List Operations:
1. Insert
2. Delete
3. Display
4. Search
5. Exit
Enter choice: 1
Enter position (0 to 0): 0
Enter value: 50
Inserted successfully.
Enter choice: 1
Enter position (0 to 1): 1
Enter value: 70
Inserted successfully.
Enter choice: 3
List elements: 50 70
Enter choice: 4
Enter value to search: 70
Element found at position 1
Enter choice: 2
Enter position to delete (0 to 1): 0
Deleted successfully.
Enter choice: 3
List elements: 70
RESULT
The List ADT in C was successfully implemented using a simple array. The program supports:
Insertion at any position
Deletion from any position
Searching for a value
Displaying the list
Ex NO: 2
LINKED LIST IMPLEMENTATION OF STACK ADT
DATE:
AIM
To implement the Stack Abstract Data Type (ADT) using a singly linked list in C, supporting:
ALGORITHM
Initialization:
PUSH Operation:
POP Operation:
DISPLAY Operation:
C PROGRAM
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
void push(int value);
void pop();
void peek(); void
display();
int main() {
int choice, value;
while (1) {
printf("\nStack Operations:\n");
printf("1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
// Push function
void push(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Heap Overflow\n");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
printf("Pushed %d to stack\n", value);
}
// Pop function
void pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return;
}
Node* temp = top;
printf("Popped %d from stack\n", top-
>data); top = top->next;
free(temp);
}
// Peek function
void peek() {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
printf("Top element is %d\n", top->data);
}
// Display function
void display() {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
Node* current = top;
printf("Stack elements: ");
while (current != NULL)
{
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
SAMPLE OUTPUT
Stack Operations:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter choice: 1
Enter value to push: 10
Pushed 10 to stack
Enter choice: 1
Enter value to push: 20
Pushed 20 to stack
Enter choice: 3
Top element is 20
Enter choice: 4
Stack elements: 20 10
Enter choice: 2
Popped 20 from stack
Enter choice: 4
Stack elements: 10
RESULT
The Stack ADT with all operations (push, pop, peek, and display) was successfully implemented using a
linked list in C.
Ex NO: 3
LINKED LIST IMPLEMENTATION OF QUEUE ADT
DATE:
AIM
To implement the Queue Abstract Data Type (ADT) using a singly linked list in C, supporting:
ALGORITHM
DISPLAY
C PROGRAM
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
void enqueue(int value);
void dequeue();
void peek();
void display();
int main() {
int choice, value;
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue:
"); scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
// Enqueue function
void enqueue(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("Enqueued %d\n", value);
}
// Dequeue function
void dequeue() {
if (front == NULL) {
printf("Queue Underflow (empty)\n");
return;
}
Node* temp = front;
printf("Dequeued %d\n", front->data);
front = front->next;
if (front == NULL)
rear = NULL;
free(temp);
}
// Peek function
void peek() {
if (front == NULL) {
printf("Queue is empty\n");
} else {
printf("Front element is %d\n", front->data);
}
}
// Display function
void display() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}
Node* current = front;
printf("Queue elements: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
SAMPLE OUTPUT
Queue Operations:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter value to enqueue: 10
Enqueued 10
RESULT
AIM
ALGORITHM
BFS(G, start_vertex):
C PROGRAM
#include <stdio.h>
#include <stdlib.h>
// Queue operations
void enqueue(int value) {
if (rear == MAX - 1) return;
if (front == -1) front = 0;
queue[++rear] = value;
}
int dequeue() {
if (front == -1 || front > rear) return -1;
return queue[front++];
}
int isEmpty() {
return (front == -1 || front > rear);
}
// BFS traversal
void bfs(int n, int start) {
for (int i = 0; i < n; i++) visited[i] = 0;
enqueue(start);
visited[start] = 1;
while (!isEmpty()) {
int v = dequeue();
printf("%d ", v);
// Main function
int main() {
int n, e, u, v, start;
// Read edges
printf("Enter edges (u v):\n");
for (int i = 0; i < e; i++) {
scanf("%d %d", &u, &v);
adj[u][v] = 1;
adj[v][u] = 1; // Comment this line if graph is directed
}
bfs(n, start);
return 0;
}
SAMPLE OUTPUT
Enter number of vertices: 5
Enter number of edges: 6
Enter edges (u v):
01
02
13
14
24
34
Enter starting vertex: 0
BFS Traversal starting from vertex 0: 0 1 2 3 4
RESULT
The Breadth First Search (BFS) algorithm was successfully implemented using an adjacency matrix in C,
and the traversal output is generated from a given starting node.
Ex NO: 5
IMPLEMENTATION OF BINARY SEARCH
DATE:
AIM
To implement the Binary Search algorithm in C for searching an element in a sorted array.
ALGORITHM
Steps:
C PROGRAM
#include <stdio.h>
if (arr[mid] == key)
return mid;
else if (arr[mid] <
key) low = mid + 1;
else
high = mid - 1;
}
return -1; // Not found
}
int main() {
int arr[100], n, key, i, result;
if (result == -1)
printf("Element not found in the array.\n");
else
printf("Element found at index %d.\n", result);
return 0;
}
SAMPLE OUTPUT
Enter number of elements: 5
Enter 5 sorted elements:
13579
Enter element to search:
5 Element found at index
2.
RESULT
AIM
ALGORITHM
Steps:
Optional: Use a swapped flag to optimize by stopping early if no swaps are made.
C PROGRAM
#include <stdio.h>
int main() {
int arr[100], n;
printf("Enter %d elements:\n",
n); for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
}
SAMPLE OUTPUT
Enter number
of elements: 5
Enter 5
elements:
64 25 12 22 11
Sorted array: 11 12 22 25 64
RESULT
Aim
Algorithm
Step 3: Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
Program
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?:");
scanf("%d",&n);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
Output:
Result
Thus the ‘C’ program to implement the Linear search has been executed and the output is verified
successfully.
Ex NO: 8
IMPLEMENTATION OF BINARY SEARCH TREES
DATE:
AND TRAVERSALS
Aim
Algorithm
Step 1: Start
Step 2: Create a Binary Search Tree for N elements.
Step 3: Traverse the tree in inorder.
Step 4: Traverse the tree in preorder
Step 6: Traverse the tree in postorder.
Step 7: Search the given key element in the BST.
Step 8: Delete an element from BST.
Step 9: Stop
Program
#include <stdlib.h>
typedef struct tnode
{
int data;
struct tnode *right,*left;
}TNODE;
Output
OPERATIONS ---
1 - Insert an element into tree
2 - Delete an element from the tree
3 - Inorder Traversal
4 - Preorder Traversal
5 - Postorder Traversal
6 - Exit
Result
Thus, the ‘C’ program to implement Binary search Tree has been executed and the output is verified
successfully.
Ex NO: 9
Aim
Algorithm
Program
#include<stdio.h>
void quicksort(int number[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main()
{
int i, count, number[25];
printf("Enter some elements (Max. - 25): ");
scanf("%d",&count);
printf("Enter %d elements: ",
count); for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("The Sorted Order is: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Output
Result
Thus the ‘C’ program to implement Quick sort has been executed and the output is verified successfully.
Ex NO: 10
Aim
To write a C program to implement Hashing using Linear and Quadratic Probing.
Algorithm
Step 1: Create a structure, data (hash table item) with key and value as data.
Step 2: Now create an array of structure, data of some certain size. But, the size of array must be
immediately updated to a prime number just greater than initial array
Step 3: A menu is displayed on the screen.
Step 4: User must choose one option from four choices given in the menu
Step 5: Perform all the operations
Step 6: Stop the program
Program
#include <stdio.h>
#include <conio.h>
int tsize;
int hasht(int key)
{
int i ;
i = key%tsize ;
return i;
}
//-------LINEAR PROBING-------
int rehashl(int key)
{
int i ;
i = (key+1)%tsize ;
return i ;
}
//-------QUADRATIC PROBING-------
int rehashq(int key, int j)
{
int i ;
i = (key+(j*j))%tsize ;
return i ; }
void main()
{
int key,arr[20],hash[20],i,n,s,op,j,k ;
clrscr() ;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);
printf ("\nEnter the number of elements: ");
scanf ("%d",&n);
for (i=0;i<tsize;i++)
hash[i]=-1 ;
printf ("Enter Elements: ");
for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
do
{
printf("\n\n1.Linear Probing\n2.Quadratic Probing \n3.Exit \nEnter your option: ");
scanf("%d",&op);
switch(op)
{
case 1:
for (i=0;i<tsize;i+
+) hash[i]=-1 ;
for(k=0;k<n;k++)
{
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashl(i);
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;
case 2:
for (i=0;i<tsize;i+
+) hash[i]=-1 ;
for(k=0;k<n;k++)
{
j=1;
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashq(i,j);
j++ ;
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;
}
}while(op!=3);
getch() ; }
Output
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 1
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 2
Result
Thus the ‘C’ program to implement Hashing using Linear and Quadratic Probing has been executed and the
output is verified successfully.
####################################################################################