LabManual DS 18MCAL27-2
LabManual DS 18MCAL27-2
#include<stdio.h>
#include<conio.h>
void main()
int a[100],i,n;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
avg(a,n);
getch();
int sum=0,i;
float avg,dev[100];
for(i=0;i<n;i++)
sum=sum+a[i];
avg=sum/n;
for(i=0;i<n;i++)
dev[i]=a[i]-avg;
Output:
#include<stdio.h>
#include<string.h>
/* To find length */
void length()
char str[100];
int l,i;
scanf("%s",str);
for(i=0;str[i]!='\0';i++);
/* To concatinate */
void concat()
char s1[100],s2[100];
int i,j;
scanf("%s",s1);
scanf("%s",&s2);
i=strlen(s1);
for(j=0;s2[j]!='\0';i++,j++)
s1[i]=s2[j];
s1[i]='\0';
/* To find substring */
void substr()
char s1[100],s2[100];
int i,j,k,m;
scanf("%s",s1);
scanf("%d%d",&i,&j);
for(k=i,m=0;m<j;s2[m++]=s1[k++]);
s2[m]='\0';
void main()
int ch;
char ans;
clrscr();
do
scanf("%d",&ch);
switch(ch)
case 1:length();
break;
case 2:concat();
break;
case 3:substr();
break;
scanf("%s",&ans);
}while(ans=='y');
getch();
Output:
Algorithm:
For each symbol in the infix expression
{
While(stk_precedence(s[top]>input_precedence(symbol))
{
Pop element from stack and add it to postfix expression
}
If(stk_precedence(s[top]!=input_precedence(symbol))
Push the item into the stack
else
delete the item from the stack
}
After reaching end of expression move the content of stack output expression.
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
switch(symbol)
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '^':
case '$':return 5;
case '(':return 0;
default:return 8;
switch(symbol)
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case '(':return 9;
case ')':return 0;
default:return 7;
int top=-1,j=0,i;
char s[20],symbol;
s[++top]='#';
for(i=0;i<strlen(infix);i++)
symbol=infix[i];
while(stack_prec(s[top])>input_prec(symbol))
postfix[j]=s[top--];
j++;
if(stack_prec(s[top])!=input_prec(symbol))
s[++top]=symbol;
else
top--;
while(s[top]!='#')
postfix[j++]=s[top--];
postfix[j]='\0';
void main()
char infix[20],postfix[20];
clrscr();
scanf("%s",infix);
infix_postfix(infix,postfix);
getch();
Output:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
int isdigit (char );
float compute (char ,float ,float );
void main()
{
int i,top;
char symbol,postfix[20];
float s[20],op1,op2,res;
clrscr();
printf("\n\t***EVALUATION OF POSTFIX EXPRESSION***");
printf("\nEnter the postfix expression:");
gets(postfix);
top=-1;
for(i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if(isdigit(symbol))
{
top=top+1;
s[top]=symbol-'0';
}
else
{
op2=s[top--];
op1=s[top--];
res=compute(symbol,op1,op2);
s[++top]=res;
}
}
res=s[top--];
printf("\n\n\tResult of the given postfix Expression:%f",res);
getch();
}
OUTPUT:
Algorithm :
a. Procedure for GCD & LCM :
GCD(int m, int n)
i. if m is smaller than n,
return n,m
ii. Remainder equal to m modulo n.
iii. if r is zero then,
return n
else
return GCD(n,r)
iv. exit
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
int gcd(int,int);
int lcm(int,int,int);
void hanoi(int,char,char,char);
int sum(int);
void main()
{
int g,l,m,x,y,z,n,ch,disk,move,i,j,ele,temp,s,range;
clrscr();
switch(ch)
{
case 1: {
printf("Enter the Three integer");
scanf("%d%d%d",&x,&y,&z);
n=1;
g=gcd(gcd(x,y),z);
l=lcm(lcm(x,y,n),z,n);
printf("GCD is %d LCM is %d",g,l);
break;
}
case 2: {
printf("Enter the no of disk");
scanf("%d",&disk);
move=pow(2,disk)-1;
printf("No of moves are %d",move);
hanoi(disk,'A','B','C');
break;
}
case 3:{
OUTPUT:
1. if Queue is full,
Write: OVERFLOW of QUEUE
2. if Queue is not NULL, insert an ITEM,
REAR:=REAR%Size+1[increment the rear point by 1]
3. [Update the counter variable to check overflow]
COUNT:=COUNT+1
4. exit
1. if Queue is empty,
Write: UNDERFLOW of QUEUE
2. [Point to next front item]
FRONT:=FRONT%Size+1
3. [update the counter variable to check underflow ]
COUNT:=COUNT-1
4. exit
Program:
#include<stdio.h>
#include<conio.h>
#define MAX 5
int cq[MAX],rear=0,front=1,count=0;
void cqins()
{
int elt;
if(count==MAX)
{
printf("\nCQ is full");
}
else
{
printf("\nEnter the element of insert");
scanf("%d",&elt);
rear=rear%MAX+1;
cq[rear]=elt;
printf("\nElement insert at location %d",rear);
count++;
}
Department of MCA, NHCE Page 16 2017- 2018
Data Structures Using C Laboratory 18MCAL27
void cqdel()
{
if(count==0)
{
printf("\nCQ is empty");
}
else
{
printf("\nDelete item is %d",cq[front]);
front=front%MAX+1;
count--;
}
}
void cqdis()
{
int i,j;
i=front;
if(count==0)
{
printf("\nempty");
}
else
{
printf("\nEnter the element are:");
for(j=1;j<=count;j++)
{
printf("%d",cq[i]);
i=i%MAX+1;
}
printf("\nFront of the CQ:%d",cq[front]);
printf("\nRear of the CQ:%d",cq[rear]);
}
}
void main()
{
int ch;
clrscr();
for(;;)
{
printf("\nCQ Operation");
printf("\n1.Insert");
printf("\n2.Delete");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice...");
scanf("%d",&ch);
switch(ch)
{
case 1: cqins();
break;
case 2: cqdel();
break;
case 3: cqdis();
break;
case 4:exit(0);
default: printf("\nInvalid");
}
}
getch();
}
OUTPUT:
if front=0
front=1;
Step-3 : return
if front=rear
front=0;
rear=0;
else
front=front+1;
Step-3 : Return
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
int dq[MAX],front=0,rear=0;
void add_rear();
void add_front();
void delete_rear();
void delete_front();
void display();
void main()
{
int ch;
clrscr();
while(ch!=6)
{
printf("\n1.Insert at rear");
printf("\n2.Insert at front");
printf("\n3.Delete at rear");
printf("\n4.Delete at front");
printf("\n5.Display");
Department of MCA, NHCE Page 21 2017- 2018
Data Structures Using C Laboratory 18MCAL27
printf("\n6.Exit");
printf("\nEnter the choice...");
scanf("%d",&ch);
switch(ch)
{
case 1:{
add_rear();
printf("\nConsist of DQ after insert at rear");
display();
break;
}
case 2:{
add_front();
printf("\nIN DQ after insert at front");
display();
break;
}
case 3:{
delete_rear();
printf("\nDQ after delete at rear");
display();
break;
}
case 4:{
delete_front();
printf("\nDQ after front at front");
display();
break;
}
case 5:{
display();
break;
}
case 6:{
exit(0);
}
default : printf("\nInvalid Choice...");
}
}
getch();
}
void add_rear()
{
int no;
printf("\nEnter the value to insert");
scanf("%d",&no);
if(rear==MAX)
{
printf("\noverflow");
return;
}
else
{
rear++;
dq[rear]=no;
if(rear==0)
rear=1;
if(front==0)
front=1;
}
}
void add_front()
{
int no;
printf("\nEnter the value for insert");
scanf("%d",&no);
if(front<=1)
{
printf("\nCan not add front");
return;
}
else
{
front++;
dq[front]=no;
}
}
void delete_front()
{
int no;
if(front==0)
{
printf("\nUnderflow");
return;
}
else
{
no=dq[front];
printf("\nDelete element is %d",no);
if(front==rear)
{
front=0;
rear=0;
}
else
{
front++;
}
}
}
void delete_rear()
{
int no;
if(rear==0)
{
printf("\nCan not delete value at rear");
return;
}
else
{
no=dq[rear];
if(front==rear)
{
front=0;
rear=0;
}
else
{
rear--;
printf("\nDelete element is %d",no);
}
}
}
void display()
{
int i;
if(front==0)
{
printf("\nunderflow");
return;
}
else
{
printf("\nOutput:\n");
for(i=front;i<=rear;i++)
{
printf("\n%d",dq[i]);
}
}
}
Output:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE head;
NODE getnode();
void stkpush();
void pop_del();
void qinsert();
void display();
void freenode(NODE);
void main()
{
int ch;
char op;
clrscr();
head->link=NULL;
while(1)
{
printf("\n------------MENU------------");
printf("\n\t 1: STACK IMPLEMENTATION");
printf("\n\t 2: QUEUE IMPLEMENTATION");
printf("\n\t 3: DISPLAY");
printf("\n\t 4: EXIT");
printf("\n\t ENTER THE CHOICE: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\t\t STACK ");
printf("\n\t 1: Push - Insert at front");
printf("\n\t 2: Pop - Delete at front");
printf("\n\t Enter the option : " );
scanf("%d",&op);
switch(op)
{
case 1:
stkpush();
printf("\n\t Stack contents after Push operation");
display();
break;
case 2:
pop_del();
printf("\n\t Stack contents after Pop");
display();
break;
default: printf("\n\t Invalid choice: ");
exit(1);
break;
}
break;
case 2:
printf("\n\t\t QUEUE ");
printf("\n\t 1: Insert - Insert at Last");
printf("\n\t 2: Delete - Delete at front");
printf("\n\t Enter the option : " );
scanf("%d",&op);
switch(op)
{
case 1:
qinsert();
printf("\n\t Queue contents after insertion");
display();
break;
case 2:
pop_del();
printf("\n\t Queue contents after deletion");
display();
break;
default: printf("\n\t Invalid choice: ");
exit(1);
break;
}
break;
case 3: display();
break;
case 4:
exit(1);
break;
default:
printf("\n\t INVALID CHOICE \n");
}
}
}
void stkpush() //insert at front
{
NODE p,next;
p=getnode();
printf("\n\t Enter the element to be inserted at front: ");
scanf("%d",&p->info);
if(head->link==NULL)
{
printf("\n\t This is the 1st element of the list");
head->link=p;
p->link=NULL;
}
else
{
next=head->link;
p->link=next;
head->link=p;
}
}
void pop_del() // delete at front
{
NODE cur,next;
if(head->link==NULL)
{
printf("\n\t Empty List - Underflow");
return;
}
cur=head->link;
if(cur->link!=NULL)
{
next=cur->link;
head->link=next;
freenode(cur);
return;
}
head->link=NULL;
freenode(cur);
p->link=NULL;
}
else
{
temp=head->link;
while(temp->link != NULL)
temp=temp->link;
temp->link=p;
p->link=NULL;
}
}
void display()
{
NODE p;
if(head->link==NULL)
{
printf("\n\t Empty List");
return;
}
p=head->link;
printf("\n\t output List :\n");
printf("\n Head -->");
while(p->link!=NULL)
{
printf("%d-->",p->info);
p=p->link;
}
printf(" %d",p->info);
printf("-->");
printf("NULL");
}
NODE getnode()
{
NODE p;
p=(NODE)malloc(sizeof(struct node));
return(p);
}
void freenode(NODE p)
{
free(p);
}
Output:
Algorithm:
Delete at Front:
Delete at last:
Traverse the list to find the last node and its predecessor.
Point the link field of the predecessor to the first node (link field of the last node).
Free the memory of last node
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int elt;
struct node *next;
};
typedef struct node *NODE;
NODE last;
Department of MCA, NHCE Page 35 2017- 2018
Data Structures Using C Laboratory 18MCAL27
void delfront();
void delback();
void delelt();
void insback();
void display();
NODE getnode();
void freenode(NODE);
void main()
{
int ch;
clrscr();
last=NULL;
while(1)
{
printf("\n------------MENU------------");
printf("\n\t 1: INSERT AT BACK");
printf("\n\t 2: DELETE AT FRONT");
printf("\n\t 3: DELETE AT BACK");
printf("\n\t 4: DELETE THE GIVEN ELEMENT");
printf("\n\t 5: DELETE ALTERNATE ELEMENT");
printf("\n\t 6: DISPLAY");
printf("\n\t 7: EXIT");
printf("\n\t ENTER THE CHOICE: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insback();
display();
break;
case 2:
delfront();
display();
break;
case 3:
delback();
display();
break;
case 4:
delelt();
display();
break;
case 6:
display();
break;
case 7:
exit(1);
break;
default: printf("\n\t INVALID CHOICE\n");
}
}
}
void insback()
{
NODE p,temp;
p=getnode();
printf("\n\t Enter the element to be inserted at last: ");
scanf("%d",&p->elt);
if(last==NULL)
{
printf("\n\t This is the 1st element of the list");
last=p;
}
else
p->next=last->next;
last->next=p;
last=p;
}
void delfront()
{
NODE first;
if(last==NULL)
{
printf("\n\t empty list...deletion is not possible.");
return;
}
if(last->next==last) //when there is only one node in the list
{
printf("\n\t Deleted element : %d",last->elt);
printf("\n\t After deletion, list is empty");
free(last);
last=NULL;
return;
}
first=last->next; //points to the first node to be deleted
last->next=first->next; //links to the second node
printf("\n\t Deleted element : %d",first->elt);
freenode(first);
}
void delback()
{
NODE prev;
if(last==NULL)
{
printf("\n\t Empty list...deletion is not possible");
return;
}
if(last->next==last)
{
printf("\n\t Deleted element : %d",last->elt);
printf("\n\t After deletion , list is empty");
freenode(last);
last=NULL;
return;
}
prev=last->next; //to traverse from 1st node
while(prev->next != last)
prev=prev->next;
void delelt()
{
NODE d,prev,temp;
if(last==NULL)
{
printf("\n\t Empty list. Deletion is not possible.");
return;
}
printf("\n\t Enter the element to be deleted: ");
scanf("%d",&d->elt);
if(last->next->elt==d->elt)
{
printf("\n\t Deleting the 1st element");
delfront();
return;
}
//if the elt to be deleted is present at last
if(last->elt==d->elt)
{
printf("\n\t Deleting the last element");
delback();
return;
}
//otherwise traverse the list from 2nd node to the node before last node
void display()
{
NODE p,temp;
if(last==NULL)
{
printf("\n\t No Elements to Display");
return;
}
temp=last->next;
printf("\n\t THE LIST OF ELEMENTS ARE :\n");
while(temp!=last)
{
printf("%d-->",temp->elt);
temp=temp->next;
}
printf("%d--->",temp->elt);
printf("%d",temp->next->elt);
}
NODE getnode()
{
NODE p;
p=(NODE)malloc(sizeof(struct node));
return(p);
}
void freenode(NODE p)
{
free(p);
}
Output:
11. Implement
a. Selection sort
b. Insertion sort
Insertion Sort
Program:
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
return 0;
}
Output:
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
Output:
Program:
#include<stdio.h>
#include<conio.h>
do j-- ;
while (key<a[j]);
//Partition not completed .Exchange a[i],a[j] and repeat the process
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
//Partition completed and return the position of pivot element
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
quick_sort(1,n);
printf("The sorted array is:\n");
for(i=1;i<=n;i++)
printf("%d ",a[i]);
getch();
}
Output:
Program:
/* Sort the array elements using heap sort. */
#include<stdio.h>
void main()
{
int heap[10], no, i, j, c, root, temp;
clrscr();
printf("\n\t HEAP SORT");
printf("\n Enter no of elements :");
scanf("%d", &no);
scanf("%d", &heap[i]);
{
c = i;
do
root = (c - 1) / 2;
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
c = root;
} while (c != 0);
temp = heap[0];
heap[j] = temp;
root = 0;
do
c++;
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
root = c;
Output:
Algorithm:
SequentialSearch(a[1..n],n,key)
for i = 1 to n do
if(a[i] = key)
return i;
end if
return -1;
end for
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int key,n,a[10],i,ch,pos=-1;
clrscr();
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
printf("Enter the key to search...");
scanf("%d",&key);
while(ch!=3)
{
printf("\n1.Linear");
printf("\n2.Binary");
printf("\n3.Exit");
printf("\nEnter the choice...");
scanf("%d",&ch);
switch(ch)
{
case 1:{
pos=l(a,n,key);
if(pos!=-1)
{
printf("Found at %d",key);
}
else
{
printf("Not Found");
}
break;
}
case 2:{
pos=b(a,n,key);
if(pos==-1)
{
printf("Not Found");
}
else
{
printf("Element found at pos %d",pos);
}
break;
}
case 3:{
exit(0);
}
default: printf("Invalid choice...");
}
getch();
}
}
{
int i,pos=-1;
for(i=0;i<n;i++)
{
if(key==a[i])
{
pos=i;
}
}
return pos;
}
Output:
Algorithm:
Creating a BINARY SEARCH TREE
1. Given a set of elements that takes the first element as a root node.
2. for each of the remaining element(ITEM).
a. Compare ITEM with the root node of the tree.
i. If (ITEM<root node)
Proceed to the left of the root node.
ii. If (ITEM>root node)
Proceed to the right of the root node.
b. Repeat step(a) until we meet an empty sub tree.
3. Insert ITEM in place of empty sub tree.
Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *left,*right;
};
void main()
{
NODE root=NULL;
int ch;
clrscr();
for(;;)
{
printf("\n 1. Create Binary Tree");
printf("\n 2. In-order Traversal");
printf("\n 3. Pre-order Traversal");
printf("\n 4. Post-order Traversal");
printf("\n 5. Exit");
printf("\n Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : root=create(root);
break;
case 2 : if (root==NULL)
printf("\n\t Tree is NULL");
else
inorder(root);
break;
case 3 : if (root==NULL)
printf("\n\t Tree is null, traversing is not possible");
else
preorder(root);
break;
case 4 : if (root==NULL)
printf("\n\t Tree is null, traversing is not possible");
else
postorder(root);
break;
case 5 : exit(0);
break;
default : printf("\n Invalid option");
break;
}
}
newnode->num=num;
newnode->left=NULL;
newnode->right=NULL;
if(root==NULL)
{
return newnode;
}
temp=root;
prev=root;
while(temp!=NULL)
{
prev=temp;
if(num<temp->num)
{
temp=temp->left;
}
else if(num>temp->num)
{
temp=temp->right;
}
else
{
printf("\n Value already exists");
return root;
}
}
if(num<prev->num)
{
prev->left=newnode;
}
else
{
prev->right=newnode;
}
return root;
}
}
void preorder(NODE root)
{
if(root!=NULL)
{
printf("%d\t",root->num);
preorder(root->left);
preorder(root->right);
}
}
void postorder(NODE root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->num);
}
}
Output: