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

Data Structure using C Lab Manual Book

The document is a lab manual for a B.C.A Data Structure course at Jaihind Degree College, detailing various programming exercises. It includes tasks such as implementing search algorithms, sorting methods, linked lists, queues, and binary trees using C programming. Each task provides specific requirements and sample code to guide students in completing their assignments.

Uploaded by

Mohammed Mateen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Data Structure using C Lab Manual Book

The document is a lab manual for a B.C.A Data Structure course at Jaihind Degree College, detailing various programming exercises. It includes tasks such as implementing search algorithms, sorting methods, linked lists, queues, and binary trees using C programming. Each task provides specific requirements and sample code to guide students in completing their assignments.

Uploaded by

Mohammed Mateen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Jaihind DEGREE college

st
1 Semester B.C.A
Data Structure
using C
Lab Manual
1

Jaihind Degree College | Megharaj, Computer Science Lecturer


2

Jaihind Degree College | Megharaj, Computer Science Lecturer


Content
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.

2. Given {5,3,1,6,0,2,4} order the numbers in ascending order using Bubble Sort Algorithm

3. Perform the Insertion and Selection Sort on the input {75,8,1,16,48,3,7,0} and display the output in
descending order.

4. Write a program to insert the elements {61,16,8,27} into singly linked list and delete 8,61,27 from the list.
Display your list after each insertion and deletion.

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.

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.

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.

8. Write a program to add 6x3+10x2+0x+5 and 4x2+2x+1 using linked list.

9. Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack, also display the popped
numbers.

10. Write a recursive program to find GCD of 4,6,8.

11. Write a program to inert the elements {5,7,0,6,3,9} into circular queue and delete 6,9&5 from it(using
linked list implementation)..

12. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix expression

13. Write a program to evaluate a postfix expression 5 3+8 2 - *.

14. Write a program to create a binary tree with the elements {18,15,40,50,30,17,41} after creation insert 45
and 19 into tree and delete 15,17 and 41 from tree. Display the tree on each insertion and deletion operation

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.

16. Write a program to Sort the following elements using heap sort {9.16,32,8,4,1,5,8,0}
3
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”

Jaihind Degree College | Megharaj, Computer Science Lecturer


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.

# include<stdio.h>
# include<conio.h>

void LINEAR_SEARCH(int a[10], int n, int key)


{
int i,found=0;
for(i=0;i<n;i++)
{
if(key==a[i])
{
printf("\n %d found at a[%d]",key,i);
found=1;
break;
}
}
if(found==0)
{
printf("\n Element not found in the list");
}
}

void BINARY_SEARCH(int a[10], int key, int first, int last)


