Você está na página 1de 44

LIST OF PRACTICALS OF DATA STRUCTURES

Paper Code: ETCS 257


P
C

Paper: Data Structure Lab.

0
2

1. Perform various operations on arrays using switch statements


a)
b)
c)
d)
e)
f)
g)

Traversing
Insertion through position
Deletion through position
Searching ( Linear and Binary)
Bubble Sort
Merging
Reversing

2. Store a two D Array in memory and check whether it is stored as row


measure or column measure.
3. Generate a try diagonal sparse matrix, store its memory and derive a
formula for its address retrieval.
4. Generate the lower and upper sparse matrix using for loop, and store it in
two one-ray. Derive a formula for its address retrieval.
5. Implement stack operation (Push and POP) using arrays.
6. Implement queue operation (insertion and delete) using arrays.
7. Write a program to add two polynomials.
8. Implement stack operation (Push and POP) using Linked List.
9. Implement queue operation (insertion and delete) using Linked List.
10. Implement Circular linked list (Insertion & Deletion)
11. Implement Evaluation of postfix expression?
12. Implement Infix to Postfix Conversion?

13. Implement Preorder, Inorder, Postorder traversing of a trees using


Recursion.
14. WAP to Implement Doubly Linked List.
15. WAP to Sort an Array of elements using Bubble, Selection & Insertion
Sort.

1. Perform various operations on arrays using switch statements


