You are on page 1of 15

WRITE A C PROGRAM TO IMPLEMENT MORE THAN ONE STACK IN SINGLE ARRAY #include <STDIO.

H> #define MAX 10 int stack[MAX],topA=-1,topB=MAX; void pushA(int no) { if(topA==topB) { printf("\n OVERFLOW"); return; } stack[++(topA)]=no; } int popA() { if(topA==-1) { printf("\n UNDERFLOW"); return -999; } return stack[(topA)--]; } void showA() { int i; if(topA==-1) { printf("\n stack Empty"); return; } for(i=topA;i>=0;i--) { printf("\n %d",stack[i]); } } void pushB(int no) { if(topB-1==topA) { printf("\n OVERFLOW"); return; } stack[--(topB)]=no;

} int popB() { if(topB==MAX) { printf("\n UNDERFLOW"); return -999; } return stack[(topB)--]; } void showB() { int i; if(topB==MAX) { printf("\n stack Empty"); return; } for(i=topB;i>=0;i--) { printf("\n %d",stack[i]); } } #include<stdio.h> #include<conio.h> #include<string.h> #define MAX 20 char stack[MAX]; int top = -1; char pop(); void push(char item); int prcd(char symbol) { switch(symbol) { case '+': case '-': return 2; case '*': case '/': return 4; case '^': case '$': return 6;

