0% found this document useful (0 votes)
20 views

DS LAB Programs

The document contains 5 programs: 1) inserting and removing elements from arrays, 2) stack operations using an array, 3) prefix conversion of infix expressions, 4) queue implementation using linked lists, 5) linear and binary search algorithms. Each program is explained with code snippets and sample outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

DS LAB Programs

The document contains 5 programs: 1) inserting and removing elements from arrays, 2) stack operations using an array, 3) prefix conversion of infix expressions, 4) queue implementation using linked lists, 5) linear and binary search algorithms. Each program is explained with code snippets and sample outputs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

1.

Program:-

1a) Program: Inserting an element in the array

#include<stdio.h>
#include<conio.h>
#define max 50
void main()
{
int arr[max],size;
int element, pos, i;

printf("Enter Array size:");


scanf("%d",&size);
printf("enter %d elements into Array:",size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);

printf("Enter position and element:\n");


scanf("%d%d",&pos,&element);

if(pos <= size && pos > 0)


{

for(i = size-1; i >= pos-1; i--)


arr[i+1] = arr[i];

arr[pos-1] = element;
size++;
printf(“Array after new element:\n”);
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
}
else
printf("Invalid Position\n");

getch();
}
Output:-
Enter Array size: 5

Enter 5 elements into Array: 87 55 34 23 12

Enter position and element:

99

Array after new element:

87 55 34 99 23 12
1b) Program: Remove an element in the array

#include<stdio.h>
#include<conio.h>
#define size 5
void main()
{
int arr[size] = {1, 20, 5, 78, 30};
int key, i, index = -1;
printf("Enter element to delete\n");
scanf("%d",&key);
for(i = 0; i < size; i++)
{
if(arr[i] == key)
{
index = i;
break;
}
}

if(index != -1)
{
for(i = index; i < size - 1; i++)
arr[i] = arr[i+1];

printf("New Array : ");


for(i = 0; i < size - 1; i++)
printf("%d ",arr[i]);
}
else
printf("Element Not Found\n");

getch();
}
Output:-

Enter element to delete

78

New Array: 1 20 5 30
2. Program:-

#include <stdio.h>
#include <conio.h>
#define MAX 3
int st[MAX], top=-1;
void push(int st[], int val);
int pop(int st[]);
void display(int st[]);
void main()
{
int val, option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
push(st, val);
break;
case 2:
val = pop(st);
if(val != -1)
printf("\n The value deleted from stack is: %d", val);
break;
case 3:
display(st);
break;
}
}while(option != 4);
getch();
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top] = val;
}
}
int pop(int st[])
{
int val;
if(top == -1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val = st[top];
top--;
return val;
}
}
void display(int st[])
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
printf("\n");
}
}
Output:-
*****MAIN MENU*****

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 1

Enter the number to be pushed on stack: 5

*****MAIN MENU*****

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 1

Enter the number to be pushed on stack: 9

*****MAIN MENU*****

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 1

Enter the number to be pushed on stack: 3

*****MAIN MENU*****
1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 3

*****MAIN MENU*****

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 2

The value deleted from stack is: 3

*****MAIN MENU*****

1. PUSH

2. POP

3. DISPLAY

4. EXIT

Enter your option: 4


3. Program:-

