Data Structes Lab Manual Fixed
Data Structes Lab Manual Fixed
1a. Given {4,7,3,2,1,7,9,0} find the location of 7 using Linear search and display its first occurance.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,x;
clrscr();
printf("Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n",n);
for(i=0;i<n;++i)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched : ");
scanf("%d",&x);
for(i=0;i<n;++i)
{
if(a[i]==x)
break;
}
if(i<n)
printf("\nElement is found at position : %d\n",i+1);
else
printf("\nEntered element not found\n");
}
OUTPUT:
Enter the size of the array : 8
1b. Given {4,7,3,2,1,7,9,0} find the location of 7 using Binary search and display its first occurance.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,low,high,mid,n,key;
clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n", n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the element to be searched : ");
scanf("%d",&key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(a[mid] < key)
low = mid + 1;
else if (a[mid] == key)
{
printf("\nElement found at position : %d\n",mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("\nEntered element not found\n");
}
OUTPUT:
Enter the size of array : 8
2. Given {5,3,1,6,0} order the numbers in ascending order using Bubble sort Algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,swap;
clrscr();
printf("Enter the size of array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j] > a[j+1])
{
swap=a[j];
a[j]=a[j+1];
a[j+1]=swap;
}
}
}
printf("\nSorted Array : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
OUTPUT:
Enter the size of array : 5
Sorted Array : 0 1 3 5 6
3a. Perform the Insertion sort on the input {7,5,8,1,16,48,3,7,0} and display the output in
descending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],i,j,size,swap;
clrscr();
printf("Enter the size of the array : ");
scanf("%d",&size);
printf("\nEnter %d elements to be stored in the array : \n",size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
for(i=1;i<(size);i++)
{
j=i;
while(j>0 && a[j]>a[j-1])
{
swap=a[j];
a[j]=a[j-1];
a[j-1]=swap;
j--;
}
}
printf("\nSorted array : ");
for(i=0;i<size;i++)
printf("%d ",a[i]);
printf("\n");
}
OUTPUT:
Enter the size of the array : 9
Sorted array : 48 16 8 7 7 5 3 1 0
3b. Perform the Selection sort on the input {7,5,8,1,16,48,3,7,0} and display the output in
descending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,position,swap;
clrscr();
printf("Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter %d elements to be stored in the array : \n",n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
position=i;
for(j=i+1;j<n;j++)
{
if(a[position]<a[j])
position=j;
}
if(position!=i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("\nSorted Array : ");
for(i=0;i<n;i++)
printf("%d ", a[i]);
printf("\n");
}
OUTPUT:
Enter the size of the array : 9
Sorted array : 48 16 8 7 7 5 3 1 0
4. 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<conio.h>
#include<alloc.h>
#include<ctype.h>
struct node
{
int INFO;
struct node * LINK;
};
}
else if(start->INFO==item)
{
start=start->LINK;
free(CURPTR);
return;
}
else
{
while((CURPTR->INFO!=item)&&(CURPTR!=NULL))
{
PREVPTR=CURPTR;
CURPTR=CURPTR->LINK;
}
if(CURPTR==NULL)
{
printf("\nItem not found\n");
}
else
{
PREVPTR->LINK=CURPTR->LINK;
}
}
}
void display()
{
NODE * CURPTR=start;
if(start==NULL)
printf("\nLinked list is empty\n");
else
{
printf("\nLinked List : ");
while(CURPTR!=NULL)
{
printf("%d",CURPTR->INFO);
printf("->");
CURPTR=CURPTR->LINK;
}
printf("NULL\n");
}
printf("\n");
}
void main()
{
int item;
char ch;
clrscr();
while(1)
{
printf("Create : 1\n");
printf("Insert : 2\n");
printf("Delete : 3\n");
printf("Exit : 4\n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1 :start=NULL;
create();
display();
break;
case 2 :printf("\nEnter the item to be inserted : ");
scanf("%d", &item);
insert_beg(item);
display();
break;
case 3 :printf("\nEnter the item to be deleted : ");
scanf("%d", &item);
delete(item);
display();
break;
case 4 :exit(0);
default:printf("\nINVALID CHOICE\n");
}
}
}
OUTPUT:
Create : 1
Insert : 2
Delete : 3
Exit : 4
Enter your choice : 1
Create : 1
Insert : 2
Delete : 3
Exit : 4
Create : 1
Insert : 2
Delete : 3
Exit : 4
Enter your choice : 3
Create : 1
Insert : 2
Delete : 3
Exit : 4
Enter your choice : 3
Create : 1
Insert : 2
Delete : 3
Exit : 4
Enter your choice : 4
5. 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>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
clrscr();
while(choice != 4)
{
printf("\nInsert : 1\n");
printf("Delete : 2\n");
printf("Display the queue : 3\n");
printf("Exit : 4\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("\nInvaid Choice\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter the item to be inserted : ");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nQueue is Empty\n");
}
else
{
printf("\nQueue : ");
while(ptr != NULL)
{
printf("%d ",ptr -> data);
ptr = ptr -> next;
}
printf("\n");
}
getch();
}
OUTPUT:
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 3
Queue : 61 16 8 27
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 3
Queue : 27
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 4
6. Program to insert the elements {61,16,8,27} into circular queue and delete four elements from
the list . Display your list after each insertion and deletion.
#include<stdio.h>
#include<conio.h>
#define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("\nQueue Overflow\n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front == -1)
{
printf("\nQueue Underflow\n");
return ;
}
printf("\nElement deleted from the queue is : %d",cqueue_arr[front]);
printf("\n");
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("\nQueue is empty\n");
return;
}
printf("\nQueue : ");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("\n");
}
int main()
{
int choice,item;
clrscr();
do
{
printf("\nInsert : 1\n");
printf("Delete : 2\n");
printf("Display : 3\n");
printf("Quit : 4\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\nEnter the element to be inserted : ");
scanf("%d", &item);
insert(item);
break;
case 2 :
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:printf("\nInvalid Choice\n");
}
}
while(choice!=4);
return 0;
}
OUTPUT:
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 3
Queue : 61 16 8 27
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Queue is empty
Insert : 1
Delete : 2
Display the queue : 3
Exit : 4
Enter your choice : 4
7. 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>
struct node
{
int INFO;
struct node *LINK;
};
typedef struct node NODE;
NODE *start=NULL;
void insertordered(int data)
{
NODE *NEWNODE=(NODE*)malloc(sizeof(NODE));
NEWNODE->INFO=data;
if(start==NULL)
{
start=NEWNODE;
start->LINK=NULL;
}
else if(data<start->INFO)
{
NEWNODE->LINK=start;
start=NEWNODE;
}
else
{
NODE *PREVPTR=start;
NODE *CURRPTR=start->LINK;
while(CURRPTR!=NULL&&data>CURRPTR->INFO)
{
PREVPTR=CURRPTR;
CURRPTR=CURRPTR->LINK;
}
PREVPTR->LINK=NEWNODE;
NEWNODE->LINK=CURRPTR;
}
}
void deleteordered(int data)
{
NODE *PREVPTR=start;
NODE *CURRPTR=start->LINK;
if(start==NULL)
printf("\nUnderflow\n");
else if(data==start->INFO)
{
start=CURRPTR;
free(PREVPTR);
}
else
{
while(CURRPTR!=NULL&&CURRPTR->INFO!=data)
{
PREVPTR=CURRPTR;
CURRPTR=CURRPTR->LINK;
}
if(CURRPTR!=NULL)
{
PREVPTR->LINK=CURRPTR->LINK;
free(CURRPTR);
}
else
printf("\nEntered Data Not Found In The List\n");
}
}
void display()
{
NODE *CURRPTR=start;
if(CURRPTR==NULL)
printf("Empty");
else
{
while(CURRPTR!=NULL)
{
printf("%d",CURRPTR->INFO);
printf("->");
CURRPTR=CURRPTR->LINK;
}
printf("NULL");
}
}
void main()
{
int ch,data;
clrscr();
while(1)
{
printf("\nInsert : 1");
printf("\nDelete : 2");
printf("\nExit : 3");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter data to be inserted : ");
scanf("%d",&data);
insertordered(data);
printf("\nLinked list after insertion is : ");
display();
printf("\n");
break;
case 2:printf("\nEnter data to be deleted : ");
scanf("%d",&data);
deleteordered(data);
printf("\nLinked list after deletion is : ");
display();
printf("\n");
break;
case 3:exit(0);
}
}
}
OUTPUT:
Insert : 1
Delete : 2
Exit : 3
Enter your choice: 1
Insert : 1
Delete : 2
Exit : 3
Enter your choice: 1
Insert : 1
Delete : 2
Exit : 3
Enter your choice: 1
Insert : 1
Delete : 2
Exit : 3
Enter your choice: 1
Insert : 1
Delete : 2
Exit : 3
Enter your choice: 2
Insert : 1
Delete : 2
Exit : 3
Enter your choice: 2
Insert : 1
Delete : 2
Exit : 3
Enter your choice: 2
Insert : 1
Delete : 2
Exit : 3
Enter your choice: 3
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
if(flag)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next;
tmp_node->next = NULL;
}
} while (flag);
}
void my_show_poly(my_poly * node)
{
while(node != NULL)
{
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node != NULL)
{
printf(" + ");
}
}
}
void my_add_poly(my_poly ** result, my_poly * poly1, my_poly * poly2)
{
my_poly * tmp_node;
tmp_node = (my_poly *) malloc(sizeof(my_poly));
tmp_node->next = NULL;
*result = tmp_node;
while(poly1 && poly2)
{
if (poly1->pow > poly2->pow)
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1 = poly1->next;
}
else if (poly1->pow < poly2->pow)
{
tmp_node->pow = poly2->pow;
tmp_node->coeff = poly2->coeff;
poly2 = poly2->next;
}
else
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
if(poly1 && poly2)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next;
tmp_node->next = NULL;
}
}
while(poly1 || poly2)
{
tmp_node->next = (my_poly *) malloc(sizeof(my_poly));
tmp_node = tmp_node->next;
tmp_node->next = NULL;
if(poly1)
{
tmp_node->pow = poly1->pow;
tmp_node->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2)
{
tmp_node->pow = poly2->pow;
tmp_node->coeff = poly2->coeff;
poly2 = poly2->next;
}
}
printf("\n\nAddition of the polynomials is : ");
}
OUTPUT:
Create 1st Expression :
Enter Coefficient : 6
Enter Power : 3
Enter Coefficient : 10
Enter Power : 2
Enter Coefficient : 0
Enter Power : 1
Enter Coefficient : 5
Enter Power : 0
Enter Coefficient : 4
Enter Power : 2
Enter Coefficient : 2
Enter Power : 1
Enter Coefficient : 1
Enter Power : 0
9. 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<conio.h>
#define size 5
int n=5,top=-1,s[size];
void push()
{
int item;
if(top==(size-1))
{
printf("\nStack Overflow\n");
}
else
{
printf("\nEnter the item to be pushed : ");
scanf("%d",&item);
top=top+1;
s[top]=item;
}
}
void pop()
{
if(top==-1)
{
printf("\nStack Underflow\n");
}
else
{
printf("\nPopped Element is : %d\n",s[top]);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is empty\n");
}
else
{
printf("\nStack elements are : \n");
for(i=top;i>=0;i--)
{
printf("\n%d\n",s[i]);
}
}
}
void main()
{
char ch;
int item;
clrscr();
while(1)
{
printf("\nPush : 1\n");
printf("Pop : 2\n");
printf("Display : 3\n");
printf("Exit : 4\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("\nINVALID CHOICE\n");
}
}
}
OUTPUT:
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 1
Enter the item to pushed : 32
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 3
32
17
34
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 2
Popped Element is : 32
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 2
Popped Element is : 17
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 2
Popped Element is : 34
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 3
Push : 1
Pop : 2
Display : 3
Exit : 4
Enter your choice : 4
#include<stdio.h>
#include<conio.h>
int gcd(int m,int n)
{
if(n==0)
{
return(m);
}
else if(n>m)
{
return(gcd(n,m));
}
else
{
return(gcd(n,m%n));
}
}
void main()
{
int k,m,n;
clrscr();
printf("Enter three numbers : \n");
scanf("%d %d %d",&k,&m,&n);
printf("\nGCD of(%d,%d,%d) = %d\n",k,m,n,gcd(k,gcd(m,n)));
getch();
}
OUTPUT:
Enter three numbers :
4
6
8
GCD of(4,6,8) = 2
11. Program to insert the elements {5,7,0,6,3,9} into circular queue and delete 3 elements from it
(using linked list implementation).
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *front = NULL;
struct node *rear = NULL;
void enqueue(int d)
{
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((rear==NULL)&&(front==NULL))
{
front = rear = n;
rear->next = front;
}
else
{
rear->next = n;
rear = n;
n->next = front;
}
}
void dequeue()
{
struct node* t;
t = front;
if((front==NULL)&&(rear==NULL))
{
printf("\nQueue Underflow\n");
}
else if(front == rear)
{
front = rear = NULL;
free(t);
}
else
{
front = front->next;
rear->next = front;
free(t);
}
}
void print()
{
struct node* t;
t = front;
if((front==NULL)&&(rear==NULL))
{
printf("\nQueue is Empty\n");
}
else
{
printf("\nQueue : ");
do
{
printf("%d ",t->data);
t = t->next;
}
while(t != front);
printf("\n");
}
}
int main()
{
int opt,data;
clrscr();
do
{
printf("\nInsert : 1\n");
printf("Delete : 2\n");
printf("Display : 3\n");
printf("Exit : 4\n");
printf("Enter your choice : ");
scanf("%d",&opt);
switch(opt)
{
case 1 : printf("\nEnter the item to be inserted : ");
scanf("%d",&data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
print();
break;
case 4:
break;
default : printf("\nInvalid Choice\n");
}
}
while(opt!=4);
return 0;
}
OUTPUT:
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 1
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 3
Queue : 5 7 0 6 3 9
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 2
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 3
Queue : 6 3 9
Insert : 1
Delete : 2
Display : 3
Exit : 4
Enter your choice : 4
#include<stdio.h>
#include<conio.h>
#define max 100
int top=-1, a[max];
void push(char x)
{
a[++top]=x;
}
char pop()
{
if(top==-1)
return -1;
else
return a[top--];
}
int prcd(char c)
{
if(c=='(')
return 0;
else if(c=='+'||c=='-')
return 1;
else if(c=='*'||c=='/')
return 2;
}
int infixtopostfix(char infix[max],char postfix[max])
{
char temp,x;
int i=0,j=0;
while(infix[i]!='\0')
{
temp=infix[i];
if(isalnum(temp))
{
postfix[j++]=temp;
}
else if(temp=='(')
push(temp);
else if(temp==')')
{
while((x=pop())!='(')
{
postfix[j++]=x;
}
}
else
{
while(prcd(a[top])>=prcd(temp))
{
postfix[j++]=pop();
}
push(temp);
}
i++;
}
while(top!= -1)
postfix[j++]=pop();
postfix[j]='\0';
return 0;
}
int main()
{
char infix[max],postfix[max];
clrscr();
printf("Enter the infix expression : ");
gets(infix);
infixtopostfix(infix, postfix);
OUTPUT:
Enter the infix expression : x^y/(5*z)+2
#include<stdio.h>
#include<conio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int 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)
{
case '+':
{
n3 = n1 + n2;
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 is : %d\n",exp,pop());
return 0;
}
OUTPUT:
Enter the expression : 53+82-*
14. Program to create binary search tree with the elements {2,5,1,3,9,0,6} and perform inorder ,
preorder and postorder traversal.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
{
printf("\nRepeated Entry Error, Value Rejected\n");
return;
}
if(num<(*p)->data)
{
insert(&((*p)->left),num);
}
else
{
insert(&((*p)->right),num);
}
}
return;
}
void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d ",p->data);
inorder(p->right);
}
else
return;
}
void preorder(struct node *p)
{
if(p!=NULL)
{
printf("%d ",p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d ",p->data);
}
else
return;
}
OUTPUT:
Insert a new node in the Binary Tree : 1
15. Program to sort the following elements using heap sort {9,16,32,8,4,1,5,8,0}.
#include<stdio.h>
#include<conio.h>
heap[LastElement]=CopyVariable;
heap[0]--;
Adjust(heap,1);
}
printf("\nSorted Array : ");
for(i=1;i<=NumberofElements;i++)
{
printf("%d ",heap[i]);
}
printf("\n");
return 0;
}
OUTPUT:
Enter the number of elements present in the unsorted array : 9
Sorted array : 0 1 4 5 8 8 9 16 32
#include<stdio.h>
#include<conio.h>
int LENGTH(char * str)
{
int i=0;
while(str[i]!='\0')
i++;
return i;
}
void CONCAT(char *str1,char *str2)
{
int i=0,j=0;
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
}
str1[i]='\0';
}
void SUBSTR(char str[],int pos ,int len)
{
char sub[50];
int slen=LENGTH(str);
int p,j,max_ext;
if(pos>slen)
{
printf("\nInvalid Position\n");
return;
}
max_ext=slen-pos+1;
if(len>max_ext)
{
printf("\nInvalid Substring Length\n");
return;
}
p=pos-1;
for(j=0;j<len;j++)
{
sub[j]=str[p];
p++;
}
sub[j]='\0';
printf("\nSubstring : %s",sub);
printf("\n");
}
void REPLACE(char str[],char substr[],char replacestr[])
{
char output[50];
int i=0,j=0,flag=0,start=0;
while(str[i]!='\0')
{
if(str[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]=str[i];
for(j=0;j<LENGTH(replacestr);j++)
{
output[i]=replacestr[j];
i++;
}
for(j=start + LENGTH(substr);j<LENGTH(str);j++)
{
output[i]=str[j];
i++;
}
output[i]='\0';
printf("\nReplaced String : %s",output);
printf("\n");
}
else
printf("%s is not a substring of %s \n",substr,str);
}
void main()
{
char str1[50],str2[50],substr[50],repstr[50];
int len,pos,ch;
clrscr();
while(1)
{
printf("\nFor Length of the String : 1");
printf("\nFor String Concatenation : 2");
printf("\nFor Extracting a Substring : 3");
printf("\nFor Replacing a String : 4");
printf("\nExit : 5");
printf("\nEnter your choice : ");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{
case 1:printf("\nEnter the string : ");
gets(str1);
printf("\nThe length of the string is : %d",LENGTH(str1));
printf("\n");
break;
case 2 :printf("\nEnter the first string : ");
gets(str1);
printf("\nEnter the second string : ");
gets(str2);
CONCAT(str1,str2);
printf("\nConcatenated String : %s",str1);
printf("\n");
break;
case 3:printf("\nEnter the string : ");
gets(str1);
printf("\nEnter the position of substring : ");
scanf("%d",&pos);
printf("\nEnter the length of the substring : ");
scanf("%d",&len);
SUBSTR(str1,pos,len);
break;
case 4:printf("\nEnter the string : ");
gets(str1);
printf("\nEnter the string to be removed : ");
gets(substr);
printf("\nEnter the string to be replaced : ");
gets(repstr);
REPLACE(str1,substr,repstr);
break;
case 5:exit(0);
default:printf("\nInvalid Choice\n");
break;
}
}
}
OUTPUT:
For Length of the String : 1
For String Concatenation : 2
For Extracting a Substring : 3
For Replacing a String : 4
Exit : 5
Enter your choice : 1
Substring : low