h) Traversing
i) Insertion through position
j) Deletion through position
k) Searching ( Linear and Binary)
l) Bubble Sort
m)
Merging
n) Reversing
#include<stdio.h>
#include<conio.h>
#include<process.h>
void insert(int*,int pos,int num);
void delet(int*,int pos);
void revers(int*);
void main()
{
int i,temp,j,n,n2,mid,lower=0,upper=5,flag=1;
clrscr();
int ram[6];
int hr[8];
int ch;
while(1)
{
printf("\nThe menu for operations are:->");
printf("\n\n1.Traversing");
printf("\n\n2.Insertion");
printf("\n\n3.Deletion");
printf("\n\n4.Reversing");
printf("\n\n5.Linear Search");
printf("\n\n6.Binary Search");
printf("\n\n7.Bubble Sorting");
printf("\n\n8.exit");
printf("\n\nenter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenter the array element");
for(i=0;i<=5;i++)
scanf("\n%d",&ram[i]);
printf("\n\nthe array elements are :-> ");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
break;

case 2:
printf("\nenter the array element");
for(i=0;i<=5;i++)
scanf("\n%d",&ram[i]);
printf("\n\nthe array elements are :-> ");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
insert(ram,3,9);
printf("\n\nthe array elements after insertion are:->");
for(i=0;i<=6;i++)
printf(" %d",ram[i]);
break;
case 3:
printf("\nenter the array element");
for(i=0;i<=6;i++)
scanf("\n%d",&ram[i]);
printf("\n\nthe array elements are :-> ");
for(i=0;i<=6;i++)
printf(" %d",ram[i]);
delet(ram,3);
printf("\n\nthe array element after deletion are:->");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
break;
case 4:
printf("\nenter the array element");
for(i=0;i<=5;i++)
scanf("\n%d",&ram[i]);
printf("\n\nthe array elements are :-> ");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
revers(ram);
printf("\n\nthe array element after reversing are:->");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
break;
case 5:
printf("\nenter the array element");
for(i=0;i<=5;i++)
scanf("\n%d",&ram[i]);
printf("\n\nthe array elements are :-> ");
for(i=0;i<=5;i++)

printf(" %d",ram[i]);
printf("\n\nenter the element to be search by ls");
scanf("%d",&n);
for(i=0;i<=5;i++)
{
if(ram[i]==n)
break;
}
if(i==6)
printf("\n\nthe element %d is not in array",n);
else
printf("\n\nthe element %d is at %d position",n,i+1);
break;
case 6:
printf("\n\nenter the element of a new array in sorted order");
for(i=0;i<=7;i++)
scanf("%d",&hr[i]);
for(i=0;i<=7;i++)
printf(" %d",hr[i]);
printf("\n\nenter the element to be search by bs");
scanf("%d",&n2);
for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2)
{
if(hr[mid]==n2)
{
printf("\n\nthe no. is present at %d position of array",(mid+1));
flag=0;
break;
}
if(hr[mid]>n2)
upper=mid-1;
else
lower=mid+1;
}
if(flag)
printf("\n\nthe element is not in array");
break;
case 7:
printf("\n\nthe element before sorting are:->");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
for(i=0;i<=4;i++)
{
for(j=0;j<=4-i;j++)

{
if(ram[j]>ram[j+1])
{
temp=ram[j];
ram[j]=ram[j+1];
ram[j+1]=temp;
}
}
}
printf("\n\nthe array after sorting is:->");
for(i=0;i<=5;i++)
printf(" %d",ram[i]);
break;
case 8:
exit(0);
}
getch();
}
}
void insert(int*ram,int pos,int num)
{
int i;
for(i=6;i>=pos;i--)
{
ram[i]=ram[i-1];
}
ram[i]=num;
}
void delet(int*ram,int pos)
{
int i;
for(i=pos;i<=6;i++)
{
ram[i-1]=ram[i];
}
}
void revers(int*ram)
{
int temp;
for( int i=0;i<=5/2;i++)
{
temp=ram[i];
ram[i]=ram[5-i];
ram[5-i]=temp;
}

2. Store a two D Array in memory and check whether it is stored as


row measure or column measure.
#include<stdio.h>
#include<conio.h>
void main ()
{
int a[3][3];
int i, j, b;
clrscr();
printf("\nEnter the values of element in the array \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the value in the element a[%d][%d]",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n the array element are:");
for(i=0;i<=3;i++)
{
printf("\n");
for(j=0;j<=3;j++)
{
printf(" %d",a[i][j]);
}
}
printf("\nCOMPARISON THROUGH ADDRESS ");
if(a == (&a[0][1] - 1))
printf("\nROW MAJOR FORM");
else
printf("\nCOLUMN MAJOR FORM");
getch();
}
*/ output
Enter
Enter
Enter
Enter

the
the
the
the

values of element in the array


value in the element a[0][0]1
value in the element a[0][1]5
value in the element a[0][2]5

Enter
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the
the

value
value
value
value
value
value

in
in
in
in
in
in

the
the
the
the
the
the

element
element
element
element
element
element

a[1][0]45
a[1][1]5
a[1][2]7
a[2][0]8
a[2][1]9
a[2][2]7

the array element are:


1 5 5 45
45 5 7 8
8 9 7 0
0 344 0 0
COMPARISON THROUGH ADDRESS
ROW MAJOR FORM

3. Generate a try diagonal sparse matrix, store it memory and


derive a formula for its address retrieval.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int i,j;
int *p;
clrscr();
printf("\nEnter the values of element in the sparse tridiagonal matrix \n");
for (i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("\nSprarse Matrix Is\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j==i)
printf(" %d",a[(2*i)+j]);
else
if(j==i-1)
printf(" %d",a[(2*i)+j]);
else
if(j==i+1)
printf(" %d",a[(2*i)+j]);
else
printf(" 0");
}
printf("\n");
}
p=a;
printf("\nThe address of element a[0][0] is %u ",p);
printf("\nEnter the index of element you want to get address [i][j]: ");
scanf("%d %d",&i,&j);
printf("\nThe address of element a[%d][%d] is %u ",i,j,p+((2*i)+j));
getch()

/*output is
Enter the values of element in the sparse tridiagonal matrix
1
2
3
4
5
6
7
8
9
1
Sprarse Matrix Is
1 2 0 0
3 4 5 0
0 6 7 8
0 0 9 1
The address of element a[0][0] is 65502
Enter the index of element you want to get address [i][j]: 0 1
The address of element a[0][1] is 65504
*/

4. Generate the lower and upper sparse matrix using for loop, and
store it in two one-ray. Derive a formula for its address retrieval.
#include<conio.h>
#include<stdio.h>
void main()
{
int a[4][4],b[10],i,j,ch,k=0;
clrscr();
printf("Do u want to enter upper triangular (press 1) or \nlower
triangular(press 2) :");
scanf("%d",&ch);
printf("\n\nEnter the elements of the array : \n\n");
/* for upper matrix*/
if(ch==1)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(i>=j)
{
printf("Enter the value of element a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
else
a[i][j]=0;
}
/*for column major storage in 1-D array*/
for(j=0,k=0;j<4;j++)
for(i=0;i<4;i++)
if(i>=j)
{
b[k]=a[i][j];
k++;
}
}
/* for lower matrix*/
if(ch==2)

{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(j>=i)
{
printf("Enter the value of element a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
else
a[i][j]=0;
}
/*for column major storage in 1-D array*/
for(j=0,k=0;j<4;j++)
for(i=0;i<4;i++)
if(j>=i)
{
b[k]=a[i][j];
k++;
}
}
printf("\n\nYour 2-D array is : \n\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(" %d ",a[i][j]);
printf("\n\n");
}
printf("\n\nYour 1-D array is : \n\n");
for(k=0;k<10;k++)
printf(" %d ",b[k]);
printf("Enter the index of the non zero element of a[i][j]: ");
scanf("%d %d",&i,&j);
/*for address manupulation */
if(ch==1)
{
printf("FOR UPPERCASE MATRIX ONL\n");
printf("\nBase address is : %u\n",b);
printf("The address of the element is : %u " ,b+(((j*(9-i))/2)+i-j+1));

}
getch();
}
/*
output is
Do u want to enter upper triangular (press 1) or
lower triangular(press 2) :1
Enter the elements of the array :
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the
the
the
the
the
the

value
value
value
value
value
value
value
value
value
value

of
of
of
of
of
of
of
of
of
of

element
element
element
element
element
element
element
element
element
element

a[0][0]
a[1][0]
a[1][1]
a[2][0]
a[2][1]
a[2][2]
a[3][0]
a[3][1]
a[3][2]
a[3][3]

:
:
:
:
:
:
:
:
:
:

1
1
1
1
1
1
1
1
1
1

Your 2-D array is :


1

Your 1-D array is :


1 1 1 1 1 1 1 1 1 1
Enter the index of the non zero element of a[i][j]: 2 3
Base address is : 65468
The address of the element is : 65486

5. Implement stack operation (Push and POP) using arrays.


#include<stdio.h>
#include<conio.h>
#define maxsize 5
struct stack
{
int ram[maxsize];
int top;
};
void inistack(struct stack*);
void push(struct stack*,int num);
int pop(struct stack*);
void main()
{
struct stack s;
int i;
clrscr();
inistack(&s);
push(&s,2);
push(&s,3);
push(&s,75);
push(&s,5);
push(&s,8);
push(&s,9);
i=pop(&s);
printf("\n\nitem is poped:%d",i);
i=pop(&s);
printf("\n\nitem is poped:%d",i);
i=pop(&s);
printf("\n\nitem is poped:%d",i);
getch();
}
void inistack(struct stack*s)
{
s->top=-1;
}
void push(struct stack*s,int num)
{
if(s->top==maxsize-1)
{
printf("\n\nstack is full");
return;
}

s->top++;
s->ram[s->top]=num;
}
int pop(struct stack*s)
{
int data;
if(s->top==-1)
{
printf("\n\nstack is empty");
return NULL;
}
data=s->ram[s->top];
s->top--;
return data;
}
/* output
stack is full
item is poped:8
item is poped:5
item is poped:75
*/

6. Implement queue operation (insertion and delete) using arrays.


#include<stdio.h>
#include<conio.h>
#define max 5
void insq(int*,int,int*,int*);
int delq(int*,int*,int*);
void main()
{
int ram[max];
int front=-1,rear=-1,i;
clrscr();
insq(ram,5,&front,&rear);
insq(ram,6,&front,&rear);
insq(ram,7,&front,&rear);
insq(ram,8,&front,&rear);
insq(ram,9,&front,&rear);
insq(ram,2,&front,&rear);
i=delq(ram,&front,&rear);
printf("\n\nitem is deleted",i);
i=delq(ram,&front,&rear);
printf("\n\nitem is deleted",i);
i=delq(ram,&front,&rear);
printf("\n\nitem is deleted",i);
getch();
}
void insq(int*ram,int num,int*pfront,int*prear)
{
if(*prear==max-1)
{
printf("\n queue is full");
return;
}
(*prear)++;
ram[*prear=num];
if(*pfront==-1)
*pfront=0;
}
int delq(int*ram,int*pfront,int*prear)
{
int data;
if (*pfront==-1)
{
printf("\n queue is empty");

return NULL;
}
data=ram[*pfront];
ram[*pfront]=0;
if(*pfront==*prear)
*pfront=*prear=-1;
else
(*pfront)++;
return data;
}
/* output
item is deleted
item is deleted
item is deleted
*/

7. Write a program to add two polynomials.


#include<stdio.h>
#include<conio.h>
# define max 15
struct term
{
int coff;
int exp;
};
struct poly
{
struct term t[15];
int not;
};
void inipoly(struct poly*);
void polyassign(struct poly*,int c,int e);
struct poly polyadd(struct poly,struct poly);
void display(struct poly);
void main()
{
struct poly p1,p2,p3;
clrscr();
inipoly(&p1);
inipoly(&p2);
inipoly(&p3);
polyassign(&p1,1,7);
polyassign(&p1,2,6);
polyassign(&p1,3,5);
polyassign(&p1,4,4);
polyassign(&p2,1,4);
polyassign(&p2,1,3);
polyassign(&p2,1,5);
polyassign(&p2,2,2);
p3=polyadd(p1,p2);
printf("\n\nfirst polynomial is:\n\n");
display(p1);

printf("\n\nsecond polynomial is:\n\n");


display(p2);
printf("\n\nres polynomial is:\n\n");
display(p3);
getch();
}
void inipoly(struct poly*p)
{
int i;
p->not=0;
for(i=0;i<max;i++)
{
p->t[i].coff=0;
p->t[i].exp=0;
}
}
void polyassign(struct poly*p,int c,int e)
{
p->t[p->not].coff=c;
p->t[p->not].exp=e;
(p->not)++;
}
void display(struct poly p)
{
int flag=0,i;
for(i=0;i<p.not;i++)
{
if(p.t[i].exp!=0)
printf("%dx^%d+",p.t[i].coff,p.t[i].exp );
else
{
printf("%d",p.t[i].coff);
flag=1;
}
}
if(!flag)
printf("\b\b ");
}
struct poly polyadd(struct poly p1,struct poly p2)
{
int i,j,c;
struct poly p3;
inipoly(&p3);

if(p1.not>p2.not)
c=p1.not;
else
c=p2.not;
for(i=0,j=0;j<=c;p3.not++)
{
if(p1.t[i].coff==0&&p2.t[j].coff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.not].coff=p1.t[i].coff+p2.t[j].coff;
p3.t[p3.not].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.not].coff=p1.t[i].coff;
p3.t[p3.not].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.not].coff=p2.t[j].coff;
p3.t[p3.not].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
/* output
first polynomial is:
1x^7+2x^6+3x^5+4x^ +
second polynomial is:
1x^4+1x^3+1x^5+2x^ +
res polynomial is:

1x^7+2x^6+3x^5+5x^4+1x^3+1x^5+2x^ +
*/

8. Implement stack operation (Push and POP) using Linked List.


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
void push(struct node **, int);
int pop(struct node **);
void delstack(struct node **);
void main()
{
struct node *s=NULL;
int i;
clrscr();
push(&s,14);
push(&s,-3);
push(&s,18);
push(&s,29);
push(&s,31);
push(&s,16);
i=pop(&s);
printf("\nItem popped:%d",i);
i=pop(&s);
printf("\nItem popped:%d",i);
i=pop(&s);
printf("\nItem popped:%d",i);
delstack(&s);
getch();
}
void push(struct node **top, int item)
{
struct node *temp;

temp=(struct node *)malloc(sizeof(struct node));


if(temp == NULL)
printf("\nStack is full");
temp->data=item;
temp->link= *top;
*top=temp;
}
int pop(struct node **top)
{
struct node *temp;
int item;
if( *top==NULL)
{
printf("\nStack is empty");
return NULL;
}
temp= *top;
item=temp->data;
*top=(*top)->link;
free(temp);
return item;
}
void delstack(struct node **top)
{
struct node *temp;
if(*top == NULL)
return;
while(*top!=NULL)
{
temp= *top;
*top=(*top)->link;
free(temp);
}
}
/*OUTPUT:
Item popped:16
Item popped:31

Item popped:29
*/

9. Implement queue operation (insertion and delete) using Linked


List.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
struct queue
{
struct node *front;
struct node *rear;
};
void initqueue(struct queue *);
void addq(struct queue *, int);
int delq(struct queue *);
void delqueue(struct queue *);
void main()
{
struct queue a;
int i;
clrscr();
initqueue(&a);
addq(&a,11);
addq(&a,-8);
addq(&a,23);
addq(&a,19);
addq(&a,15);
addq(&a,16);
addq(&a,28);
i=delq(&a);
printf("\nItem extracted:%d",i);
i=delq(&a);
printf("\nItem extracted:%d",i);

delqueue(&a);
getch();
}
void initqueue(struct queue *q)
{
q->front=q->rear=NULL;
}
void addq(struct queue *q, int item)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if (temp == NULL)
printf("\nQueue is full");
temp->data=item;
temp->link=NULL;
if(q->front == NULL)
{
q->rear=q->front=temp;
return;
}
q->rear->link=temp;
q->rear=q->rear->link;
}
int delq(struct queue *q)
{
struct node *temp;
int item;
if(q->front==NULL)
{
printf("\nQueue is empty");
return NULL;
}
item=q->front->data;
temp=q->front;
q->front=q->front->link;
free(temp);
return item;
}
void delqueue(struct queue *q)

{
struct node *temp;
if(q->front==NULL)
return;
while(q->front!=NULL)
{
temp=q->front;
q->front=q->front->link;
free(temp);
}
}
/*OUTPUT
Item extracted:11
Item extracted:-8
Item extracted:23*/

10. Implement Circular linked list (Insertion & Deletion)


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
void addcirq(struct node **, struct node **, int);
int delcirq(struct node **,struct node **);
void cirq_display(struct node *);
void main()
{
struct node *front, *rear;
front=rear=NULL;
addcirq(&front,&rear,10);
addcirq(&front,&rear,17);
addcirq(&front,&rear,18);
addcirq(&front,&rear,5);
addcirq(&front,&rear,30);
addcirq(&front,&rear,15);
clrscr();
printf("Before deletion:\n");
cirq_display(front);
delcirq(&front,&rear);
delcirq(&front,&rear);
delcirq(&front,&rear);
printf("\n\nAfter deletion:\n");
cirq_display(front);
getch();
}
void addcirq(struct node **f, struct node **r, int item)
{
struct node *q;
q=malloc(sizeof(struct node));

q->data=item;
if(*f==NULL)
*f=q;
else
(*r)->link=q;
*r=q;
(*r)->link=*f;
}
int delcirq(struct node **f, struct node **r)
{
struct node *q;
int item;
if(*f==NULL)
printf("queue is empty");
else
{
if(*f==*r)
{
item=(*f)->data;
free(*f);
*f=NULL;
*r=NULL;
}
else
{
q=*f;
item=q->data;
*f=(*f)->link;
(*r)->link=*f;
free(q);
}
return(item);
}
return NULL;
}
void cirq_display(struct node *f)
{
struct node *q=f,*p=NULL;
while(q!=p)
{

printf("%d",q->data);
q=q->link;
p=f;
}
}
/*OUTPUT
Before deletion:
10 17 18 5 30 15
After deletion:
5 30 15
*/

11. Implement Evaluation of postfix expression?


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#define MAX 50
struct postfix
{
int stack[MAX];
int top, nn;
char *s;
};
void initpostfix(struct postfix *);
void setexpr(struct postfix *, char *);
void push(struct postfix *, int);
int pop(struct postfix *);
void calculate(struct postfix *);
void show(struct postfix);
void main()
{
struct postfix q;
char expr[MAX];
clrscr();
initpostfix(&q);
printf("\nEnter postfix expression to be evaluated:");
gets(expr);
setexpr(&q,expr);
calculate(&q);
show(q);
getch();
}
void initpostfix(struct postfix *p)
{
p->top=-1;

}
void setexpr(struct postfix *p, char *str)
{
p->s=str;
}
void push(struct postfix *p, int item)
{
if(p->top==MAX-1)
printf("\nStack is full");
else
{
p->top++;
p->stack[p->top]=item;
}
}
int pop(struct postfix *p)
{
int data;
if(p->top==-1)
{
printf("\nStack is empty");
return NULL;
}
data=p->stack[p->top];
p->top--;
return data;
}
void calculate(struct postfix *p)
{
int n1, n2,n3;
while(*(p->s))
{
if(*(p->s)==' '||*(p->s)=='\t')
{
p->s++;
continue;
}
if(isdigit(*(p->s)))
{
p->nn=*(p->s)-'0';
push(p,p->nn);

}
else
{
n1=pop(p);
n2=pop(p);
switch(*(p->s))
{
case'+':
n3=n2+n1;
break;
case'-':
n3=n2-n1;
break;
case'/':
n3=n2/n1;
break;
case'*':
n3=n2*n1;
break;
case'%':
n3=n2%n1;
break;
case'$':
n3=pow(n2,n1);
break;
default:
printf("Unknown operator");
exit(1);
}
push(p,n3);
}
p->s++;
}
}
void show(struct postfix p)
{

p.nn=pop(&p);
printf("Result is:%d",p.nn);
}
/*OUTPUT
Enter postfix expression to be evaluated:4 2 $ 3 * 3 - 8 4 / 1 1 + / +
Result is:46
*/

12. Implement Infix to Postfix Conversion?


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MAX 50
struct infix
{
char target[MAX];
char stack[MAX];
char *s, *t;
int top;
};
void initfix(struct infix *);
void setexpr(struct infix *, char *);
void push(struct infix *);
char pop(struct infix *);
void convert(struct infix *);
int priority(char);
void show(struct infix);
void main()
{
struct infix p;
char expr[MAX];
initfix(&p);
clrscr();
printf("\nEnter an expression in infix form:");
gets(expr);
setexpr(&p, expr);
convert(&p);
printf("The postfix expression is:");
show(p);
getch();
}

void initfix(struct infix *p)


{
p->top=-1;
strcpy(p->target," ");
strcpy(p->stack," ");
p->t=p->target;
p->s=" ";
}
void setexpr(struct infix *p, char *str)
{
p->s=str;
}
void push(struct infix *p, char c)
{
if(p->top==MAX)
printf("\nStack is full\n");
else
{
p->top++;
p->stack[p->top]=c;
}
}
char pop(struct infix *p)
{
if(p->top==-1)
{
printf("\nStack is empty\n");
return-1;
}
else
{
char item=p->stack[p->top];
p->top--;
return item;
}
}
void convert(struct infix *p)
{
char opr;

while(*(p->s))
{
if(*(p->s)==' '||*(p->s)=='\t')
{
p->s++;
continue;
}
if(isdigit(*(p->s))||isalpha(*(p->s)))
{
while(isdigit(*(p->s))||isalpha(*(p->s)))
{
*(p->t)=*(p->s);
p->s++;
p->t++;
}
}
if(*(p->s)=='(')
{
push(p, *(p->s));
p->s++;
}
if(*(p->s)=='*'||(p->s)=='/'||
*(p->s)=='%'||*(p->s)=='_'||*(p->s)=='$')
{
if(p->top!=-1)
{
opr=pop(p);
while(priority(opr)>=priority(*(p->s)))
{
*(p->t)=opr;
p->t++;
opr=pop(p);
}
push(p, opr);
push(p,*(p->s));
}
else
push(p, *(p->s));
p->s++;
}
if(*(p->s)==')')
{
opr=pop(p);

while((opr)!='(')
{
*(p->t)=opr;
p->t++;
opr=pop(p);
}
p->s++;
}
}
while(p->top!=-1)
{
char opr=pop(p);
*(p->t)=opr;
p->t++;
}
*(p->t)='\0';
}
int priority(char c)
{
if(c=='$')
return3;
if(c=='*'||c=='/'||c=='%')
return2;
else
{
if(c=='+'||c=='-')
return 1;
else
return 0;
}
}
void show(struct infix p)
{
printf("%s",p.target);
}

/*OUTPUT
Enter an expression in infix form: 4 $ 2 * 3 - 3 + 8 / 4 / ( 1 + 1
Stack is empty.
The postfix expression is: 42$3*3-84/11+/+*/

13. Implement Preorder, Inorder, Postorder traversing of a trees


using Recursion.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};
void insert( struct btreenode **,int);
void inorder( struct btreenode *);
void preorder( struct btreenode *);
void postorder( struct btreenode *);
void main()
{
struct btreenode *bt;
int req, i=1, num;
bt=NULL;
clrscr();
printf("Specify the number of items to be inserted:");
scanf("%d", &req);
while(i++ <= req)
{
printf("Enter the data:");
scanf("%d", &num);
insert(&bt, num);
}
printf("\nIn-order Traversal:");
inorder(bt);
printf("\nPre-order Traversal:");
preorder(bt);
printf("\nPost-order Traversal:");
postorder(bt);
getch();
}
void insert( struct btreenode **sr, int num)
{
if( *sr==NULL)
{

*sr=malloc(sizeof (struct btreenode));


( *sr)->leftchild=NULL;
( *sr)->data=num;
( *sr)->rightchild=NULL;
return;
}
else
{
if(num<( *sr)->data)
insert(&(( *sr)->leftchild), num);
else
insert(&(( *sr)->rightchild), num);
}
return;
}
void inorder (struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
printf("\t%d", sr->data);
inorder(sr->rightchild);
}
else
return;
}
void preorder (struct btreenode *sr)
{
if(sr!=NULL)
{
printf("\t%d", sr->data);
preorder(sr->leftchild);
preorder(sr->rightchild);
}
else
return;
}
void postorder( struct btreenode *sr)
{
if(sr!=NULL)
{
postorder(sr->leftchild);
postorder(sr->rightchild);

printf("\t%d", sr->data);
}
else
return;
}
/*OUTPUT
Specify the number of items to be inserted:5
Enter the data:1
Enter the data:2
Enter the data:3
Enter the data:4
Enter the data:5
In-order Traversal:
1
Pre-order Traversal: 1
Post-order Traversal: 5
*/

2
2
4

3
3
3

4
4
2

5
5
1

Você também pode gostar