Dsa Lab
Dsa Lab
III SEMESTER
Computer Science & Engineering
Department
Laboratory Manual
2| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
INSTRUCTIONS TO STUDENTS
1. The main objective of the DSA laboratory is: Learning through the Experimentation. All
the experiments are designed to illustrate various problems in different areas of DSA and
also to expose the students to various problems and their uses.
2. Be prompt in arriving to the laboratory and always come well prepared for the practical.
3. Every student should have his/her individual copy of the DSA Practical Book.
4. Every student have to prepare the notebooks specifically reserved for the DSA practical
work: ”DSA Practical Book”
5. Every student has to necessarily bring his/her DSA Practical Book, DSA Practical Class
Notebook and DSA Practical Final Notebook.
6. Finally find the output of the experiments along with the problem and note results in the
DSA Practical Notebook.
7. The grades for the DSA practical course work will be awarded based on our performance
in the laboratory, regularity, recording of experiments in the DSA Practical Final
Notebook, lab quiz, regular viva-voce and end-term examination.
3| Page
CERTIFICATE
Head Of Department:...........................................
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
TABLE OF CONTENT
Page No
Sr. Date of Date of Marks
Experiment Title Sign
No Start Completion (out of 10)
From To
1. Write a program to implement
1 (a) linear Search (b) Binary
Search
Write a program to implement
(a) Bubble Sort
2.
(b) Insertion Sort
(c) Selection Sort
Implement a program to
3. convert infix notation to postfix
notation using stack.
Write a program to
implement QUEUE using
arrays that performs
4. following operations
(a)INSERT
(b) DELETE
(c) DISPLAY
Write a menu driven program to
implement following operations
on the singly linked list and
5. doubly linked list.
(a) Insert a node at the specified
position
(b) Delete a first node of the
linked list.
2| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
3| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
PRACTICAL NO: 1
Aim: - Write a program to implement (a) linear Search (b) Binary Search
end procedure
Program Code:
#include <stdio.h>
int main(void)
{
int arr[] = { 10, 20, 30, 40, 50, 100, 0 };
int key = 100;
int size = 10;
int res = LINEAR_SEARCH(arr, size, key);
if (res == -1)
4| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
return 0;
}
Output Obtained:
5| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
Binary Algorithm :
Step 1: Start
Step 2: Input Sorted array in "a[]" and element to be searched in "x" and size of array in "size"
Step 3: Initialize low=0, high=size-1
Step 4: Repeat until low>=high
Step 4.1: mid=(low+high)/2
Step 4.2: If a[mid] is equal to x,
then, print index value of mid and Goto step 6
Else
If a[mid]<x
low=mid+1
else
high=mid-1
Step 5: Print x not found in the list
Stop 6: Stop
Source Code:
6| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? printf("Element is not present in array")
: printf("Element is present at index %d", result);
return 0;
}
7| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
PRACTICAL NO: 2
Aim:- Write a program to implement (a) Bubble Sort (b) Insertion Sort (c) Selection Sort
loop = list.count;
end for
end for
8| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
Source code:
9| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
10| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
i?1
while i < length(A)
j?i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
j?j-1
end while
i?i+1
end while
Source Code:
11| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
12| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
for i = 1 to n - 1
/* set current element as minimum*/
min = i
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
end procedure
Source Code:
13| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
14| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
PRACTICAL NO – 3
Aim :~ Implement a program to convert infix notation to postfix notation using stack.
• Infix Notation:
We write expression in infix notation, e.g. a - b + c, where operators are used in-between
operands. It is easy for us humans to read, write, and speak in infix notation but the same does
not go well with computing devices. An algorithm to process infix notation could be difficult and
costly in terms of time and space consumption.
• Postfix Notation:
This notation style is known as Reversed Polish Notation. In this notation style, the operator is
postfixed to the operands i.e., the operator is written after the operands. For example, ab+. This is
equivalent to its infix notation a + b.
15| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
Program to convert :~
// Stack type
struct Stack
{
int top;
unsigned capacity;
int* array;
};
// Stack Operations
struct Stack* createStack( unsigned capacity )
{
struct Stack* stack = (struct Stack*)
malloc(sizeof(struct Stack));
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
return stack;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1 ;
16| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
}
char peek(struct Stack* stack)
{
return stack->array[stack->top];
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
}
void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
17| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
18| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
exp[++k] = pop(stack );
exp[++k] = '\0';
printf( "%s", exp );
}
Output:
PRACTICAL NO – 4
Aim :~ Write a program to implement QUEUE using arrays that performs following operations
(a)INSERT (b) DELETE (c) DISPLAY
Source Code:
#include<stdlib.h>
#include<stdio.h>
#define max 5
int front=-1,rear=-1; // global variable
int CQueue[max];
void insert();
int remove();
void display();
int main()
{
int w,no;
for(;;)
{
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. EXIT");
printf("\nEnter what you want :");
scanf("%d",&w);
switch(w)
{
case 1:
insert();
break;
case 2:
no=remove();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("\nInvalid Choice !!\n");
}
}
20| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
}
void insert()
{
int no;
if((front ==0 && rear == max-1) || front == rear+1)
{
printf("\nCircular Queue Is Full !\n");
return;
}
printf("\nEnter a number to Insert :");
scanf("%d",&no);
if(front==-1)
front=front+1;
if(rear==max-1)
rear=0;
else rear=rear+1;
CQueue[rear]=no;
}
int remove()
{
int e;
if(front==-1)
{
printf("\nThe Circular Queue is Empty !!\n");
}
e=CQueue[front];
if(front==max-1)
front=0;
else if(front==rear)
{
front=-1;
rear=-1;
}
else front=front+1;
printf("\n%d was deleted !\n",e);
return e;
}
void display()
{
int i;
if(front==-1)
{
printf("\nThe Circular Queue is Empty ! Nothing To Display !!\n");
return;
}
i=front;
if(front<=rear)
{
21| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
printf("\n\n");
while(i<=rear)
printf("%d ",CQueue[i++]);
printf("\n");
}
else
{
printf("\n\n");
while(i<=max-1)
printf("%d ",CQueue[i++]) ;
i=0;
while(i<=rear)
printf("%d ",CQueue[i++]);
printf("\n");
}
}
22| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
23| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
EXPERIMENT NO.: 5
Aim: Write a program to implement STACK using arrays that performs following operations
A) PUSH
B) POP
C) SHOW
What is STACK?
Stack is a linear data structure which follows a particular order in
which the operations are performed. The order may be LIFO (Last In
First Out) or FILO(First In Last Out).
Queue Operations Pseudo Code:
Input Used:
Pushed:[45,58,25]
Program Code:
#include<stdio.h>
#include<stdlib.h>
#define Size 4
int main()
{
int choice;
24| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
while(1)
{
printf("\nOperations performed by Stack");
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
printf("\n\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: show();
break;
case 4: exit(0);
void Push()
{
int x;
if(Top==Size-1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter element to be inserted to the stack:");
scanf("%d",&x);
Top=Top+1;
inp_array[Top]=x;
}
}
void Pop()
{
if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d",inp_array[Top]);
Top=Top-1;
25| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
}
}
void show()
{
if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nElements present in the stack: \n");
for(int i=Top;i>=0;--i)
printf("%d\n",inp_array[i]);
}
}
Output Obtained:
26| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
EXPERIMENT NO : 6
AIM: Write a menu driven program to implement following operations on the singly linked list
and doubly linked list.
#include <stdio.h>
#include <stdlib.h> #include
traversal();
int data;
* H, *T;
int main()
7)
{
printf("\n1.insert a node at starting position"); printf("\
27| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
scanf("%d", &ch);
switch (ch)
case 1: insert_beg();
break;
case 2:
insert_end(); break;
case 3: delete_beg();
break;
case 4: delete_end();
break;
case 5:
break;
case 6:
traversal();
break;
case 7:
break;
default:
printf("invalid");
}
28| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
getch();
void insert_beg()
{
struct node *newnode;
int x;
newnode = (struct node *)malloc(sizeof(struct node)); if (newnode ==
NULL)
}
printf("\ninsert data for new node"); scanf("%d",
&x);
newnode->data = x;
newnode->next = H; if (H ==
NULL)
{
T = newnode;
}
H = newnode;
void traversal()
}
}
29| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
void insert_end()
{
struct node *newnode;
int x;
newnode = (struct node *)malloc(sizeof(struct node)); if (newnode ==
NULL)
{
printf("\nNode not created"); return;
}
printf("\ninsert data for new node"); scanf("%d",
&x);
newnode->data = x;
newnode->next = NULL;
if (H == NULL)
{
H = newnode;
T = newnode;
} else
{
T->next = newnode;
T = newnode;
}
}
void insert_AGN(int info)
{
struct node *newnode, *temp;
int x;
newnode = (struct node *)malloc(sizeof(struct node)); if (newnode ==
NULL)
{
printf("\nNode not created"); return;
}
temp = H;
while (temp->data != info && temp != NULL)
{
temp = temp->next;
}
if (temp == NULL)
{
30| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
}
printf("\ninsert data for new node"); scanf("%d",
&x);
newnode->data = x;
if (temp == T)
{
newnode->next = NULL;
T->next = newnode;
T = newnode;
} else
{
newnode->next = temp->next;
temp->next = newnode;
}
}
void delete_beg()
H;
if (H == NULL)
{
printf("\nBlank LIST"); return;
}
if (H->next == NULL)
{
T = NULL;
}
H = H->next;
free(temp);
void delete_end()
31| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
if (H == NULL)
{
printf("BLANK LIST");
return;
}
if (H->next == NULL)
{
H = NULL;
T = NULL;
else
curr = H;
while (curr->next != T)
curr = curr->next;
}
curr->next = NULL;
} free(temp);
OUTPUT:
32| Page
COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING & TECHNOLOGY
DSA (203105306) B. Tech. 3rd SEM
ENROLLMENT NO:210303105563
33| Page