{
int mid;
mid=(first+last)/2;

if(key < a[mid])


{
BINARY_SEARCH(a,key,first,mid-1);
}
else
{
if(key > a[mid])
{
BINARY_SEARCH(a,key,mid+1,last);
}
else
{
printf("\n %d found at a[%d]",key,mid);
}
} 4
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


void DISPLAY_ARRAY(int a[10],int n)
{
int i;
printf("\n Given array : ");
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
}

void main()
{
int a[8] = {4,7,3,2,1,7,9,0};
int sa[8] = {0,1,2,3,4,7,7,9};
int n=8;
int key=7;
int choice;
clrscr();
printf("\n 1. Linear Search \n");
printf("\n 2. Binary Search \n");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : DISPLAY_ARRAY(a,n);
LINEAR_SEARCH(a,n,key);
break;

case 2 : DISPLAY_ARRAY(sa,n);
BINARY_SEARCH(sa,key,0,n-1);
break;

default: printf("\n Invalid choice ");


}
getch();
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


2. Given {5,3,1,6,0,2,4} order the numbers in ascending order using Bubble Sort Algorithm

# include<stdio.h>
# include<conio.h>

void BUBBLE_SORT(int a[7], 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();
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


3. Perform the Insertion and Selection Sort on the input {75,8,1,16,48,3,7,0} and display the
output in descending order.

# include<stdio.h>
# include<conio.h>

void INSERTION_SORT(int a[8], int n)


{
int pass,k,temp,i,j;
for(pass=1;pass<n;pass++)
{

k=a[pass];
for(j=pass-1;j>=0 && k<a[j];j--)
{
a[j+1] = a[j];
}
a[j+1]=k;
printf("\n\n Sorted arrays after %d pass -->",pass);
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
}
printf("\n\n Sorted arrays using Insertion sort in Descending Order\n");
for(i=n-1;i>=0;i--)
{
printf("%3d",a[i]);
}
}

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); 7
getch();
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


# include<stdio.h>
# include<conio.h>

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

void SELECTION_SORT(int a[], int k, int n)


{
int i,loc,temp;
for(k=0;k<n;k++)
{
loc=MIN(a,k,n);
temp=a[k];
a[k]=a[loc];
a[loc]=temp;
printf("\n\nSorted arrays after %d pass:-->",k);
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
}
printf("\n\n Sorted arrays using Selection sort in Descending Order\n");
for(i=n-1;i>=0;i--)
{ 8
printf("%3d",a[i]);
}
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


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");
SELECTION_SORT(a,0,n);
getch();
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


4. Write a program to insert the elements {61,16,8,27} into singly linked list and delete
8,61,27 from the list. Display your list after each insertion and deletion.

# include<stdio.h>
# include<stdlib.h>
# include<conio.h>
# include<alloc.h>
# include<ctype.h>

typedef struct node


{
int info;
struct node *link;
}NODE;

NODE *header=NULL;

void INSERT(int item)


{
NODE *newnode;
newnode = (NODE *) malloc(sizeof(NODE));
newnode->info=item;
newnode->link=header;
header=newnode;
}

10

Jaihind Degree College | Megharaj, Computer Science Lecturer


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;
}
}
}
}
}

11

Jaihind Degree College | Megharaj, Computer Science Lecturer


void DISPLAY()
{
NODE *start=header;
while(start!=NULL)
{
printf("%4d",start->info);
start=start->link;
}
}
void main()
{
int item,choice;
clrscr();
while(1)
{
printf("\n 1. Insert \n");
printf("\n 2. Delete \n");
printf("\n 3. Exit \n");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : {
printf("\n Enter Item to be inserted : ");
scanf("%d",&item);
INSERT(item);
DISPLAY();
break;
}
case 2 : {
printf("\n Enter Item to be deleted : ");
scanf("%d",&item);
DELETE(item);
DISPLAY();
break;
}
case 3 : exit(0);
} 12
}
getch();
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


13

Jaihind Degree College | Megharaj, Computer Science Lecturer


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.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX 50

void insert();
void remove();
void display();
int a[MAX];
int rear = - 1;
int front = - 1;

void main()
{
int choice;
clrscr();
while (1)
{
printf("\n 1. Insert element to queue \n");
printf("\n 2. Delete element from queue \n");
printf("\n 3. Display all elements of queue \n");
printf("\n 4. Quit \n");
printf("\n Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1: insert();
break;
case 2: remove();
break;
case 3: display();
break;
case 4: exit(1);
default:printf("\n Wrong choice");
}
}
}

14

Jaihind Degree College | Megharaj, Computer Science Lecturer


void insert()
{
int item;
if(rear == MAX - 1)
{
printf("\n Sorry, Queue full \n");
}
else
{
if(front== - 1)
{
front = 0;
rear = 0;
}
else
{
rear++;
}
printf("\n Insert the element in queue : ");
scanf("%d", &item);
a[rear] = item;
printf("\n %d is Successfully inserted",item);
}
}

void remove()
{
if(front == - 1)
{
printf("\n Sorry, Queue Empty... \n");
return;
}
else
{
if(front == rear)
{
front = -1;
rear = -1;
}
else
{
front++;
}
} 15
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


void display()
{
int i;
if(front == - 1)
{
printf("\n Sorry, Queue is empty \n");
}
else
{
printf("\n Queue is : \n");
for(i = front; i <= rear; i++)
{
printf("%d ", a[i]);
}
}
}

16

Jaihind Degree College | Megharaj, Computer Science Lecturer


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

#include<stdio.h>
# define MAX 5
int cq[MAX];
int front = -1;
int rear = -1;

void insert(int item)


{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
{
rear = 0;
}
else
{
rear = rear+1;
}
}
cq[rear] = item ;
}

void deletion()
{
if(front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cq[front]);
if(front == rear) 17
{
front = -1;
rear=-1;
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


else
{
if(front == MAX-1)
{
front = 0;
}
else
{
front = front+1;
}
}
}

void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{
while(front_pos <= rear_pos)
{
printf("%3d ",cq[front_pos]);
front_pos++;
}
}
else
{
while(front_pos <= MAX-1)
{
printf("%3d ",cq[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cq[front_pos]);
front_pos++;
}
}
printf("\n"); 18
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


void main()
{
int choice,item;
clrscr();
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Input the element for insertion in queue : ");
scanf("%d", &item);
insert(item);
break;
case 2 : deletion();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("Wrong choice\n");
}
}
getch();
}

19

Jaihind Degree College | Megharaj, Computer Science Lecturer


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<stdlib.h>

struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *start=NULL;

void insert(int item)


{
NODE *NEWNODE = (NODE*)malloc(sizeof(NODE));
NEWNODE->INFO = item;
if(start == NULL)
{
start = NEWNODE;
start->LINK = NULL;
}
else
{
if(item < start->INFO)
{
NEWNODE->LINK = start;
start = NEWNODE;
}
else
{
NODE *PREVPTR = start;
NODE *CURRPTR = start->LINK;
while(CURRPTR != NULL && item > CURRPTR->INFO)
{
PREVPTR = CURRPTR;
CURRPTR = CURRPTR->LINK;
}
PREVPTR->LINK = NEWNODE;
NEWNODE->LINK = CURRPTR;
}
}
}

20

Jaihind Degree College | Megharaj, Computer Science Lecturer


void remov(int item)
{
NODE *PREVPTR = start;
NODE *CURRPTR = start->LINK;
if(start == NULL)
{
printf("\n List is empty");
}
else
{
if(item == start->INFO)
{
start = CURRPTR;
free(PREVPTR);
}
else
{
while(CURRPTR != NULL && CURRPTR->INFO != item)
{
PREVPTR = CURRPTR;
CURRPTR = CURRPTR->LINK;
}
if(CURRPTR != NULL)
{
PREVPTR->LINK = CURRPTR->LINK;
free(CURRPTR);
}
else
{
printf("\n Item not found in the list");
}
}
}
}

void display()
{
NODE * CURRPTR = start;
if(CURRPTR == NULL)
{
printf("Empty");
}
else
{
while(CURRPTR != NULL)
{
printf("%d", CURRPTR->INFO);
printf(" -> ");
CURRPTR = CURRPTR->LINK; 21
}
printf("NULL");
}
}
Jaihind Degree College | Megharaj, Computer Science Lecturer
void main()
{
int choice,item;
clrscr();
while(1)
{
printf("\n\n ORDERED LINKED LIST OPERATIONS");
printf("\n ******************************");
printf("\n 1.Insert");
printf("\n 2.Delete");
printf("\n 3.Display");
printf("\n 4.Quit");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\n Enter item to be inserted : ");
scanf("%d", &item);
printf("\n Linked list before insertion is : \n");
display();
insert(item);
printf("\n Linked list after insertion is : \n");
display();
break;
case 2 : printf("\n Enter item to be deleted : ");
scanf("%d",&item);
printf("\n Linked list before deletion is : \n");
display();
remov(item);
printf("\n Linked list after deletion is : \n");
display();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("Wrong choice\n");
}
}
getch();
}

22

Jaihind Degree College | Megharaj, Computer Science Lecturer


23

Jaihind Degree College | Megharaj, Computer Science Lecturer


8. Write a program to add 6x3+10x2+0x+5 and 4x2+2x+1 using linked list.
#include<stdio.h>

struct poly
{
int coeff;
int expo;
};

struct poly p1[10],p2[10],p3[10];

int readPoly(struct poly []);


int addPoly(struct poly [],struct poly [],int ,int ,struct poly []);
void displayPoly( struct poly [],int terms);

void main()
{
int t1,t2,t3;
clrscr();
t1=readPoly(p1);
printf(" \n First polynomial : ");
displayPoly(p1,t1);
t2=readPoly(p2);
printf(" \n Second polynomial : ");
displayPoly(p2,t2);
t3=addPoly(p1,p2,t1,t2,p3);
printf(" \n\n Resultant polynomial after addition : ");
displayPoly(p3,t3);
printf("\n");
getch();
}

int readPoly(struct poly p[10])


{
int t1,i;
printf("\n\n Enter the total number of terms in the polynomial:");
scanf("%d",&t1);
printf("\n Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER\n");
for(i=0;i<t1;i++)
{
printf(" Enter the Coefficient(%d): ",i+1);
scanf("%d",&p[i].coeff);
printf(" Enter the exponent(%d): ",i+1);
scanf("%d",&p[i].expo); /* only statement in loop */
}
return(t1);
24
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


int addPoly(struct poly p1[10],struct poly p2[10],int t1,int t2,struct poly p3[10])
{
int i,j,k;
i=0;
j=0;
k=0;
while(i<t1 && j<t2)
{
if(p1[i].expo==p2[j].expo)
{
p3[k].coeff=p1[i].coeff + p2[j].coeff;
p3[k].expo=p1[i].expo;
i++;
j++;
k++;
}
else if(p1[i].expo>p2[j].expo)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}
else
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
}
}

while(i<t1)
{
p3[k].coeff=p1[i].coeff;
p3[k].expo=p1[i].expo;
i++;
k++;
}

while(j<t2)
{
p3[k].coeff=p2[j].coeff;
p3[k].expo=p2[j].expo;
j++;
k++;
} 25
return(k);
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


void displayPoly(struct poly p[10],int term)
{
int k;
for(k=0;k<term-1;k++)
printf("%d(x^%d)+",p[k].coeff,p[k].expo);
printf("%d(x^%d)",p[term-1].coeff,p[term-1].expo);
}

26

Jaihind Degree College | Megharaj, Computer Science Lecturer


9. Write a program to push 5,9,34,17,32 into stack and pop 3 times from the stack, also
display the popped numbers.

#include<stdio.h>
#include<stdlib.h>
#define MAX 5

int top=-1,stack[MAX];
void push();
void pop();
void display();

void main()
{
int ch;
clrscr();
while(1)
{
clrscr();
printf("\n** Stack Menu **\n");
printf("\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit");
printf("\n\nEnter your choice(1-4):");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong Choice!!");
}
}
}

27

Jaihind Degree College | Megharaj, Computer Science Lecturer


void push()
{
int val;
if(top==MAX-1)
{
printf("\nStack is full!!");
}
else
{
printf("\nEnter element to push:");
scanf("%d",&val);
top=top+1;
stack[top]=val;
}
getch();
}

void pop()
{
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nDeleted element is %d",stack[top]);
top=top-1;
}
getch();
}

void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty!!");
}
else
{
printf("\nStack is...\n");
for(i=top;i>=0;--i)
{
printf("%d\n",stack[i]);
} 28
}
getch();
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


29

Jaihind Degree College | Megharaj, Computer Science Lecturer


10. Write a recursive program to find GCD of 4,6

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int gcf(int n1, int n2);

void main()
{
int n1, n2;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcf(n1, n2));
getch();
}

int gcf(int n1, int n2)


{
if (n2 != 0)
{
return gcf(n2, n1 % n2);
}
else
{
return n1;
}
}

30

Jaihind Degree College | Megharaj, Computer Science Lecturer