#include<stdio.h>
#include<conio.h>
#define SIZE 100
char stack[SIZE];
int top = -1;
void push(char item)
{
if(top >= SIZE-1)
{
printf("\nStack Overflow.");
}
else
{
top = top+1;
stack[top] = item;
}
}
char pop()
{
char item ;
if(top <0)
{
printf("stack under flow: invalid infix
expression");
exit(1);
}
else
{
item = stack[top];
top = top-1;
return(item);
}
return 'a';
}
int is_operator(char symbol)
{
if(symbol == '^' || symbol == '*' || symbol ==
'/' || symbol == '+' || symbol =='-')
{
return 1;
}
else
{
return 0;
}
}
int precedence(char symbol)
{
if(symbol == '^')
{
return(3);
}
else if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
void InfixToPostfix(char infix_exp[], char postfix_exp[])
{
int i, j;
char item;
char x;
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item != '\0')
{
if(item == '(')
{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
postfix_exp[j] = item;
j++;
}
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 &&
precedence(x)>= precedence(item))
{
postfix_exp[j] = x;
j++;
x = pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("\nInvalid infix Expression.\n");
exit(1);
}
i++;
item = infix_exp[i];
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
exit(1);
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
exit(1);
}
postfix_exp[j] = '\0';
}
void main()
{
char infix[SIZE], postfix[SIZE];
clrscr();
printf("ASSUMPTION: The infix expression
contains single letter variables and single digit constants
only.\n");
printf("\nEnter Infix expression : ");
gets(infix);
InfixToPostfix(infix,postfix);
printf("Postfix Expression: ");
puts(postfix);
getch();
}
Output:-
ASSUMPTION: The infix expression contains single letter
variables and single digit constants only.

Enter Infix expression: A+B-C*D/E

Postfix Expression: AB+CD*E/-


4. Program:-
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct queue
{
struct node *front;
struct node *rear;
};
struct queue *q;
void create_queue(struct queue *);
struct queue *insert(struct queue *,int);
struct queue *delete_element(struct queue *);
struct queue *display(struct queue *);
int peek(struct queue *);
int main()
{
int val, option;
create_queue(q);
clrscr();
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. INSERT");
printf("\n 2. DELETE");
printf("\n 3. PEEK");
printf("\n 4. DISPLAY");
printf("\n 5. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the number to insert in the queue:");
scanf("%d", &val);
q = insert(q,val);
break;
case 2:
q = delete_element(q);
break;
case 3:
val = peek(q);
if(val! = –1)
printf("\n The value at front of queue is : %d", val);
break;
case 4:
q = display(q);
break;
}
}while(option != 5);
getch();
return 0;
}
void create_queue(struct queue *q)
{
q -> rear = NULL;
q -> front = NULL;
}
struct queue *insert(struct queue *q,int val)
{
struct node *ptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr -> data = val;
if(q -> front == NULL)
{
q -> front = ptr;
q -> rear = ptr;
q -> front -> next = q -> rear -> next = NULL;
}
else
{
q -> rear -> next = ptr;
q -> rear = ptr;
q -> rear -> next = NULL;
} return q;
}
struct queue *display(struct queue *q)
{
struct node *ptr;
ptr = q -> front;
if(ptr == NULL)
printf("\n QUEUE IS EMPTY");
else
{
printf("\n");
while(ptr!=q -> rear)
{
printf("%d\t", ptr -> data);
ptr = ptr -> next;
}
printf("%d\t", ptr -> data);
}
return q;
}
struct queue *delete_element(struct queue *q)
{
struct node *ptr;
ptr = q -> front;
if(q -> front == NULL)
printf("\n UNDERFLOW");
else
{
q -> front = q -> front -> next;
printf("\n The value being deleted is : %d", ptr -> data);
free(ptr);
}
return q;
}
int peek(struct queue *q)
{
if(q->front==NULL)
{
printf("\n QUEUE IS EMPTY");
return –1;
}
else
return q->front->data;
}
Output:-
*****MAIN MENU*****
1. INSERT
2. DELETE
3. PEEK
4. DISPLAY
5. EXIT
Enter your option: 3
QUEUE IS EMPTY
Enter your option: 5
5. Program:-
5A. Linear search:-
#include<stdio.h>

void main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);

printf("Enter array elements:\n");


for(i=0;i<n;++i)
scanf("%d",&a[i]);

printf("\nEnter element to search:");


scanf("%d",&x);

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");

getch();
}
Output:-

How many elements? 6

Enter array elements:

Enter element to search: 3

Element found at index 3


5B.Binary search:-
#include <stdio.h>
#include<conio.h>
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;

if (arr[mid] == x)
return mid;

if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);

return binarySearch(arr, mid + 1, r, x);


}

return -1;
}

void main()
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x,result;
clrscr();
printf("Enter a element u want to search:");
scanf("%d",&x);
result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present in
array") : printf("Element is present at index %d", result);
getch();
}
Output:-
Enter a element u want to search: 10

