DS LAB PROGRAMS.RK
DS LAB PROGRAMS.RK
3. Develop a menu driven Program in C for the following operations on STACK of Integers (Array
Implementation of Stack with maximum size MAX)
a) Push an Element on to Stack
b) Pop an Element from Stack
c) Demonstrate how Stack can be used to check Palindrome
d) Demonstrate Overflow and Underflow situations on Stack
e) Display the status of Stack
f) Exit
#include<stdio.h>
#include<stdlib.h>
#define MAX 3
int s[MAX];
palindrome(); void
display();
void main()
while (1)
printf("\n\n\n\n-----------Menu----------- : ");
case 1:
push(item); break;
case 2:
break;
case 3:
palindrome();
if (top == MAX - 1)
printf("\n-----------Stack overflow-----------");
return;
top = top + 1;
s[top] = item;
int pop()
{ int item; if
(top == -1)
{
printf("\n-----------Stack underflow-----------");
return -1;
item = s[top];
top = top - 1;
return item;
void display()
{ int
i;
if (top == -1)
printf("\n-----------Stack is empty-----------");
return;
void palindrome()
content are:\n");
flag = 0;
break;
if (flag == 1)
else
PROGRAM 2
2. Develop a Program in C for the following operations on Strings.
a) Read a main String (STR), a Pattern String (PAT) and a Replace String (REP).
b) Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if
PAT exists in STR. Report suitable messages in case PAT does not exist in STR.
#include<stdio.h>
c = 0, m = 0, i = 0, j = 0, k, flag = 0;
void stringmatch()
if (str[m] == pat[i])
{ i++;
m++; if (pat[i]
== '\0')
flag = 1; for (k = 0;
res[j] = rep[k];
i = 0;
c = m;
else
res[j] = str[c];
j++; c++;
m = c; i = 0;
} res[j] =
'\0';
void main()
stringmatch(); if (flag == 1)
printf("\nThe string after pattern match and replace is: \n %s ", res);
else
PROGRAM 4
4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should
support for both parenthesized and free parenthesized expressions with the operators: +, -, *, /,
(Remainder), ^ (Power) and alphanumeric operands.
#include<stdio.h>#include<stdlib.h>void evaluate();
main()
}void evaluate()
symb = infix[i];
switch (symb)
{ case
'(':
push(symb);
= pop(); while
(temp != '(')
temp = pop();
break;
case '+':
case '%':
case '^':
case '$':
temp = pop();
push(symb);
break; default:
postfix[j] = symb;
j++;
temp = pop();
postfix[j] = '\0';
top = top + 1;
pop()
stack[top]; top =
top - 1; return
item;
{ int
p;
switch (symb)
case '#': p = -
1; break;case
0; break;case
= 1;
break;case '*':
'%': p = 2;
break;case '^':
case '$': p = 3;
break;
return p;
PROGRAM 5 A
5. Develop a Program in C for the following Stack Applications.
a) Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,^.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
postfix[90], symb;
top = top + 1;
s[top] = item;
int pop()
int item;
item = s[top];
top = top - 1;
return item;
void main()
'\0'; i++)
symb = postfix[i];
if (isdigit(symb))
{
push(symb - '0');
else
op2 = pop();
op1 = pop();
switch (symb)
case '+':
push(op1 + op2);
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
push(op1 % op2);
break;
'^':
push(pow(op1, op2));
break;
default:
push(0);
PROGRAM 5 B
5 b) Solving Tower of Hanoi problem with n disks.
#include <stdio.h>
if (n == 0)
return;
source, destination);
void main()
{ int
n;
PROGRAM 6
6. Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX).
a) Insert an Element on to Circular QUEUE
b) Delete an Element from Circular QUEUE
c) Demonstrate Overflow and Underflow situations on Circular QUEUE
d) Display the status of Circular QUEUE
e) Exit
Support the program with appropriate functions for each of the above operations.
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
char circular_queue[MAX];
int isEmpty()
return 1;
else
return 0;
int isFull()
{ if
(isFull())
else if (isEmpty())
front = rear = 0;
else
{
rear = (rear + 1) % MAX;
circular_queue[rear] = element;
void deleteElement()
if (isEmpty())
return;
else
void display()
{ int i; if
(isEmpty())
return;
i = front;
do
{
printf("%c ", circular_queue[i]);
i = (i + 1) % MAX;
printf("\n");
int main()
int choice;
char element;
do
&choice);
switch(choice)
case 1:
insertElement(element);
break; case
2:
deleteElement();
break; case 3:
display(); break;
case 4:
printf("Exiting...\n");
break; default:
while(choice != 4);
return 0;
PROGRAM 8
8. Develop a menu driven Program in C for the following operations on Doubly Linked List (DLL) of
Employee Data with the fields: SSN, Name, Dept, Designation, Sal, PhNo.
a) Create a DLL of N Employees Data by using end insertion.
b) Display the status of DLL and count the number of nodes in it
c) Perform Insertion and Deletion at End of DLL
d) Perform Insertion and Deletion at Front of DLL
e) Demonstrate how this DLL can be used as Double Ended Queue.
f) Exit
#include<stdio.h>
#include<stdlib.h>
struct node
phone; struct
node * rlink;
};
count = 0;
NODE create()
NULL)
exit(0);
scanf("%s %s %s %s %d %ld", enode -> ssn, enode -> name, enode -> dept, enode -> designation, &
enode -> sal, & enode -> phone); enode -> llink = NULL; enode -> rlink = NULL; count++;
return enode;
NODE insertfront()
NODE temp;
temp = create();
if (first == NULL)
return temp;
return temp;
void display()
NODE cur;
int nodeno = 1;
cur = first; if (cur == NULL)
nodeno++;
NODE deletefront()
NODE temp;
if (first == NULL)
return NULL;
printf("\nThe employee node with the ssn:%s is deleted ", first -> ssn);
free(first);
count--; return
NULL;
temp = first; first = first -> rlink; temp -> rlink = NULL; first -> llink
}
NODE insertend()
temp = create();
if (first == NULL)
return temp;
cur = first;
return first;
NODE deleteend()
if (first == NULL)
return NULL;
printf("\nThe employee node with the ssn:%s is deleted ", first -> ssn);
free(first);
count--; return
NULL;
prev = NULL;
cur = first;
prev = cur;
cur -> llink = NULL; printf("\nThe employee node with the ssn:%s is
-; return first;
void deqdemo()
{ int ch;
while (1)
switch (ch)
{
case 1: first =
insertfront();
break;
case 2: first =
deletefront();
break; case 3:
first = insertend();
break; case 4:
first = deleteend();
break; case 5:
display(); break;
default: return;
void main()
{ int ch, i,
n; while
(1)
printf("\n\n--------Menu--------");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront");
printf("\n6:DeleteAtFront"); printf("\n7:Double
case 1:
case 2:
display();
break;
case 3: first =
insertend();
break;
case 4: first =
deleteend();
break;
case 5: first =
insertfront();
break;
case 6:
first = deletefront();
break;
case 7:
deqdemo();
break;
case 8:
exit(0);
default:
PROGRAM 10
10. Design, Develop and Implement a menu driven Program in C for the following operations on
Binary Search Tree (BST) of Integers:
a) Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2.
b) Traverse the BST in Inorder, Preorder and Post Order.
c) Search the BST for a given element. (KEY) and report the appropriate message.
d) Exit
#include<stdio.h>
#include<stdlib.h>
struct BST
BST * rchild;
};
NODE create()
return temp;
search(NODE root);
newnode);
newnode);
{
int key; NODE
cur; if (root ==
NULL)
printf("\nBST is empty.");
return;
(cur != NULL)
return;
rchild;
if (root != NULL)
}
void preorder(NODE root)
if (root != NULL)
-> rchild);
if (root != NULL)
void main()
printf("\n-------BST MENU-------");
printf("\n2.Search ");
printf("\n4.Exit"); printf("\nEnter
case 1:
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
break;
case 2:
if (root == NULL)
else
preorder(root); printf("\nThe
postorder(root);
break;
case 3:
search(root);
break;
case 4:
exit(0);
}
}
PROGRAM 11
11. Develop a Program in C for the following operations on Graph(G) of Cities.
a) Create a Graph of N cities using Adjacency Matrix.
b) Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method.
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
void bfs(int v)
{ int i, cur;
visited[v] = 1;
q[++rear] = v; while
(front != rear) {
cur = q[++front];
q[++rear] = i;
visited[i] = 1;
}
void dfs(int v)
{ int
i;
visited[v] = 1;
s[++top] = v; for (i =
1; i <= n; i++)
dfs(i);
int main()
matrix:\n");
visited[i] = 0;
printf("\n==>2. DFS: Print all nodes reachable from a given starting node");
switch (ch)
case 1:
bfs(start); for (i
= 1; i <= n; i++)
{ if
(visited[i] == 0)
break;
case 2:
dfs(start);
break; case
3: exit(0);
default:
PROGRAM 12
12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine the
records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m memory
locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys in K and
addresses in L are Integers. Develop a Program in C that uses Hash function H:K →L as H(K)=K mod m
(remainder method), and implement hashing technique to map a given key K to the address space L.
Resolve the collision (if any) using linear probing.
#include<stdio.h>
#include<stdlib.h>
int key[20], n, m;
count = 0;
index = key % m;
index = (index + 1) % m;
ht[index] = key;
count++;
void display()
{ int
i;
if (count == 0)
return;
}
void main()
{ int
i;
printf("\nEnter the two digit memory locations (m) for hash table: ");
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
if (count == m)
break;
insert(key[i]);
display();