Professional Documents
Culture Documents
20mcal16 DS Lab Manual Isem
20mcal16 DS Lab Manual Isem
Note 1: In the practical Examination each student has to pick one question from a lot of all 8 questions.
#include<ctype.h>
char stack[100];
void push(char x)
stack[++top] = x;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
int priority(char x)
if(x == '(')
return 0;
return 1;
return 0;
int main()
char exp[100];
char *e, x;
clrscr();
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0')
if(isalnum(*e))
printf("%c",*e);
push(*e);
printf("%c", x);
else
3
printf("%c",pop());
push(*e);
e++;
while(top != -1)
printf("%c",pop());
getch();
return 0;
OUTPUT :
#include<stdio.h>
int stack[20];
int top = -1;
4
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
clrscr();
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
5
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
getch();
return 0;
}
OUTPUT :
#include <stdio.h>
int queue[MAX_SIZE];
6
int front = -1, rear = -1;
void insert() {
int element;
if (rear == MAX_SIZE - 1) {
return;
scanf("%d", &element);
if (front == -1)
front = 0;
rear++;
queue[rear] = element;
void delete() {
return;
front++;
void display() {
7
printf("Error: Queue is empty\n");
return;
printf("\n");
int main() {
int choice;
while (1) {
printf("Queue Operations\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
8
delete();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
return 0;
OUTPUT:
Queue Operations
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter element to insert: 10
Element inserted successfully
Queue Operations
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter element to insert: 20
Element inserted successfully
9
Queue Operations
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter element to insert: 30
Element inserted successfully
Queue Operations
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
Queue elements: 10 20 30
Queue Operations
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 2
Element deleted successfully
Queue Operations
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 3
Queue elements: 20 30
Queue Operations
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 4
Exiting the program...
10
4. Write a C Program to simulate the working of a singly linked list providing the following
operations a) Display & Insert b) Delete from beginning/end c) Delete a given element
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void beginsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main()
clrscr();
while(choice != 9)
11
printf("\n=======================================================\n");
printf("\n1.Insert in begining \n2.Delete from Beginning\n3.Delete from last\n4.Delete node after specified
location\n5.Search for an element\n6.Show\n7.Exit\n");
scanf("%d",&choice);
switch(choice)
case 1:
beginsert();
break;
case 2:
begin_delete();
break;
case 3:
last_delete();
break;
case 4:
random_delete();
break;
case 5:
search();
break;
case 6:
display();
break;
case 7:
exit(0);
12
break;
default:
getch();
void beginsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
head = ptr;
printf("\nNode inserted");
13
void begin_delete()
if(head== NULL)
printf("\nList is empty\n");
else
ptr= head;
head= ptr->next;
free(ptr);
void last_delete()
if(head== NULL)
printf("\nlist is empty");
14
head = NULL;
free(head);
else
ptr = head;
while(ptr->next!= NULL)
ptr1 = ptr;
ptr1->next = NULL;
free(ptr);
void random_delete()
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
15
{
ptr1 = ptr;
ptr =ptr->next;
if(ptr== NULL)
printf("\nCan't delete");
return;
free(ptr);
void search()
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
16
scanf("%d",&item);
while (ptr!=NULL)
if(ptr->data == item)
flag=0;
else
flag=1;
i++;
if(flag == 1)
void display()
ptr = head;
17
if(ptr == NULL)
printf("Nothing to print");
else
printf("\nprinting values.....\n");
while (ptr!=NULL)
printf("\n%d",ptr->data);
OUTPUT :
18
19
20
21
22
5. Write a C Program to implement the following searching techniques a. Linear Search
b.Binary Search
#include <stdio.h>
#include <stdlib.h>
main()
{
/* Declare variables - array_of_number,search_key.i.j,low,high*/
int array[100],search_key,i,j,n,low,high, location,choice;
void linear_search(int search_key, int array[100], int n);
void binary_search(int search_key, int array[100], int n);
clrscr();
/*read the elements of array */
printf("ENTER THE SIZE OF THE ARRAY:");
scanf("%d",&n);
printf("ENTER THE ELEMENTS OF THE ARRAY:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&array[i]);
}
/* Get the Search Key element for Linear Search */
23
default:
exit(0);
}
getch();
return 0;
}
/* LINEAR SEARCH*/
24
}
else
{
low= mid+1;
high = n;
mid=(low+high)/2;
}
}
printf("______________________________________\n");
printf("location=%d\t",mid);
printf("Search Key=%d Found!\n",search_key);
printf("______________________________________\n");
}
6. Write a C Program to implement the following sorting algorithms using user defined
functions a) Bubble Sort(Ascending order b) selection sort(Descending order)
#include<stdio.h>
#include<stdlib.h>
//------------------Main Function-------------------
25
int main()
int n,choice,i;
int arr[20];
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
while(1)
printf("\nl. Bubble Sort\n2. Selection Sort\n3. Display Array \n4. Exit the Program\n");
scanf("%d",&choice);
switch(choice)
case 1:
bubble_sort(arr,n);
break;
case 2:
selection_sort(arr,n);
break;
26
case 3:
display(arr,n);
break;
case 4:
return 0;
default:
getch();
return 0;
//-----------Display Function-------------------
int i;
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
int i,j,temp;
27
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(arr[j]>arr[j+1])
temp=arr[j];
arr[j]= arr[j+1];
arr[j+1]=temp;
display(arr,n);
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(arr[i]<arr[j])
temp=arr[i];
28
arr[i]=arr[j];
arr[j]=temp;
display(arr,n);
OUTPUT:
29
30
7. Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal's
algorithm ( C programming)
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int from;
int to;
int weight;
} Edge;
Edge edges[MAX_EDGES];
31
int parent[MAX_VERTICES];
int rank[MAX_VERTICES];
parent[vertex] = vertex;
rank[vertex] = 0;
if (vertex != parent[vertex]) {
parent[vertex] = find_set(parent[vertex]);
return parent[vertex];
if (root_x != root_y) {
parent[root_y] = root_x;
} else {
parent[root_x] = root_y;
if (rank[root_x] == rank[root_y]) {
rank[root_y]++;
32
}
void kruskal() {
int i, total_weight = 0;
make_set(i);
if (find_set(edge.from) != find_set(edge.to)) {
union_set(edge.from, edge.to);
total_weight += edge.weight;
int main() {
int i;
33
printf("Enter number of vertices: ");
scanf("%d", &num_vertices);
scanf("%d", &num_edges);
kruskal();
return 0;
OUTPUT
015
0 2 10
038
134
1 4 12
233
256
342
457
34
Minimum Spanning Tree:
(2, 3) -> 3
(3, 4) -> 2
(1, 3) -> 4
(0, 1) -> 5
(2, 5) -> 6
8. From a given vertex in a weighted connected graph, find shortest paths to other
vertices Using Dijkstra's algorithm (C programming)
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct {
int dest;
int weight;
} Node;
typedef struct {
Node* head;
} List;
typedef struct {
int parent;
int distance;
int visited;
List* adj_list;
} Vertex;
35
typedef struct {
Vertex* vertices[MAX_VERTICES];
int num_vertices;
} Graph;
new_node->dest = dest;
new_node->weight = weight;
new_node->next = graph->vertices[src]->adj_list->head;
graph->vertices[src]->adj_list->head = new_node;
int i;
graph->num_vertices = num_vertices;
vertex->parent = -1;
vertex->distance = INT_MAX;
vertex->visited = 0;
vertex->adj_list->head = NULL;
graph->vertices[i] = vertex;
int i;
36
graph->vertices[start]->distance = 0;
int j;
min_distance = graph->vertices[j]->distance;
current_vertex = j;
if (current_vertex == -1) {
break;
graph->vertices[current_vertex]->visited = 1;
if (!graph->vertices[neighbor]->visited) {
graph->vertices[neighbor]->distance = tentative_distance;
graph->vertices[neighbor]->parent = current_vertex;
current_adj_list = current_adj_list->next;
37
}
int main() {
Graph graph;
initialize_graph(&graph, num_vertices);
int start_vertex;
OUTPUT:
012
024
121
133
38
231
242
345
0 0 -1
1 2 0
2 3 1
3 4 2
4 5 2
39
VIVA QUESTIONS
A data structure is a way of organizing and storing data in a computer so that it can be
accessed and manipulated efficiently.
An array is a data structure that stores a fixed-size sequential collection of elements of the
same type, whereas a linked list is a data structure that consists of a sequence of nodes, where
each node contains data and a reference (link) to the next node in the sequence.
3. What is a stack?
A stack is a linear data structure in which elements are added and removed from the same
end, known as the top of the stack. The last element added to the stack is the first one to be
removed, a process known as Last In, First Out (LIFO).
4. What is a queue?
A queue is a linear data structure in which elements are added at the rear end and removed
from the front end, known as the head of the queue. The first element added to the queue is
the first one to be removed, a process known as First In, First Out (FIFO).
A binary tree is a tree data structure in which each node has at most two children, referred to
as the left child and the right child. The left child is typically smaller than the parent, while
the right child is typically larger.
A hash table is a data structure that maps keys to values using a hash function, which takes
the key as input and returns an index into an array of buckets. Each bucket contains a linked
list of key-value pairs that have the same hash value.
40
7. What is the time complexity of searching an element in a binary search tree?
The time complexity of searching an element in a binary search tree is O(log n), where n is
the number of nodes in the tree.
A tree is a connected acyclic graph with a unique node called the root, whereas a graph is a
collection of vertices (nodes) and edges that connect them.
The time complexity of inserting an element into a heap is O(log n), where n is the number
of elements in the heap.
10.What is a trie?
A trie is a tree data structure that stores a collection of strings or sequences, where each node
represents a prefix of a string and the edges represent the characters of the string.
A binary search is an algorithm for finding the position of a target value within a sorted array.
It works by repeatedly dividing the array in half and comparing the middle element to the
target value, until the value is found or the search space is exhausted.
12.What is a heap?
A heap is a data structure that represents a nearly complete binary tree, where each node is
greater than or equal to (or less than or equal to) its children. It can be used to implement
priority queues.
A priority queue is a data structure that allows elements to be inserted and removed in
arbitrary order, but always returns the element with the highest (or lowest) priority first.
41