Posts

Program that implements queue as an array.

#include #include #define MAX 10 void addq ( int *, int, int *, int * ) ; int delq ( int *, int *, int * ) ; void main( ) { int arr[MAX] ; int front = -1, rear = -1, i ; clrscr( ) ; addq ( arr, 23, &front, &rear ) ; addq ( arr, 9, &front, &rear ) ; addq ( arr, 11, &front, &rear ) ; addq ( arr, -10, &front, &rear ) ; addq ( arr, 25, &front, &rear ) ; addq ( arr, 16, &front, &rear ) ; addq ( arr, 17, &front, &rear ) ; addq ( arr, 22, &front, &rear ) ; addq ( arr, 19, &front, &rear ) ; addq ( arr, 30, &front, &rear ) ; addq ( arr, 32, &front, &rear ) ; i = delq ( arr, &front, &rear ) ; printf ( "\nItem deleted: %d", i ) ; i = delq ( arr, &front, &rear ) ; printf ( "\nItem deleted: %d", i ) ; i = delq ( arr, &front, &rear ) ; printf ( "\nItem deleted: %d", i ) ; getch( ) ; } /* adds an element to the queue */ void addq ( int *arr...

Program to evaluate an epression entered in postfix form

#include #include #include #include #include #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( ) ; } /* initializes data members */ void initpostfix ( struct postfix *p ) { p -> top = -1 ; } /* sets s to point to the given expr. */ void setexpr ( struct postfix *p, char *str ) { p -> s = str ; } /* adds digit to the stack */ void push ( struct postfix *p, int item ) { if ( p -> top == MAX - 1 ) printf ( "\nStack is full." ) ; else { p -> top++ ; p -> sta...

Program to convert an expression in postfix form to an infix form

#include #include #include #define MAX 50 struct postfix { char stack[MAX][MAX], target[MAX] ; char temp1[2], temp2[2] ; char str1[MAX], str2[MAX], str3[MAX] ; int i, top ; } ; void initpostfix ( struct postfix * ) ; void setexpr ( struct postfix *, char * ) ; void push ( struct postfix *, char * ) ; void pop ( struct postfix *, char * ) ; void convert ( struct postfix * ) ; void show ( struct postfix ) ; void main( ) { struct postfix q ; char expr[MAX] ; clrscr( ) ; initpostfix ( &q ) ; printf ( "\nEnter an expression in postfix form: " ) ; gets ( expr ) ; setexpr ( &q, expr ) ; convert ( &q ) ; printf ( "\nThe infix expression is: " ) ; show ( q ) ; getch( ) ; } /* initializes data member */ void initpostfix ( struct postfix *p ) { p -> i = 0 ; p -> top = -1 ; strcpy ( p -> target, "" ) ; } /* copies given expression to target string */ void setexpr ( struct postfix *p, char *c ) { strcpy ( p -> target, c ) ; } ...

Program to convert expression in postfix form to prefix form

#include #include #include #define MAX 50 struct postfix { char stack[MAX][MAX], target[MAX] ; char temp1[2], temp2[2] ; char str1[MAX], str2[MAX], str3[MAX] ; int i, top ; } ; void initpostfix ( struct postfix * ) ; void setexpr ( struct postfix *, char * ) ; void push ( struct postfix *, char * ) ; void pop ( struct postfix *, char * ) ; void convert ( struct postfix * ) ; void show ( struct postfix ) ; void main( ) { struct postfix q ; char expr[MAX] ; clrscr( ) ; initpostfix ( &q ) ; printf ( "\nEnter an expression in postfix form: " ) ; gets ( expr ) ; setexpr ( &q, expr ) ; convert ( &q ) ; printf ( "\nThe Prefix expression is: " ) ; show ( q ) ; getch( ) ; } /* initializes the elements of the structure */ void initpostfix ( struct postfix *p ) { p -> i = 0 ; p -> top = -1 ; strcpy ( p -> target, "" ) ; } /* copies given expr. to target string */ void setexpr ( struct postfix *p, char *c ) { strcpy ( p -> ta...

Program to convert an Infix form to Postfix form

#include #include #include #include #define MAX 50 struct infix { char target[MAX] ; char stack[MAX] ; char *s, *t ; int top ; } ; void initinfix ( struct infix * ) ; void setexpr ( struct infix *, char * ) ; void push ( struct infix *, char ) ; char pop ( struct infix * ) ; void convert ( struct infix * ) ; int priority ( char ) ; void show ( struct infix ) ; void main( ) { struct infix p ; char expr[MAX] ; initinfix ( &p ) ; clrscr( ) ; printf ( "\nEnter an expression in infix form: " ) ; gets ( expr ) ; setexpr ( &p, expr ) ; convert ( &p ) ; printf ( "\nThe postfix expression is: " ) ; show ( p ) ; getch( ) ; } /* initializes structure elements */ void initinfix ( struct infix *p ) { p -> top = -1 ; strcpy ( p -> target, "" ) ; strcpy ( p -> stack, "" ) ; p -> t = p -> target ; p -> s = "" ; } /* sets s to point to given expr. */ void setexpr ( struct infix *p, char *...

Program to convert an Infix expression to Prefix form.

#include #include #include #include #define MAX 50 struct infix { char target[MAX] ; char stack[MAX] ; char *s, *t ; int top, l ; } ; void initinfix ( struct infix * ) ; void setexpr ( struct infix *, char * ) ; void push ( struct infix *, char ) ; char pop ( struct infix * ) ; void convert ( struct infix * ) ; int priority ( char c ) ; void show ( struct infix ) ; void main( ) { struct infix q ; char expr[MAX] ; clrscr( ) ; initinfix ( &q ) ; printf ( "\nEnter an expression in infix form: " ) ; gets ( expr ) ; setexpr ( &q, expr ) ; convert ( &q ) ; printf ( "The Prefix expression is: " ) ; show ( q ) ; getch( ) ; } /* initializes elements of structure variable */ void initinfix ( struct infix *pq ) { pq -> top = -1 ; strcpy ( pq -> target, "" ) ; strcpy ( pq -> stack, "" ) ; pq -> l = 0 ; } /* reverses the given expression */ void setexpr ( struct infix *pq, char *str ) { pq -> s = str ; str...

Program implements linked list as a stack.

#include #include #include /* structure containing data part and linkpart */ 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( ) ; } /* adds a new node to the stack as linked list */ 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 ; } /* pops an eleme...