case '(': case ')': case '#': return 1; } } int isoperator(char symbol) { switch(symbol) { case '+': case '-': case '*': case '/': case '^': case '$': case '(': case ')': return 1; default: return 0; } } void convertip(char infix[],char prefix[]) { int i,symbol,j=0; char test[MAX]; infix=strrev(infix); stack[++top]='#'; for(i=0;i<strlen(infix);i++) { symbol=infix[i]; if(isoperator(symbol)==0) { prefix[j]=symbol; j++; }else { if(symbol==')') { push(symbol); }else if(symbol=='(') { while(stack[top]!=')') { prefix[j]=pop(); j++;

} pop();//pop out (. }else { if(prcd(symbol)>prcd(stack[top])) { push(symbol); }else { while(prcd(symbol)<=prcd(stack[top])) { prefix[j]=pop(); j++; } push(symbol); }//end of else. }//end of else. }//end of else. }//end of for. while(stack[top]!='#') { prefix[j]=pop(); j++; } prefix[j]='\0';//null terminate string. prefix=strrev(prefix); } int main() { char infix[20],prefix[20]; clrscr(); printf("Enter the valid infix string:\n"); gets(infix); convertip(infix,prefix); printf("The corresponding prefix string is:\n"); puts(prefix); getch(); return 0; } void push(char item) { top++; stack[top]=item;

} char pop() { char a; a=stack[top]; top--; return a; } C Program to evaluate Prefix Expression #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> #define MAX 30 #define OPERAND 10 #define OPERATOR 20 typedef struct prexp { int top; int stack[MAX]; }stck; void init(stck*); void push(stck*,int); int pop(stck*); void eval(stck*,char,int,int); int gettype(char); void main() { char pre[MAX]; int num1,num2,item,l,i,pr; stck stk; fflush(stdin); clrscr(); init(&stk); printf(" ENTER THE PREFIX EXPRESSION "); gets(pre);

l=strlen(pre); for(i=l-1;i>=0;i--) { if(pre[i]==' ' || pre[i]=='\0') continue; switch(gettype(pre[i])) { case OPERAND : item=pre[i]-'0'; push(&stk,item); break; case OPERATOR : num1=pop(&stk); num2=pop(&stk); eval(&stk,pre[i],num1,num2); } } printf("%d",stk.stack[0]); getch(); } void init(stck *st ) { st->top=-1; } void push(stck *st,int num) { st->top++; st->stack[st->top]=num; } int pop(stck *st) { int num; num=st->stack[st->top]; st->top--; return num; } void eval(stck *st,char op,int num1,int num2) { int res; switch(op) { case '+': res=num1+num2; break;

case '-': res=num1-num2; break; case '*': res=num1*num2; break; case '/': res=num1/num2; break; case '%': res=num1%num2; break; case '$': res=pow(num1,num2); break; } push(st,res); } int gettype(char c) { switch(c) { case '+': case '-': case '*': case '/': case '$': case '%': return OPERATOR; default : return OPERAND; } }

program to convert infix to prefix expression


//program to convert infix to prefix expression #include<iostream.h> #include<conio.h> #include<string.h> char *infix,*prefix,*stack,*temp,c; int length,counti,countp,top=-1,tmp; void pre(void ) {

int i; for(i=0;i<length;i++) if((infix[i]=='(')||(infix[i]==')')||(infix[i]==' ')) tmp++; tmp=length-tmp; countp=tmp; cout<<"\nreverse exp is : "; for(i=0;i<length;i++) cout<<infix[i]; stack=new char[length-countp]; prefix=new char[countp]; for(counti=0;counti<length;counti++) { c=infix[counti]; if(((c>47)&&(c<58))||((c>64)&&(c<91))||((c>96)&&(c<123))) prefix[--countp]=c; if(c==')') stack[++top]=c; if(c=='^') stack[++top]=c; if((c=='+')||(c=='-')) { while((stack[top]=='^')||(stack[top]=='*')||(stack[top]=='/')) prefix[--countp]=stack[top--]; stack[++top]=c; } if((c=='*')||(c=='/')) { while(stack[top]=='^') prefix[--countp]=stack[top--]; stack[++top]=c; } if(c=='(') { while(stack[top]!=')') { prefix[--countp]=stack[top--]; }

top--; } } while(top>-1) prefix[--countp]=stack[top--]; cout<<"\n\nPrefix exp is : \n"; for(i=0;i<tmp;i++) cout<<prefix[i]; } void main() { clrscr(); int i=0; cout<<"\nEnter the infix expression : \n"; cin>>temp; length=strlen(temp); //cout<<"\nlength of infix : "<<length; infix=new char[length]; for(i=0;i<length;i++) infix[i]=temp[length-1-i]; cout<<endl; delete temp; pre(); getch(); } #include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer to left child and a pointer to right child */ struct node { int data; struct node* left; struct node* right; }; /* Function to get the count of leaf nodes in a binary tree*/

unsigned int getLeafCount(struct node* node) { if(node == NULL) return 0; if(node->left == NULL && node->right==NULL) return 1; else return getLeafCount(node->left)+ getLeafCount(node->right); } /* Helper function that allocates a new node with the given data and NULL left and right pointers. */ struct node* newNode(int data) { struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } /*Driver program to test above functions*/ int main() { /*create a tree*/ struct node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); /*get leaf count of the above created tree*/ printf("Leaf count of the tree is %d", getLeafCount(root)); getchar(); return 0; }

Threaded Binary Tree C program

#include<stdio.h> #include<conio.h> struct tree { int data; struct tree *left; struct tree *right; }; int i=0,a[20],c=0; struct tree *s,*t,*header; struct tree *create(); void inorder(struct tree *); void inorder1(struct tree *); void header_node(struct tree *p); void display(struct tree *p); struct tree *create() { struct tree *p,*root; int m,x; char s; root=(struct tree *)malloc(sizeof(struct tree)); printf("\nenter the value of the main root"); scanf("%d",&m); root->data=m; root->left=NULL; root->right=NULL; printf("\nenter n to discontinue creation of the binary search tree"); fflush(stdin); scanf("%c",&s); while(s!='n') { p=root; printf("\nenter the value of the newnode"); fflush(stdin); scanf("%d",&x); while(1) { if(x<p->data) { if(p->left==NULL) { p->left=(struct tree *)malloc(sizeof(struct tree)); p=p->left; p->data=x; p->right=NULL;

p->left=NULL; break; } else p=p->left; } else { if(p->right==NULL) { p->right=(struct tree *)malloc(sizeof(struct tree)); p=p->right; p->data=x; p->right=NULL; p->left=NULL; break; } else p=p->right; } } printf("\nwant to continue"); fflush(stdin); scanf("%c",&s); } return(root); } void inorder(struct tree *p) { if(p!=NULL) { inorder(p->left); a[i]=p->data; i++; inorder(p->right); } } void inorder1(struct tree *p) { if(p!=NULL) { inorder1(p->left); if(a[c]==p->data) s=p; else if(a[c+1]==p->data) {

t=p; if(s->right==NULL) s->right=t; else if(t->left==NULL) t->left=s; s=t; c++; } inorder1(p->right); } } void header_node(struct tree *p) { struct tree *top; int m; top=p; header=(struct tree *)malloc(sizeof(struct tree)); printf("\nenter the value of the header node"); scanf("%d",&m); header->data=m; while(p->right!=NULL) p=p->right; p->right=header; p=top; while(p->left!=NULL) p=p->left; p->left=header; header->left=top; header->right=top; } void display(struct tree *p) { char a; while(p->right!=header) p=p->right; printf("\npress n to quit display"); scanf("%c",&a); while(a!='n') { while(p->left!=header) { printf("\t%d",p->data); p=p->left; } printf("\t%d\t",header->data); while(p->right!=header)

{ printf("\t%d",p->data); p=p->right; } printf("\npress n to discontinue"); scanf("%c",&a); } } void main() { int h; struct tree *root; clrscr(); while(1) { printf("\nenter 1. for creation of the binary search tree"); printf("\nenter 2. for creation of a binary threaded tree"); printf("\nenter 3. for display"); printf("\nenter 4. for exit"); printf("\nenter your choice"); scanf("%d",&h); switch(h) { case 1: root=create(); break; case 2: inorder(root); inorder1(root); header_node(root); break; case 3: display(root); break; case 4: exit(0); default: printf("\nentered a wrong choice"); } } }

Algorithm to construct the binary tree with given preorder and inorder sequences

struct node { int data; node* lptr; node* rptr; node(int val) : lptr(0), rptr(0), data(val) { } }; node* buildtree(int* inorder,int* preorder,int left,int right,int& current) { 1. Test if left is equal to right and return NULL if it is. 2. Create a new temporary node with value of preorder[current]. 3. Return this new temporary node if left+1 is equal to right. 4. Search starting from inorder+left up to inorder+right and get an int pointer to the element that has the same value as preorder[current]. 5. Find the distance from the start of the inorder array to this pointer, call it position. 6. If position is greater than left then... 6a. Increment current 6b. Set the lptr of the temporary node equal to the return value of buildtree(inorder,preorder,left,position,current) 7. If position+1 is less than right then... 7a. Increment current 7b. Set the rptr of the temporary node equal to the return value of buildtree(inorder,preorder,position+1,right,current) 8. Return the temporary node } int main() { int preorder[] = { 6, 15, 1, 23, 9, -9, 22 }; int inorder[] = { 23, 1, 15, 9, 6, 22, -19 }; int count = 0; node* root = buildtree(inorder,preorder,0,7,count); // root now points to the root of the binary-search tree // built from the two arrays. }

You might also like