11. Write a program to inert the elements {5,7,0,6,3,9} into circular queue and delete 6,9&5
from it(using linked list implementation)..

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

struct node
{
int INFO;
struct node *LINK;
};
struct node *front = NULL, *rear = NULL;

void QInsert(int item)


{
struct node *NEWNODE;
NEWNODE = (struct node*)malloc(sizeof(struct node));
NEWNODE -> INFO = item;
NEWNODE -> LINK = NULL;
if(front == NULL & rear == NULL)
{
front = rear = NEWNODE;
rear -> LINK = front;
}
else
{
rear -> LINK = NEWNODE;
rear = NEWNODE;
rear -> LINK = front;
}
}

31

Jaihind Degree College | Megharaj, Computer Science Lecturer


void QDelete()
{
struct node *ptr;
ptr = front;
if(front == NULL && rear == NULL)
{
printf("\n Queue is Empty");
}
else
{
if(front == rear)
{
front = rear = NULL;
printf("\n THe value being deleted is : %d",ptr->INFO);
free(ptr);
}
else
{
front = front -> LINK;
rear -> LINK = front;
printf("\n The value being deleted is : %d",ptr->INFO);
free(ptr);
}
}
}

void display()
{
struct node *ptr;
ptr = front;
if(front == NULL & rear == NULL)
{
printf("\n Queue is Empty");
}
else
{
printf("\n The Queue elements are : ");
do 32
{
printf("%d",ptr->INFO);
ptr=ptr->LINK;

Jaihind Degree College | Megharaj, Computer Science Lecturer


}while(ptr != front);
}
}

void main()
{
int choice,item;
clrscr();
while(1)
{
printf("\n ****** MAIN MANU ******");
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display");
printf("\n 4. Quit");
printf("\n Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\n Enter the number to insert into Queue : ");
scanf("%d",&item);
QInsert(item);
break;
case 2 : QDelete();
break;
case 3 : display();
break;
case 4 : exit(0);
break;
default: printf("\n Invalid Option");
break;
}
}
}

33

Jaihind Degree College | Megharaj, Computer Science Lecturer


34

Jaihind Degree College | Megharaj, Computer Science Lecturer


12. Write a program to convert an infix expression x^y/(5*z)+2 to its postfix expression.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.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");
getchar();
exit(1);
}
else
{
item = stack[top];
top = top-1; 35
return(item);
}
}

Jaihind Degree College | Megharaj, Computer Science Lecturer


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('('); 36
strcat(infix_exp,")");
i=0;
j=0;

Jaihind Degree College | Megharaj, Computer Science Lecturer


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"); /* the it is illegeal symbol */
getchar(); 37
exit(1);
}
i++;

Jaihind Degree College | Megharaj, Computer Science Lecturer


item = infix_exp[i]; /* go to next symbol of infix expression */
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
getchar();
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();
}

38

Jaihind Degree College | Megharaj, Computer Science Lecturer


13. Write a program to evaluate a postfix expression 53+8 2 - *.

#include<stdio.h>

int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

void 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)
39
{
case '+':
{
n3 = n1 + n2;
Jaihind Degree College | Megharaj, Computer Science Lecturer
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
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();
}

40

Jaihind Degree College | Megharaj, Computer Science Lecturer


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 <stdlib.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);
} 41

Jaihind Degree College | Megharaj, Computer Science Lecturer


// 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);
clrscr();
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();
}

42

Jaihind Degree College | Megharaj, Computer Science Lecturer


16. Write a program to Sort the following elements using heap sort {1, 12, 9, 5, 6, 10}

#include <stdio.h>

void swap(int *a, int *b)


{
int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i)


