Write A Program in C To Simulate or To Implement A Stack Using A Pointer or Structure

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Write a program in to implement a stack using a pointer or structure #include <stdio.

h> struct st{ int item[10]; int top; }; typedef struct st stk; stk STACK; int n; main() { int choice = 0,l; printf("Enter the size of the stack\n"); scanf("%d",&n); while(choice!=4) { printf("1. PUSH \n"); printf("2. POP \n"); printf("3. Dispa \n"); printf("4. Exit \n"); printf("Enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: PUSH(); printf("The stack after insertion \n"); DISPLAY(); break; case 2: l=POP(); printf("The deleted element is %d\n", l); printf("The stack after the deletion\n"); DISPLAY(); break; case 3: printf("the content of the stack is \n"); DISPLAY(); break; case 4: exit(0); } } } PUSH() { int ele; printf("Enter the element\n"); scanf("%d",&ele); if(STACK.top == n) { printf("Stack is overflow\n"); return; } STACK.top++;

STACK.item[STACK.top]=ele; } POP() { int ele; if(STACK.top == 0) { printf("Stack is underflow\n"); return(0); } ele=STACK.item[STACK.top]; STACK.top--; return(ele); } DISPLAY() { int i; if(STACK.top == 0) { printf("Stack is empty \n"); return; } printf("TOP \n"); for(i=STACK.top;i>=1;i--) printf("%d \n",STACK.item[i]); printf("BOTTOM\n"); }

Write a program to convert infix expression to postfix expression. #include <stdio.h> #include <string.h> char infix[80],postfix[80]; main() { int l; clrscr(); printf("enter the infix expression terminated by #\n"); scanf("%s",infix); printf("infix expression is %s\n",infix); CONVERT(); printf("The postfix expression is %s\n",postfix); } CONVERT() { char ch,stk[80]; int top=0,i=0,j=0; int p1,p2; stk[top]='#'; while(infix[i] != '\0') { ch=infix[i]; if(isalnum(ch)) { postfix[j]=ch; j++; } else { if(ch == '#') { while(top!= -1) { postfix[j]=stk[top]; j++; top--; } } else { if(ch == ')') { while(stk[top]!='(') { postfix[j]=stk[top]; j++; top--; }

top--; } else { p1=INPR(ch); p2=STKPR(stk[top]); while(p1<=p2) { postfix[j]=stk[top]; j++; top--; p2=STKPR(stk[top]); } top++; stk[top]=ch; } } } i++; } postfix[j]='\0'; } INPR(c) char c; { switch(c) { case '*': case '/': return(2); case '+': case '-': return(1); case '(':return(3); } } STKPR(c) char c; { switch(c) { case '*': case '/': return(2); case '+': case '-': return(1); case '(': case '#': return(0); } }

Write a program in C to simulate or to implement a stack using a pointer or structure. #include <stdio.h> struct q{ int item[10]; int front; int rear; }; typedef struct q que; que QUEUE; int n; main() { int choice = 0,l; printf("Enter the size of the queue\n"); scanf("%d",&n); while(choice!=4) { printf("1. INSERT \n"); printf("2. DELETE \n"); printf("3. DISPLAY \n"); printf("4. Exit \n"); printf("Enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: INSERT(); printf("The queue after insertion \n"); DISPLAY(); break; case 2: l=DELETE(); printf("The deleted element is %d\n",l); printf("The queue after the deletion\n"); DISPLAY(); break; case 3: printf("the content of the queue is \n"); DISPLAY(); break; case 4: exit(0); } } } INSERT() { int ele; printf("Enter the element\n"); scanf("%d",&ele); if(QUEUE.rear == n) { printf("Queue is overflow\n"); return; } QUEUE.rear++;

QUEUE.item[QUEUE.rear]=ele; if(QUEUE.front == 0) QUEUE.front=1; } DELETE() { int ele; if(QUEUE.front == 0) { printf("Queue is underflow\n"); return(0); } ele=QUEUE.item[QUEUE.front]; if(QUEUE.front == QUEUE.rear) { QUEUE.front=0; QUEUE.rear=0; } else QUEUE.front++; return(ele); } DISPLAY() { int i; if(QUEUE.front == 0) { printf("Queue is empty \n"); return; } printf("FRONT -> "); for(i=QUEUE.front;i<=QUEUE.rear;i++) printf("%d-> ",QUEUE.item[i]); printf("REAR\n"); }

Write a program to search an element inm an array using Indexed Sequential Search. #include<stdio.h> #include<conio.h> int index_size ; int kindex[10] ; int *pindex[10] ; int p[10]; int indseq(int a[] , int n , int key) { int i , low , high ; for (i=0 ; i < index_size ; ++i) { if (key < kindex[i] ) break ; } /* find lower limit */ if (i==0) low = 0; else low = *pindex[i-1]; /* find upper limit */ if (i==index_size ) high = n-1 ; else high = *pindex[i] ; /* search for the key in master record */ for (i=low ; i<=high ; ++i) { if (a[i] == key ) return ( i+1 ) ; } return (-1); } void main() { int a[50] ,i , pos , k , n , item ; clrscr() ; printf("Enter number of elements in an Array : "); scanf("%d",&n); printf("Enter Array elements \n"); for (i=0; i<n ; ++i) scanf("%d",&a[i]); printf("Enter element to search : "); scanf("%d",&item) ;

printf("Array elements are \n"); for (i=0 ; i<n ; ++i) printf("%d\n",a[i]); for (i=0 ; i<n ; ++i) { p[i] = i ; } index_size = n / 5 ;

for (i=0 , k = 4 ; i < index_size ; ++i , k=k+5 ) { kindex[i] = a[k] ; } for (i=0 , k = 4 ; i < index_size ; i=i+1 , k=k+5 ) { pindex[i] = &p[k] ; *pindex[i] = p[k] ; } pos = indseq(a , n , item ); if (pos == -1) printf("Unsuccessful \n"); else printf("Found at positition : %d \n", pos); getch(); }

You might also like