Skip to main content

Posts

Stack

Stack:                 Stack is a linear data structure that follows a particular order in which the operations are performed. Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. Peek or Top: Returns the top element of the stack. isEmpty: Returns true if the stack is empty, else false. How to understand a stack practically?  There are many real-life examples of a stack. Consider the simple example of plates stacked over one another in a canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow the LIFO/FILO order. Time Complexities of operations on stack: push(), pop(), isEmpty() and peek() all take O
Recent posts

Evaluating Postfix Expression

  Evaluating Postfix Expression CODE 👇 #include <stdio.h> #include <ctype.h> #define MAX 20 typedef struct { int data[MAX]; int top; }STACK; void initstack(STACK *ps) { ps->top = -1; } void push(STACK *ps, int num) { ps->top++; ps->data[ps->top]=num; } int pop(STACK *ps) { return(ps->data[ps->top--]); } int isempty(STACK *ps) { return(ps->top == -1); } int isfull(STACK *ps) { return(ps->top == MAX-1); } void main() { void Eval(char post[]); char post[20]; printf(">>Evaluating Postfix Expression<<\n"); printf("Enter The Postfix Expression:\t"); scanf("%s", post); Eval(post); } void Eval(char post[]) { int value, i, op1, op2; STACK s1; initstack(&s1); for(i=0; post[i]!='\0'; i++) { if(isalpha(post[i])) { printf("Enter The Value Of %c: \t", post[i]); scanf("%d", &value); push(&s1, value); } else { op2=po

Infix To Postfix Conversion.

 Infix To Postfix Conversion. CODE:-1 👇 #include <stdio.h> #include <stdlib.h> #define MAX 20 typedef struct { int data[MAX]; int top; }STACK; void initstack(STACK *ps) { ps->top=-1; } void push(STACK *ps, int num) { ps->top++; ps->data[ps->top]=num; } int pop(STACK *ps) { return(ps->data[ps->top--]); } int isempty(STACK *ps) { return(ps->top==-1); } int isfull(STACK *ps) { return(ps->top=MAX-1); } void main() { void postfix(char in[], char post[]); char in[20], post[20]; printf("Enter The Fully Parenthesized Infix Expression:\t"); scanf("%s", in); postfix(in, post); printf("\n The Postfix String Is: %s",post); } void postfix(char in[], char post[]) { int i, j=0; char ch1; STACK s1; initstack(&s1); for(i=0; in[i]!='\0'; i++) { switch(in[i]) { case '+': case '-': case '*': case '/': case '%': case &

Palindrome String & Palindrome Number

 //Palindrome Number                          A number that is equal to the reverse of that same number is called a palindrome number. For example, 3553, 12321, etc. CODE 👇 #include <stdio.h> int main() { int n,r,sum=0, temp; printf("Enter the number:"); scanf("%d", &n); temp=n; while(n>0) { r=n%10; sum=(sum*10)+r; n=n/10; } if(temp==sum) printf(">>Palindrome Number<<\n"); else printf(">>Not Palindrome<<\n"); return 0; } 👉 Execute 👈 /* Output 1] Enter the number:5116532 >>Not Palindrome<< 2] Enter the number:1551 >>Palindrome Number<< */ //Palindrome String                Palindrome string if the reverse of that string is the same as the original string. For example, radar , level , etc.  Palindrome String(Main File) CODE 👇 #define false 0 #define true 1 #include <stdio.h> #include <stdlib.h> #include <string.h> #include "pal

Dynamic Implementation Of Stack

Dynamic Implementation Of Stack CODE 👇 #include <stdio.h> #include <stdlib.h> typedef struct node {       int info;       struct node *next; }NODE; NODE *top; void initstack() {      top=NULL; } int isempty() {       return(top==NULL); }  void push() {     int num;     NODE*  nn=(NODE*)malloc(sizeof(NODE));     nn->info=num;     nn->next=NULL;     nn->next=top;     top=nn; } int pop() {     int num;     NODE * temp = top;     num = top -> info;     top = top -> next;     free (temp);     return num; } void main() {     int choice, n;     initstack();     do     {         printf("\n1.PUSH \n2.POP \n3.EXIT\n");         printf("Enter Your Choice:");         scanf("%d",&choice);         switch(choice)         {             case 1:/*PUSH*/                 printf("\n ENTER THE TO BE PUSHED:");                 scanf("%d",&n);                 push(n);                 break;             case 2:/*POP*/        

Static Implementation Of Stack

Static Implementation Of Stack CODE 👇 #include <stdio.h> #include <stdlib.h> #define MAX 20 typedef struct  { int data[MAX]; int top; }STACK; void initstack(STACK *ps) { ps->top = -1; } void push(STACK *ps, int num) { ps->top++; ps->data[ps->top] = num; } int pop(STACK *ps) { return(ps->data[ps->top--]); } int isempty(STACK *ps) { return(ps->top == -1); } int isfull(STACK *ps) { return(ps->top==MAX-1); } void main() { int n, choice; STACK s1; initstack(&s1); do { printf("\n1.PUSH \n2.POP \n3.EXIT"); printf("\n Enter Your Choice:"); scanf("%d",&choice); switch(choice) { case 1: if(isfull (&s1)) printf("\n Stack Overflow"); else { printf("Enter the element to be pushed:"); scanf("%d", &n); push(&s1, n); } break; case 2: if(isempty(&s1)==1) printf(&quo