Ds Lab Manual
Ds Lab Manual
1. Given {4,7,3,2,1,7,9,0} find the location of 7 using Linear and Binary search and also display its first occurrence
//Given {4,7,3,2,1,7,9,0} find the location of 7 using
// Linear search and Binary search and also display its
// first occurance
# include<stdio.h>
# include<conio.h>
case 2 : {
DISPLAY_ARRAY(sa,n);
BINARY_SEARCH(sa,key,0,n-1);
break;
}
default : printf("\n Invalid choice ");
}
getch();
}
OUTPUT :
LINEAR SEARCH :
BINARY SEARCH
2. Given {5,3,1,6,0,2,4} order the numbers in ascending order using Bubble Sort Algorithm
// C program to perform Bubble Sort
# include<stdio.h>
# include<conio.h>
void BUBBLE_SORT(int a[], int n)
{
int pass,temp,j,i;
for(pass=1;pass<=n-1;pass++)
{
for(j=0;j<=n-pass-1;j++)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\n Array after %d pass --->",pass);
for(i=0;i<n;i++)
printf("%3d",a[i]);
}
void main()
{
int a[7]={5,3,1,6,0,2,4};
int n=7;
clrscr();
printf("\n Input arrays : 5 3 1 6 0 2 4");
BUBBLE_SORT(a,n);
getch();
}
OUTPUT :
3(a). Perform the Insertion sort on the input {75,8,1,16,48,3,7,0} and display the output in descending order.
// C program to perform Insertion sort
# include<stdio.h>
# include<conio.h>
// Insertion sort
void main()
{
int a[8]={75,8,1,16,48,3,7,0};
int n=8;
clrscr();
printf("\n Input arrays : 75,8,1,16,48,3,7,0");
INSERTION_SORT(a,n);
getch();
}
OUTPUT : INSERTION SORT
3(b). Perform the Selection sort on the input {75,8,1,16,48,3,7,0} and display the output in descending order.
// C program to perform Selection sort
# include<stdio.h>
# include<conio.h>
// Selection sort
int MIN(int a[],int k, int n)
{
int loc,j,min;
min=a[k];
loc=k;
for(j=k+1;j<=n-1;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
return(loc);
}
NODE *header=NULL;
void DISPLAY()
{
NODE *start=header;
printf("\n *** LIST *** : ");
while(start!=NULL)
{
printf("%4d",start->info);
start=start->link;
}
}
if(header==NULL)
header=newnode;
else
{
curptr=header;
while(curptr->link !=NULL)
{
curptr=curptr->link;
}
curptr->link=newnode;
}
DISPLAY();
}
void DELETE(int item)
{
NODE *curptr=header, *prevptr=header;
if(header==NULL)
{
printf("\n EMPTY LIST");
}
else if(header->info==item)
{
header=header->link;
free(curptr);
}
else
{
while(curptr!=NULL)
{
if(curptr->info==item)
{
prevptr->link=curptr->link;
free(curptr);
curptr=curptr->link->link;
}
else
{
prevptr=curptr;
curptr=curptr->link;
}
}
}
DISPLAY();
}
void main()
{
int item,choice;
clrscr();
printf("\n Insertion :");
INSERT(61);
INSERT(16);
INSERT(8);
INSERT(27);
printf("\n Deletion :");
DELETE(8);
DELETE(61);
DELETE(27);
getch();
}
}
5. Write a program to insert the elements {61,16,8,27} into linear queue and delete three elements from the list.
Display your list after each insertion and deletion.
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void display()
{
int i;
if (front == - 1)
printf("\nQueue is empty \n");
else
{
printf("\nQueue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
}
getch();
}
void delete_Q()
{
if (front == - 1 || front > rear)
{
printf("\nQueue Underflow ");
return ;
}
else
{
printf("\nElement deleted from queue is : %d", queue_array[front]);
front = front + 1;
}
display();
}
void main()
{
int choice;
clrscr();
insert_Q(61);
insert_Q(16);
insert_Q(8);
insert_Q(27);
delete_Q();
delete_Q();
delete_Q();
getch();
}
6. Write a program to insert the elements {61,16,8,27} into circular queue and delete 4 elements from the list.
Display your list after each insertion and deletion
/*static circular queue*/
#include <stdio.h>
#define size 4
int front = - 1;
int rear = - 1;
int queue[size];
void display_CQ()
{
int i;
printf("\n Circular Queue : ");
if (front > rear)
{
for (i = front; i < size; i++)
{
printf("%d ->", queue[i]);
}
for (i = 0; i <= rear; i++)
printf("%d -> ", queue[i]);
}
else
{
for (i = front; i <= rear; i++)
printf("%d ->", queue[i]);
}
printf("[%d]",queue[front]);
getch();
}
void delete_CQ()
{
if (front == - 1)
{
printf("Queue is empty ");
}
else if (front == rear)
{
front = - 1;
rear = - 1;
}
else
{
front++;
}
display_CQ();
}
int main()
{
clrscr();
printf("\n *** Insertion *** : ");
insert_CQ(61);
insert_CQ(16);
insert_CQ(8);
insert_CQ(27);
printf("\n *** Deletion *** : ");
delete_CQ();
delete_CQ();
delete_CQ();
delete_CQ();
OUTPUT :
7. Write a program to insert the elements {61,16,8,27} into ordered singly linked list and delete 8,61,27 from the
list. Display your list after each insertion and deletion
# include<stdio.h>
# include<conio.h>
NODE *header;
void CREATE_HEADER()
{
header = (NODE *) malloc(sizeof(NODE));
header->info=0;
header->link=NULL;
}
if(header->link==NULL)
{
header->link=NEWNODE;
}
else if(item < header->info)
{
NEWNODE->link=header;
header=NEWNODE;
}
else
{
PREVPTR=header;
CURPTR=header->link;
while(CURPTR!=NULL && item > CURPTR->info)
{
PREVPTR=CURPTR;
CURPTR=CURPTR->link;
}
PREVPTR->link=NEWNODE;
NEWNODE->link=CURPTR;
}
void DISPLAY_NODE()
{
NODE *CURPTR;
CURPTR=header->link;
printf("\n LIST : ");
while(CURPTR!=NULL)
{
printf("%d->",CURPTR->info);
CURPTR=CURPTR->link;
}
}
void main()
{
clrscr();
CREATE_HEADER();
printf("\n *** INSERTING 61,16,8,27 : ***");
INSERT_ORDERLIST(61);
DISPLAY_NODE();
INSERT_ORDERLIST(16);
DISPLAY_NODE();
INSERT_ORDERLIST(8);
DISPLAY_NODE();
INSERT_ORDERLIST(27);
DISPLAY_NODE();
# include<stdio.h>
# include<conio.h>
# define MAX 5
int STACK[MAX];
int TOP=-1;
void DISPLAY();
void PUSH(int item)
{
if(TOP==MAX-1)
{
printf("\n STACK Overflow");
}
else
{
printf("\n ** PUSH %d **",item);
TOP=TOP+1;
STACK[TOP]=item;
DISPLAY();
}
}
void POP()
{
if(TOP==-1)
{
printf("\n STACK Underflow");
getch();
}
else
{
printf("\n ** %d POPED **",STACK[TOP]);
TOP=TOP-1;
DISPLAY();
}
}
void DISPLAY()
{
int i;
for(i=TOP;i>=0;i--)
{
printf("\n STACK[%d]=%d",i,STACK[i]);
}
getch();
}
void main()
{
int i;
clrscr();
printf("\n PUSH 5,9,34,17,32\n");
PUSH(5);
PUSH(9);
PUSH(34);
PUSH(17);
PUSH(32);
printf("\n POP 3 elements\n");
POP();
POP();
POP();
}
10. Write a recursive program to find GCD of 4,6,8.
// C program to find GCD (4,6,8) using recursion.
# include<stdio.h>
# include<conio.h>
#include <stdio.h>
#include <ctype.h>
#define SIZE 50
char stack[SIZE];
int top=-1;
push(char elem)
{
stack[++top]=elem;
}
char pop()
{
return(stack[top--]);
}
if(symbol == '^')
{
return(3);
}
else if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}
void main()
{
char infix[50],postfix[50],ch,elem;
int i=0,k=0;
clrscr();
printf("Enter Infix Expression : ");
scanf("%s",infix);
push('#');
while( (ch=infix[i++]) != '\0')
{
if( ch == '(') push(ch);
else
if(isalnum(ch)) postfix[k++]=ch;
else
if( ch == ')')
{
while( stack[top] != '(')
postfix[k++]=pop();
elem=pop();
}
else
{
while( pr(stack[top]) >= pr(ch) )
postfix[k++]=pop();
push(ch);
}
}
while( stack[top] != '#')
postfix[k++]=pop();
postfix[k]='\0';
printf("\nPostfix Expression = %s\n",postfix);
getch();
}
13. Write a program to evaluate a postfix expression 5 3+8 2 - *.
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
char *postfix;
int A,B,RES,num;
clrscr();
printf("Enter the expression :: ");
scanf("%s",postfix);
while(*postfix != '\0')
{
if(isdigit(*postfix))
{
num = *postfix - 48; // converting char into num
push(num);
}
else
{
A = pop();
B = pop();
switch(*postfix)
{
case '+': RES = B + A; break;
case '-': RES = B - A; break;
case '*': RES = B * A; break;
case '/': RES = B / A; break;
}
push(RES);
}
postfix++;
}
printf("\nThe result of expression = %d\n\n",pop());
getch();
}
15. Write a program to create binary search tree with the elements {2,5,1,3,9,0,6} and perform inorder, preorder
and post order traversal.
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
node *create_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
void main()
{
int a[] = { 9,16,32,8,4,1,5,8,0 };
int n = sizeof(a) / sizeof(a[0]);
clrscr();
printf("\n Before sorting : ");
printArr(a,n);
HEAPSORT(a,n);
printf("\n After sorting : ");
printArr(a,n);
getch();
}
17. Given S1={“Flowers”} ; S2={“are beautiful”} I. Find the length of S1 II. Concatenate S1 and S2 III. Extract the
substring “low” from S1 IV. Find “are” in S2 and replace it with “is”
void main()
{
char *S1,*S2;
int len,choice,pos,elen;
while(1)
{
clrscr();
strcpy(S1,"Flowers");
strcpy(S2,"are beautiful");
printf("\n S1 = %s S2 = %s",S1,S2);
printf("\n 1. Length 2.Concatenate 3.Extract Substring 4.REPLACE 5.Exit\n");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : {
len = LENGTH(S1);
printf("\n Length of %s = %d",S1,len);
}break;
case 2: {
CONCAT(S1,S2);
}break;
case 4: {
REPLACE(S2,"are","is",0);
}break;
case 5: exit(0);
default : printf("\n Invalid option");
}
getch();
}
}