DS PGMS
DS PGMS
main()
{
int a[10],item,n,i,u,ctr=0,pos;
clrscr();
printf("\nEnter size:\t");
scanf("%d",&n);
printf("\nEnter elements in ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter item for insert:\t");
scanf("%d",&item);
u=n-1;
if(a[ctr]>item)
pos=0;
else
{
while(ctr<u)
{
if((a[ctr]<=item)&&(item<=a[ctr+1]))
{
pos=ctr+1;
break;
}
ctr=ctr+1;
}
if(ctr==u)
pos=u+1;
}
ctr=u;
while(ctr>=pos)
{
a[ctr+1]=a[ctr];
ctr=ctr-1;
}
a[pos]=item;
printf("\nAfter insertion\n");
for(i=0;i<=n;i++)
printf("\n%d",a[i]);
getch();
}
Enter size: 4
Enter elements in ascending order
2
4
7
9
Enter item for insert: 5
After insertion
2
4
5
7
9
main()
{
int a[10],n,pos,i;
clrscr();
printf("\nEnter size:\t");
scanf("%d",&n);
printf("\nEnter elements in ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter position for delete:\t");
scanf("%d",&pos);
while(pos<=n-1)
{
a[pos]=a[pos+1];
pos=pos+1;
}
printf("\nAfter deletion\n");
for(i=0;i<n-1;i++)
printf("\n%d",a[i]);
getch();
}
Enter size: 4
Enter elements in ascending order
2
5
6
9
Enter position for delete: 2
After deletion
2
5
9
Step 1: Start
Step 2: Read n, item
Step 3: Set i=0
Step 4: Read array element
Step 5: Repeats steps 6 until i<n
Step 6: If a[i] =item, go to step 7
Step 7: Display value present, go to step 10
Step 8: If i=n, go to step 9
Step 9: Display unsuccessful search
Step 10: Stop
main()
{
int a[10],i,n,item;
clrscr();
printf("\nEnter size:\t");
scanf("%d",&n);
printf("\nEnter element\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter item for search:\t");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(a[i]==item)
{
printf("\nSearch is success");
break;
}
}
if(i==n)
printf("\nValue not present");
getch();
}
Enter size: 4
Enter element
1
5
2
6
Enter item for search: 5
Search is success
Step 1: Start
Step 2: Read array elements, n, no
Step 3: Set upp=n-1, low=i= 0
Step 4: Repeats steps 5 to 7 until i<n
Step 5: Set j=i+1
Step 6: Repeat steps 7 until j<n
Step 7: If a[i]>a[j] then
t=a[i]
a[i]=a[j]
a[j]=t end of repeat
Step 8: Repeats steps 9 to 14 until low<=upp
Step 9: mid = (low+upp)/2
Step 10: If a[mid]=no go to step 11,if not go to step 12
Step 11: Display value present go to step 18
Step 12: If a[mid]<no go to step 13,if not go to step 14
Step 13: Set low =mid+1
Step 14: If a[mid]>no go to step 15,if not go to step 16
Step 15: Set upp=mid-1 end of repeats
Step 16: If low>upp
Step 17: Display value cannot present
Step 18: Stop
main()
{
int a[10],n,i,low=0,upper,mid,no,j,t;
clrscr();
printf("\nEnter the size:\t");
scanf("%d",&n);
printf("\nEnter the numbers\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnter the search no's:\t");
scanf("%d",&no);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
upper=n-1;
while(low<=upper)
{
mid=(low+upper)/2;
If(a[mid]==no)
{
printf("\nValue is present");
break;
}
if(a[mid]<no)
low=mid+1;
if(a[mid]>no)
upper=mid-1;
if(low>upper)
printf("\nUnsuccessful search");
}
getch();
}
Step 1: Start
Step 2: Read n
Step 3: Set i=j=0
Step 4: Read array element
Step 5: Repeat steps 6 until i<n
Step 6: Repeat step 7 until j<n-1-i
Step 7: If a[j]>a [j+1] then,
c=a[j]
a[j]=a[j+1]
a[j+1]=c
Step 8: j=j+1 end of repeat
Step 9: i=i+1 end of repeat
Step 10: Display array
Step 11: Stop
Step 1: Start
Step 2: Set i=0,j=0,s=0
Step 3: 1: Spar[s][0]=m
2: Spar[s][1]=n
3: Spar[s][2]=Nzero
4: Set s=1
Step 4: Repeat while i<m and j<n go to step 5
Step 5: if mat[i][j]!=0 then, Otherwise go to step 6
Spar[s][0]=i+1
Spar[s][1]=j+1
Spar[s][2]=mat[i][j]
Set i=i+1,j=j+1,s=s+1
Step 6: Set i=i+1,j=j+1
Step 7: Display Spar[ ][ ]
Step 8: Stop
main()
{
int m,n,i,j,mat[10][10],spar[20][20],s,nzero=0;
clrscr();
printf("\nEnter value of row:\t");
scanf("%d",&m);
printf("\nEnter value for column:\t");
scanf("%d",&n);
printf("\nEnter matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&mat[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(mat[i][j]!=0)
nzero=nzero+1;
}
}
s=0;
spar[s][0]=m;
spar[s][1]=n;
spar[s][2]=nzero;
s=1;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(mat[i][j]!=0)
{
spar[s][0]=i+1;
spar[s][1]=j+1;
spar[s][2]=mat[i][j];
s=s+1;
}
}
}
printf("\nSparse matrix is\n");
for(i=0;i<=nzero;i++)
{
for(j=0;j<3;j++)
Enter matrix
1
0
0
5
Sparse matrix is
2 2 2
1 1 1
2 2 5
Data structure
Two dimensional array structure with each row representing
the nonzero elements of original matrixes.
Step 1: Start
Step 2: If sp1[0][0]=sp2[0][0] AND
sp1[0][1]=sp2[0][1] then go to step 3
Else
Output: Matrix are not of same order addition not possible .
Step 3: 1: Set I=J=K=1
2: Repeat while I<=sp1[0][2] AND j<=sp2[0][2]
1: If sp1[I][0]=sp2[J][0] then, elements in same row
I: If spi[I][1]=sp2[J][1] then, elements in same column
a: Set res[K][0]= sp1[I][0]
b: Set res[K][1]=sp1[J][1]
c: Set res[K][2]=sp1[I][2]+sp2[J][2]
d: Set I=I+1,J=J+1,K=K+1
II: Else
a: Ifsp1[I][1]< sp2[J][1] then if it elements of matrix is
Proceeding column
1: Set res[K][0]=sp1[I][0]
2: Set res[K][1]=sp1[I][1]
3: Set res[K][2]=sp1[I][2]
4: Set I=I+1,K=K+1
b: Else
1: Set res[K][0]=sp2[J][0]
2: Set res[K][1]=Sp2[J][1]
3: Set res[K][2]=Sp2[J][2]
4: Set J=J+1,K=K+1
2: Else
a: If sp1[I][0]<sp2[J][0] then
1: Set res[K][0]=sp1[I][0]
2: Set res[K][1]=sp1[I][1]
3: Set res[K][2]=sp1[a][2]
b: Else
1: Set res[K][0]=sp2[J][0]
2: Set res[K][1]=sp2[J][1]
3: Set res[K][2]=sp2[J][2]
4: Set K=K+1,J=J+1
Step 4: Repeat while I<=sp1[0][2]
1: Set res[K][0]=sp1[I][0]
2: Set res[K][1]=sp1[I][1]
3: Set res[k][2]=sp1[I][2]
4: Set K=K+1,I=I+1
PROGRAM
Input
Two polynomial P1 and p2 which are array of structure. n
and m are maximum number of term in p1 and p 2.
Output
A resultant polynomial p 3 which is the sum of p 1 and p 2.
Datastructure in use
Array of structure is use to store a polynomial. The
structure will contain the fields coefficient and exponent.
Step 1: Start
Step 2: Set i=j=k=1
Step 3: Repeat while i<=m AND j<=n
3.1: if p1[i].exp=p2[j].exp then
a: Set p3[k].exp=p1[i].exp
b: Set p3[k].c=p1[i].c+p2[j].c
c: Set k=k+1,i=i+1,j=j+1
3.2: Else
a: if p1[i].e<p2[j].e then
1: Set p3[k].e=p2[j].e
2: Set p3[k].c=p2[j].c
3: Set k=k+1,j=j+1
b: Else
1: p3[k].e=p1[i].e
2: p3[k].c=p1[i].c
3: k=k+1,i=i+1
Step 4: Repeat while i<=n
1: p3[k].e=p1[i].e
2: p3[k].c=p1[i].c
3: k=k+1,i=i+1
Step 5: Repeat while j<=m
1: Set p3[k].e=p2[j].e
2: Set p3[k].c=p2[j].c
3: Set k=k+1,j=j+1
Step 6: Display polynomial p3
Step 7: Stop
struct poly1
{
int c,e;
}p1[10];
struct poly2
{
int c,e;
}p2[10];
struct poly3
{
int c,e;
}p3[20];
main()
{
int m,n,i,j,k,s;
clrscr();
printf("\n*************OUTPUT**************");
printf("\n******POLINOMIAL ADDITION********\n");
printf("\n.................................\n");
printf("\nEnter value for m&n\n");
scanf("%d%d",&n,&m);
printf("\nEnter first polynomil\n");
for(i=0;i<n;i++)
{
printf("Coefficent:");
scanf("%d",&p1[i].c);
printf("\tExponential:");
scanf("%d",&p1[i].e);
}
printf("\nEnter second polynomil\n");
for(i=0;i<m;i++)
{
printf("\nCoefficent:");
scanf("%d",&p2[i].c);
printf("\tExponential:");
scanf("%d",&p2[i].e);
}
i=0;k=0;j=0;s=0;
while((i<=n)&&(j<=m))
{
if(p1[i].e==p2[j].e)
{
p3[k].e=p1[i].e;
p3[k].c=(p1[i].c+p2[j].c);
Coefficent:2
Exponential:3
Coefficent:4
Exponential:2
New polynomil is
Exponent :3 Coefficent :2
Exponent :2 Coefficent :7
Exponent :1 Coefficent :1
Step 1: Start
Step 2: Read choice to cho
Step 3: Repeat while 1
a: if cho=1 push() function call
b: if cho=2 pop() function call
c: if cho=3 display() function call
d: if cho=4 stop program
d: other wise wrong number
End of while
Step 4: Stop
Step 1: Start
Step 2: if top=max-1 then
Display “Over flow”
Step 3: Else
a: Read n
b: Repeat while i<n
c: read item to it
d: Set top=top+1
e: Set Staarr[top]=it
End of while and else
Step 4: Stop
Step 1: Start
Step 2: if top=-1 then
Display “Stack empty”
Step 3: Else
a: Deleated item is staarr[top]
b: Set top=top-1
End of else
Step 4: Stop
#define max20
int staarr[33],top=-1;
main()
{
int cho;
clrscr();
while(1)
{
printf("\nChoices are\n");
printf("\n1 Insert");
printf("\n2 Delete");
printf("\n3 Display");
printf("\n4 Exit");
printf("\nEnter your choice:\t");
scanf("%d",&cho);
switch(cho)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(0);
default:printf("\nWrong number");break;
}
}
getch();
}
push ()
{
int it,i,n,max;
if(top==(max-1))
printf("\nStack over flow");
else
{
printf("\nHow many number enter in to the stack:\t");
scanf("%d",&n);
printf("\nEnter inserted item\n");
for(i=0;i<n;i++)
{
scanf("%d",&it);
top=top+1;
staarr[top]=it;
pop()
{
if(top==-1)
printf("\nStack is empty");
else
{
printf("\nDeleted item is \t%d",staarr[top]);
top=top-1;
}
}
display()
{
int i;
if(top==-1)
printf("\nStack is empty");
else
{
for(i=top;i>-1;i--)
printf("%d\n",staarr[i]);
} /*end of else*/
}
Choices are
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice: 1
Choices are
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice: 3
2
1
Choices are
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice: 4
Step 1: Start
Step 2: Read choice to cho
Step 3: Repeat while 1
a: if cho=1 push() function call
b: if cho=2 pop() function call
c: if cho=3 display() function call
d: if cho=4 stop program
d: other wise wrong number
End of while
Step 4: Stop
Step 1: Start
Step 2: if rear=max-1 then
Display “Over flow”
Step 3: Else
a: Read n
b: If front=-1 then
Set front=0
c: Repeat while i<n
c: read item to it
d: Set rear=rear+1
e: Set qu[rear]=it
End of while and else
Step 4: Stop
#include<stdio.h>
#define size50;
int qu[10],rear=-1,front=-1;
main()
{
char c,cho;
clrscr();
while(1)
{
printf("\nChoice are\n");
printf("\n1 Insert");
printf("\n2 Deleate");
printf("\n3 Display");
printf("\n4 Exit");
printf("\nEnter your choice:\t");
scanf("%d",&cho);
switch(cho)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
default:exit(0);
}
}
getch();
}
push ()
{
int it,i,n,size;
printf("\nHow many elements enter to the queue:\t");
scanf("%d",&n);
if(rear==(size-1))
printf("\nQueue over flow");
else
{
if(front==-1)
front=0;
printf("\nEnter inserted item\n");
for(i=0;i<n;i++)
{
scanf("%d",&it);
rear=rear+1;
pop()
{
if(front==-1||front>rear)
printf("\nQueue is empty");
else
{
printf("\nDeleted item is \t%d",qu[front]);
front=front+1;
}
}
display()
{
int i;
if(front==-1)
printf("\nQueue is empty");
else
{
for(i=front;i<=rear;i++)
printf("%d\n",qu[i]);
}
}
Choice are
1 Insert
2 Deleate
3 Display
4 Exit
Enter your choice: 1
Choice are
1 Insert
2 Deleate
3 Display
4 Exit
Enter your choice: 3
1
2
Choice are
1 Insert
2 Deleate
3 Display
4 Exit
Enter your choice: 4
Step 1: Start
Step 2: Add the unique expression # in to the
stack and at the end of array infix
Step 3: Scan the symbol of array infix
one by one from left to right
Step 4: If the symbol is left parentheses (‘
then add to the stack
Step 5: The symbol is operant then add into array postfix
Step 6:
a: If the symbol is operator then pop the operator which have same
Precedence or higher precedence then the operator which occur
b: Add the pop operator to array postfix
c: Add the scan symbol in to the stack
Step 7:
a: If the symbol is right parentheses ‘)’ then pop all operator from
stack until the left parentheses ‘(‘ in the stack.
b: Remove the left parentheses from the stack
Step 8: If the symbol is # pop the entire symbol from stack and add them to
array postfix expect #
Step 9: Do the same process until # come in scanning array infix.
Step 10: Stop
Step 1: Start
Step 2: Add symbol # at the end of array postfix.
Step 3: Scan the symbol of array postfix one by one from left to right
Step 4: If the symbol is operant push in to stack.
Step 5: If the symbol is operator then pop the last two element of stack
And evaluate it us [top-1] and [top]
Step 6: Do the same process until the # comes in scanning
Step 7: Pop the element of stack which will be the value of evaluation of
postfix arithmetic expression
Step 8: Stop
#include<stdio.h>
#include<string.h>
#include<math.h>
#define max 200
long int top,stack[max],fox(),pop();
char choice='y',infix[max],postfix[max];
main()
{
long int h,a;
clrscr();
do
{
top=0;
printf("\nEnter infix expression\n");
fflush(stdin);
gets(infix);
printf("\nPostix expression is\n");
post();
printf("%s",postfix);
h=fox();
printf("\nResult of an expression\n");
printf("\nResult:%d",h);
printf("\n Do you want to continue(y/n):\t");
scanf("%c",&choice);
}while(choice=='y');
}
post()
{
long int len,p=0,precedence,i,e;
char a;
stack[top]='#';
len=strlen(infix);
infix[len]='#';
for(i=0;infix[i]!='#';i++)
{
e=space(infix[i]);
if(e==0)
{
switch(infix[i])
{
case '(':push(infix[i]);break;
case ')':while((a=pop())!='(')
push(int s)
{
if(top>max)
{
printf("\n Stack over flow");
exit(0);
}
else
{
top=top+1;
stack[top]=s;
}
}
space(char s)
{
if(s==' '||s=='\t'||s=='\0')
return(1);
else
return(0);
}
/*EVALUATION OF EXPRESSION*/
Postix expression is
4542^+*22^93/*-
Result of an expression
Result:72
Do you want to continue(y/n): n
Step 1: Start
Step 2: Read choice to c
Step 3: Repeat while 1
a: if cho=1 insert() function call
b: if cho=2 delete() function call
c: if cho=3 display() function call
d: if cho=4 stop program
d: other wise wrong number
End of while
Step 4: Stop
Step 1: Start
Step 2: Read item to it
Step 3: if front=0 AND rear=max-1 OR front=rear+1 then
Display “Queue over flow” go to step 7
Step 4: If front=-1 then
Set front=rear=0
Step 5: else
a: if rear=max-1 then
rear=0
b: else
rear=rear+1
Step 6: set cq[rear]=it
Step 7: Stop
Step 1: Start
Step 2: if front=-1 then
Display “Queue empty” go to step 6
Step 3: Deleted item is cq[front]
Step 4: If front=rear then
Set front=rear=-1
Step 5: else
Step 1: Start
Step 2: Set f=front and r=rear
Step 3: If f=-1 then
Display “Queue empty” go to step
Step 4: display “queue element are
Step 5: If f<=r then
a: repeat while f<=r
1: display cq[f]
2: Set f=f+1
End while and if
Step 6: else
1: Repeat while f<max-1
a: Display cq[f]
b: Set f=f+1
End of while
2: Set f=0
3: Repeat while f<,=r
a: Display cq[f]
b: f=f+1
End of while and else
Step 7: Stop
#include<stdio.h>
#define max 30
int cq[max];
int rear=-1;
int front=-1;
main()
{
int c;
clrscr();
while(1)
{
printf("\n1 Insert");
printf("\n2 Delete");
printf("\n3 Display");
printf("\n4 Exit");
printf("\nEnter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1:insert();break; /*insert function call*/
case 2:delete();break; /*delete function call*/
case 3:display();break; /*display functio call*/
case 4:exit(0);
default:printf("\nwrong number");
}
}
getch();
}
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice : 1
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice : 1
Input element for insertion: 3
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice : 3
1
2
3
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice : 4
We have a structure that contain two part number part and pointer part
There are three pointer variable named first,temp,head .
Step 1: Start
Step 2: Set first=null,choice=y,read choice to c
Step 3: If choice=c then
Create() function call
Step 4: Repeat while
1: read a
2: If a=1 then
insert() function call
3: If a=2 then
delete() function call
4: If a=3 then
display() function call
5: If a=4 stop
Step 5: Stop
Step 1: Start
Step 2: Read a
Step 3:
1: if a=1 begin() call
2: if a=2 endin() call
3: if a=3 appin() call
4: IF a=4 break
5: if a=5 stop
Step 4: Stop
Step 1: Start
Step 2: Read a
Step 3:
1: if a=1 begde() call
2: if a=2 endde() call
3: if a=3 appde() call
4: if a=4 break
5: if a=5 stop
Step 4: Stop
Step 1:
Start
Step 2:
Read c
Step 3:
Repeat while c=1
Step 4:
Allocate memory and read data in data field
Step 5:
If first!=null then
Set temp→ptr and temp=head
Step 6: else set first=temp=head end of while
Step 7: temp→ptr=null and temp=first
Step 8: stop
Step 1: start
Step 2: Set temp=first
Step 3: repeat while temp!=null
Step 4: Display temp→num
Step 5: Set temp=temp→ptr
Step 6: Stop
Subfunction of begin()
Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node
Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node
Step 4: Assign the null value to the pointer field of new node
Step 5: Make the link field of second last node to point to the last node
Step 6: Stop
Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node
Step 4: Make the link field of new node to point to node B
Step 5: Make the link field of node A to point to new node
Step 6: Stop
Step 1: Start
Step 2: If the first is not empty then check whether
the element belong to first node of the list
Step 3: Move the start pointer to the second node
Step 4: free the first node
Step 5: Stop
Step 1: Start
Step 2: If the first is not empty go on traversing till the last node
Step 3: Set the link field of second last node to null
Step 4: Free the last node
Step 5: Stop
Step 1: Start
Step 2: If the first is not empty then make the link field of
Node A point to the node B
Step 3: Free the node between A and B
Step 4: Stop
do
{
head=(NODE*)malloc(sizeof(NODE));
printf("\nEnter the data item:\t");
scanf("%d",&head->num);
if(first!=null)
{
temp->ptr=head;
temp=head;
}
else
first=temp=head;
printf("\nDo you want to continue (y/n)");
scanf("%s",&cho);
}while(cho=='y');
temp->ptr=null;
temp=first;
}
Choices are
1 Insert
2 Delete
3 Display
4 Exit
Enter your choice: 4
We have a structure that contain two part number part and pointer part
There are three pointer variable named top,temp,head .
Step 1: Start
Step 2: Set top=null, read choice to cho
Step 3: Repeat while
1: Ifcho=1 then
push() function call
2: If cho=2 then
pop() function call
3: If cho=3 then
display() function call
4: If cho=4 stop
Step 4: Sotp
Subfunction of push()
Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node
Step 4: Assign the value to the top to the pointer part of new node
Step 5: Assign head to top
Steo 6: Stop
Subfunction of pop()
Step 1: Start
Step 2: if the stack is not empty ,then,
Step 3: Set temp=top
Step 4: Set top=pointer part of top
Step 5: Free the temp
Step 6: Stop
Subfunction of display()
Step 1: Start
Step 2: Set temp=top
Step 3: If temp!=null then
Step 4: Display number part of temp
Step 5: Set temp=pointer part of temp
Step 6: Stop
#define null'\0'
#include<stdio.h>
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head,*temp,*top=null;
main()
{
int cho;
clrscr();
while(1)
{
printf("\nChoice are");
printf("\n1 Insert number");
printf("\n2 Delete number");
printf("\n3 Display number");
printf("\n4 Out of program");
printf("\nEnter your choice:\t");
scanf("%d",&cho);
switch(cho)
{
case 1:push();break;
case 2:pop();break;
case 3:display();break;
case 4:exit(0);
default:printf("\nWrong number");
} /*end switch*/
/*end of main*/
} /*end main*/
Choice are
1 Insert number
2 Delete number
3 Display number
4 Out of program
Enter your choice: 1
Enter item
1
2
3
Choice are
1 Insert number
2 Delete number
3 Display number
4 Out of program
Enter your choice: 3
Elements in stack
3
2
1
Choice are
1 Insert number
2 Delete number
3 Display number
4 Out of program
Enter your choice: 4
We have a structure that contain two part number part and pointer part
There are three pointer variable named front,temp,rear.
Step 1: Start
Step 2: Set front=rear=null, read choice to cho
Step 3: Repeat while
1: Ifcho=1 then
insert() function call
2: If cho=2 then
delete() function call
3: If cho=3 then
display() function call
4: If cho=4 stop
Step 4: Sotp
Step 1: Start
Step 2: Allocate memory for new node
Step 3: Assign the value to the data field of new node
Step 4: If rear=null then,
Set rear=temp
Assign value of rear to link field of new node
Else
Step 5: Assign the link value of rear to link field of new node
Step 6: Assign value of new node to the pointer part of rear
Step 7: Set rear=temp
Step 8: Stop
We have create a node that contain three part they are lchild,
rchild, number part . Temp, root, current nod are the three
structure variable. Set top=-1, max represent the maximum size.
Step 1:
Start
Step 2:
Read choice to n
Step 3:
Create tree, create ()
Step 4:
Repeat while
1: if n=1, preorder (root)
2: if n=2, inorder (root)
3: if n=3, postorder (root)
4: if n=4, stop
Step 5: Stop
Step 1: Start
Step 2: Set ch=y
Step 3: Allocate memory for new node, assign value to number
Part and assign null value to the pointer parts of node.
Step 4: Set root=temp, current node=temp, and push the root
Step 5: Repeat while ch! =n
1: If left pointer part of new node is null, then
2: Read choice to ch ,if ch=!=n ,then go to step 3
3: Allocate memory for new node, assign value to
number Part ,and assign null value to the pointer
parts of node.
4: Set current node=temp, and push the root
Else,currentnode=pop()
Step 6: if we want right child then
Step 1: Start
Step 1: Start
Step 2: if current node! = null, then, Display current node data
Step 3: Call preorder (left pointer part of current node)
Step 4: Call preorder (right pointer part of current node)
Step 5: Stop
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
#define MAX 10
struct node
{
int data;
struct node *lchild,*rchild;
}*temp,*root,*cn;
struct node *stack[MAX];
int top=-1;
struct node *pop();
void push(struct node*cn);
void inorder(struct node*cn);
void postorder(struct node*cn);
void preorder(struct node*cn);
void create();
void main()
{
clrscr();
create();
puts(" Preorder traversal");
preorder(root);
puts("\n Inorder traversal");
inorder(root);
puts("\n Postorder traversal");
postorder(root);
getch();
}
void create()
{
int num;
char ch='y';
printf("Enter the data of root");
scanf("%d",&num);
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->lchild=temp->rchild=NULL;
root=temp;
push(root);
cn=temp;
do
{
while(ch!='n')
{
if(cn->lchild==NULL)
}
}
else
cn=pop();
}
printf("Do you want right clild for %d",cn->data);
scanf(" %c",&ch);
if(ch!='n')
{
printf("Enter the right child");
scanf(" %d",&num);
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->lchild=temp->rchild=NULL;
cn->rchild=temp;
cn=temp;
push(cn);
}
cn=pop();
}while(top!=-1);
}
void push(struct node* cn)
{
if(top==MAX)
{
getch();
exit(0);
}
else
{
top++;
stack[top]=cn;
}
}
struct node*pop()
{
struct node*cn;
cn=stack[top];
}
void postorder(struct node*cn)
{
if(cn!=NULL)
{
postorder(cn->lchild);
postorder(cn->rchild);
printf("\t%d",cn->data);
}
}
Preorder traversal
50 20 30 10 100 20 30
Inorder traversal
30 20 10 50 30 20 100
Postorder traversal
30 10 20 30 20 100 50
printf("%c",c);
fclose(fp);
getch();
}