{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
{
largest = left;
}
if (right < n && arr[right] > arr[largest])
{
largest = right;
}
if (largest != i)
{
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n)


{
int i;
for (i = n / 2 - 1; i >= 0; i--)
{
heapify(arr, n, i);
}
for (i = n - 1; i >= 0; i--)
{
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
43

Jaihind Degree College | Megharaj, Computer Science Lecturer


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

void main()
{
int arr[] = {1, 12, 9, 5, 6, 10};
int n = 6;
clrscr();
heapSort(arr, n);
printf("Sorted array is \n");
printArray(arr, n);
getch();
}

44

Jaihind Degree College | Megharaj, Computer Science Lecturer


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”
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
char S1[50],S2[50],substr[50],repstr[50];
int len,pos,ch;
clrscr();
while(1)
{
printf("\n String Operation ");
printf("\n -----------------");
printf("\n 1. Find the length of S1");
printf("\n 2. Concatenate S1 & S2");
printf("\n 3. Extract the substring 'low' from S1");
printf("\n 4. Find 'are' in S2 and replace it with 'is'");
printf("\n 5. Exit");
printf("\n Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter the String : ");
scanf("%s",&S1);
printf("\n The length of string S1 is %d",LENGTH(S1));
break;
case 2 : printf("\n Enter the First String : ");
scanf("%s",&S1);
printf("\n Enter the Second String : ");
scanf("%s",&S2);
CONCAT(S1,S2);
printf("\n The concatenate of string S1 and string S2 is %s",S1);
break;
case 3 : printf("\n Enter the String : ");
scanf("%s",&S1);
printf("\n Enter the position of a substring : ");
scanf("%d",&pos);
printf("\n Enter the length of the substring : ");
scanf("%d",&len);
SUBSTR(S1,pos,len);
break;
case 4 : printf("\n Enter the String : ");
scanf("%s",&S2);
printf("\n Enter the string to be removed : ");
scanf("%s",&substr); 45
printf("\n Enter the string to replace : ");
scanf("%s",&repstr);
REPLACE(S2,substr,repstr);
break;

Jaihind Degree College | Megharaj, Computer Science Lecturer


case 5 : exit(0);
break;
default : printf("\n Invalid Option");
break;
}
}
}

int LENGTH(char *S1)


{
int i=0;
while(S1[i]!='\0')
{
i=i+1;
}
return i;
}

int CONCAT(char *S1,char *S2)


{
int i=0,j=0;
while(S1[i]!='\0')
{
i++;
}
while(S2[j]!='\0')
{
S1[i]=S2[j];
i++;
j++;
}
S1[i]='\0';
return 0;
}

46

Jaihind Degree College | Megharaj, Computer Science Lecturer


int SUBSTR(char S1[],int pos,int len)
{
char sub[50];
int slen=LENGTH(S1);
int p,j,max;
if(pos>slen)
{
printf("\n Invalid Position ");
return;
}
max=slen-pos+1;
if(len>max)
{
printf("\n Invalid Substring Length ");
return;
}
p=pos-1;

for(j=0;j<len;j++)
{
sub[j]=S1[p];
p++;
}
sub[j]='\0';
printf("\n Substring = %s",sub);
return;
}

47

Jaihind Degree College | Megharaj, Computer Science Lecturer


int REPLACE(char S2[],char substr[],char repstr[])
{
char output[50];
int i=0,j=0,flag=0,start=0;
while(S2[i]!='\0')
{
if(S2[i]==substr[j])
{
if(!flag)
start = i;
j++;
if(substr[j]=='\0')
break;
flag=1;
}
else
{
flag=start=j=0;
}
i++;
}

if(substr[j] == '\0' && flag)


{
for(i=0; i<start; i++)
output[i]=S2[i];
for(j=0; j<LENGTH(repstr); j++)
{
output[i]=repstr[j];
i++;
}
for(j=start+LENGTH(substr); j<LENGTH(S2); j++)
{
output[i]=S2[j];
i++;
}
output[i]='\0';
printf("Output : %s \n", output);
}
else
{
printf("%s is not a substring of %s \n",substr,S2);
}
return;
}

48

Jaihind Degree College | Megharaj, Computer Science Lecturer


49

Jaihind Degree College | Megharaj, Computer Science Lecturer


50

Jaihind Degree College | Megharaj, Computer Science Lecturer

You might also like