Element is present at index 3


6. Program:-
#include <stdio.h>
#include <conio.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// preorderTraversal traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// postorderTraversal traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
// Create a new Node
struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
// Insert on the right of the node
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}
void main() {
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);

insertLeft(root->left, 5);
insertRight(root->left, 6);

printf("Inorder traversal \n");


inorderTraversal(root);

printf("\nPreorder traversal \n");


preorderTraversal(root);

printf("\nPostorder traversal \n");


postorderTraversal(root);

getch();
}
Output:-
Inorder traversal
5->12->6->1->9->
Preorder traversal
1->12->5->6->9->
Postorder traversal
5->6->12->9->1->
7. Program:-

#include <stdio.h>
void bubble_sort(int a[], int n) {
int i = 0, j = 0, tmp;
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (a[j] > a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
void main() {
int a[100], n, i, d, swap;
printf("Enter number of elements in the array:\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
bubble_sort(a, n);
printf("Printing the sorted array:\n");
for (i = 0; i < n; i++)
printf("%d\n", a[i]);
getch();
}
Output:-
Enter number of elements in the array:
5
Enter 5 integers
8
6
4
2
9

Printing the sorted array:


2
4
6
8
9
8. Program:-
#include <math.h>
#include <stdio.h>

void insertionSort(int arr[], int n)


{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

void printArray(int arr[], int n)


{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("original elements Before sorting:");
printArray(arr,n);
insertionSort(arr, n);
printf("After insertion Sort:");
printArray(arr, n);

return 0;
}
Output:-

Original elements before sorting: 12 11 13 5 6

After insertion Sort: 5 6 11 12 13


9. Program:-

#include <stdio.h>
#define MAX 5
void depth_first_search(int adj[ ][MAX],int visited[ ],int
start)
{
int stack[MAX];
int top = –1, i;
printf("%c–>",start + 65);
visited[start] = 1;
stack[++top] = start;
while(top ! = –1)
{
start = stack[top];
for(i = 0; i < MAX; i++)
{
if(adj[start][i] && visited[i] == 0)
{
stack[++top] = i;
printf("%c–>", i + 65);
visited[i] = 1;
break;
}
}
if(i == MAX)
top––;
}
}
int main()
{
int adj[MAX][MAX];
int visited[MAX] = {0}, i, j;
printf("\n Enter the adjacency matrix: ");
for(i = 0; i < MAX; i++)
for(j = 0; j < MAX; j++)
scanf("%d", &adj[i][j]);
printf("DFS Traversal: ");
depth_first_search(adj,visited,0);
printf("\n");
return 0;
}
Output:-

Enter the adjacency matrix:


0 1 0 1 0
1 0 1 1 0
0 1 0 0 1
1 1 0 0 1
0 0 1 1 0
DFS Traversal:A->B->C->E->D->
10. Program:-

#include <stdio.h>
#define MAX 5
void breadth_first_search(int adj[][MAX],int visited[],int
start)
{
int queue[MAX],rear = -1,front =-1, i;
queue[++rear] = start;
visited[start] = 1;
while(rear != front)
{
start = queue[++front];
printf("%c \t",start + 65);
for(i = 0; i < MAX; i++)
{
if(adj[start][i] == 1 && visited[i] == 0)
{
queue[++rear] = i;
visited[i] = 1;
}
}
}
}
void main()
{
int visited[MAX] = {0};
int adj[MAX][MAX], i, j;
clrscr();
printf("\n Enter the adjacency matrix: ");
for(i = 0; i < MAX; i++)
for(j = 0; j < MAX; j++)
scanf("%d", &adj[i][j]);
printf(“Breadth First traversal:\n”);
breadth_first_search(adj,visited,0);
getch();
}
Output:-

Enter the adjacency matrix:

1 0 1 0 1

1 1 1 1 0

0 0 1 0 1

0 0 0 0 1

1 1 1 1 1

Breadth First traversal:

A C E B D

You might also like