Data Structures Lab Record Format
Data Structures Lab Record Format
To implement stack
6 operations using linked
Lists
To implement queues
7 using arrays.
To implement binary
8 search trees.
Experiment 1
Aim: To use functions for array handling
Program:
#include <stdio.h>
c = traverse(a);
*(a + b) = value;
int main(){
int arr[10] = {23, 24, 63, 78, 53};
char a;
printf("Enter the operation you want t o perform in the array\n");
printf("Enter 'I' for insertion, 'D' for deletion and 'T' for traversal:");
scanf("%c", &a);
switch (a){
case 'I':
insert(arr);
break;
case 'D':
delete(arr);
break;
case 'T':
traverse(arr);
break;
};
return 0;
};
Experiment 2
Aim: To search an array for the given element using linear (or sequential) and binary search
Programming Assignment: Write a menu driven program in C which searches an array for
the input element.
Pre-Lab Work: Write the linear and binary search algorithms and compute their complexity
Program:
Linear search:-
#include <stdio.h>
int main(){
int size,arr[100],key,i;
for(i=0;i<10;i++){
if((*(arr+i))==key){
printf("Element found at index %d\n",i);};
};
return 0;
};
Binary search:-
#include <stdio.h>
int main(){
int size,arr[100],key,i;
int low=0,high,mid;
scanf("%d", &key);
high=size;
while(low<=high){
mid=(low+high)/2;
if (arr[mid]==key){
printf("Element found at index %d.\n",mid);
break;
}
else if(arr[mid]<key){
low=mid+1;
}
else{
high=mid-1;
}
};
return 0;
};
Output:
Linear search:-
Binary search:-
Experiment 3
Aim: To sort an array using iterative sorting algorithms
Programming Assignment: Write a menu driven program in C which sorts an array using
selection, bubble and insertion sorting algorithms. The expectations from the program are as
follows:-
The output of the program MUST be user friendly and self-explanatory.
The program MUST display the array at the end of each iteration.
Proper test cases MUST be designed to exhibit the best case, average case and worst-
case behavior of the algorithms.
Pre-Lab Work: Write selection, bubble and insertion sorting algorithms and compute their
complexity in best, worst and average cases.
Program:
#include <stdio.h>
int main(){
int ar[5],ans;
swapped = 1;
}
}
PrintArray(arr);
if (swapped == 0)
break;
}
printf("\nsorted array is :- ");
PrintArray(arr);
}
Output:
Experiment 4
Aim: To sort an array using recursive sorting algorithms
#include <stdio.h>
int main(){
int ar[5],ans;
printf("%d\t",arr[i]);
printf("\n");
}
Experiment 5
Aim: To implement singly linked
lists
Programming Assignment: Write a program to implement singly linked lists using the
following functions
1. Create a linked list
2. Insert before a given item in the linked list
3. Delete an item from the linked list
4. Display the linked lit
Note:-
The program should be menu driven. The menu should contain options for create,
insert, delete, display and quit.
Make sure that create function should be called only once.
Functions should be called on the basis of user’s choice till the user enters the choice
to quit.
Pre-Lab Work: Write the algorithms for 2, 3 and 4 functions and compute their complexity.
Program:
#include <stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
}*head,*second,*third,*fourth;
int main(){
head=(struct Node*)malloc(sizeof(struct Node));
second=(struct Node*)malloc(sizeof(struct Node));
third=(struct Node*)malloc(sizeof(struct Node));
fourth=(struct Node*)malloc(sizeof(struct Node));
head->data=29;
head->next=second;
second->data=45;
second->next=third;
third->data=21;
third->next=fourth;
fourth->data=82;
fourth->next=NULL;
char x='y';
while(x=='y'){
printf("The list before any operation is: \n");
print(head);
char a;
printf("Enter the operation to be performed in linklist\n");
printf("Enter T for traversal\n");
printf("Enter I for insertion\n");
printf("Enter D for deletion\n");
scanf("%c",&a);
switch(a){
case'T':
printf("We are initiating traversal.\n");
traverse(head);
break;
case'I':
int b;
printf("We are performing insertion\n");
switch(b){
case 1:
printf("You choose to insert at beginning of list\n");
head=insert_begin(head,i1);
print(head);
break;
case 2:
printf("You choose to insert at specific location of list\n");
printf("Enter the index at which you want to insert the given value\n");
scanf("%d",&indexI);
head=insert_specific(head,i1,indexI);
print(head);
break;
case 3:
printf("You choose to insert at end of list\n");
head=insert_end(head,i1);
print(head);
break;
case'D':
int e;
printf("We are performing deletion\n");
printf("Enter 1 to delete from beginning\n");
printf("Enter 2 to delete from specific\n");
printf("Enter 3 to delete from last\n");
scanf("%d",&e);
int indexD;
switch(e){
case 1:
printf("You choose to delete from beginning of list\n");
head=delete_begin(head);
case 2:
printf("You choose to delete at specific location of list\n");
printf("Enter the index at which you want to delete the given value\n");
scanf("%d",&indexD);
head=delete_specific(head,indexD);
print(head);
break;
case 3:
printf("You choose to delete at end of list\n");
head=delete_end(head);
print(head);
break;
fflush(stdin);
};
printf("\nEnd of program\n");
};
Experiment 6
Aim: To implement stack operations using linked lists
Note:-
The program should be menu driven. The menu should contain options for calling
the functions written by you repeatedly till the option to quit is chosen.
Pre-Lab Work: How is a stack implemented using linked lists?
Program:
#include <stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
}*top;
void main()
{
top=(struct node*)malloc(sizeof(struct node));
top->data=35;
top->next=NULL;
int x=1;
while(x==1){
printf("Choose the operation you want to perform from below:\n");
printf("1.PUSH\n2.POP\n3.Display\n");
printf("Enter here:");
int c;
scanf("%d",&c);
switch(c){
case 3: print(top);
break;
case 2: top=pop(top);
print(top);
break;
case 1: int a;
printf("Enter the number you want to push:");
scanf("%d",&a);
top=push(top,a);
print(top);
break;
default: printf("Invalid Input");
};
fflush(stdin);
scanf("%d",&x);
fflush(stdin);
};
printf("End of program");
};
Note:-
The program should be menu driven. The menu should contain options for calling the
functions written by you repeatedly till the option to quit is chosen.
Pre-Lab Work: How are queues implemented using arrays? What are the conditions for
overflow and underflow?
Program:
#include <stdio.h>
#include<stdlib.h>
int queue[100]={43},front=0,rear=0;
int c;
void enque(){
if(rear==99){
printf("The queue is full\n");
return;
};
int e;
printf("Enter the element to be inserted: ");
scanf("%d",&e);
if(front==-1){
front=0;
rear=0;
queue[rear]=e;
return;
};
rear=rear+1;
queue[rear]=e;
};
void deque(){
if(front==-1){
printf("The queue is empty\n");
return;
};
front=front+1;
};
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS
void display(){
printf("The elements of queue are: ");
for(int i=front;i<=rear;i++){
printf(" %d",queue[i]);
};
printf("\n");
};
int main()
{
printf("The maximum elements the queue can accomodate = 100 \n");
printf("Initiatlly the queue has one element\n");
display();
int x=1;
while(x==1){
printf("The list of operations are given below to be performed\n");
printf("Enter the respective number for below operations\n");
printf("1. Enque\n2. Deque\n\n");
printf("Enter here: ");
scanf("%d",&c);
switch(c){
case 1:
enque();
display();
break;
case 2:
deque();
display();
break;
default:
printf("Invalid input");
break;
};
fflush(stdin);
printf("\nDo you want to repeat any operation?\n");
printf("Type '1' for yes and '0' for no.\n");
scanf("%d",&x);
printf("\n");
fflush(stdin);
};
printf("End of program");
return 0;
};
Note:-
The program should be menu driven. The menu should contain options for calling the
functions written by you repeatedly till the option to quit is chosen.
Pre-Lab Work: What are the cases encountered during deletion of a key from a BST? How are
they handled?
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
int main() {
struct Node* root = NULL;
int choice, data;
while (1) {
printf("\nBinary Search Tree Operations:\n");
printf("1. Insert Element\n");
printf("2. Delete Element\n");
printf("3. Inorder Traversal\n");
printf("4. Preorder Traversal\n");
printf("5. Postorder Traversal\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSS Academy of Technical Education, Noida Department of CSDS
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Enter data to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
break;
case 3:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 4:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 5:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");}}
return 0;}
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Graph {
int numVertices;
struct Node** adjLists;
int* visited;
};
int main() {
struct Graph* graph = createGraph(5);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
printf("Graph:\n");
printGraph(graph);
printf("\nDFS Traversal:\n");
DFS(graph, 0);
// Resetting visited array for BFS
int i;
Data Structures Lab (BCS 351) Sajal Singhal 2200911540092
JSSfor
Academy
(i = 0; i <ofgraph->numVertices;
Technical Education,i++)
Noida
{ Department of CSDS
graph->visited[i] = 0;}
printf("\nBFS Traversal:\n");
BFS(graph, 0);
return 0;
}
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->numVertices = vertices;
graph->adjLists = (struct Node**)malloc(vertices * sizeof(struct Node*));
graph->visited = (int*)malloc(vertices * sizeof(int));
int i;
for (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;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;}
void printGraph(struct Graph* graph) {
int v;
for (v = 0; v < graph->numVertices; v++) {
struct Node* temp = graph->adjLists[v];
printf("Vertex %d: ", v);
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;}
printf("NULL\n");}}
graph->visited[startVertex] = 1;
queue[rear++] = graph->adjLists[startVertex];
while (front < rear) {
struct Node* currentNode = queue[front++];
printf("Visited %d \n", currentNode->data);
while (currentNode) {
int adjVertex = currentNode->data;
if (graph->visited[adjVertex] == 0) {
queue[rear++] = graph->adjLists[adjVertex];
graph->visited[adjVertex] = 1;}
currentNode = currentNode->next;
}}}
Output:
Pre-Lab Work:
1) What is minimum spanning tree?
2) Differentiate between Prim’s and Kruskal’s algorithms
Program:
#include <stdio.h>
#include <limits.h>
#define V 5
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;
}
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;}
Output: