structNode{intdata;Node*link;};structNode*top=NULL;//it means the same as Node *head=NULL;voidPush(intx){Node*temp=newNode;temp->data=x;temp->link=top;top=temp;}voidPop(){Node*temp;if(top==NULL){printf("Stack is empty\n");return;}temp=top;top=top->link;//making top point to the second nodedeletetemp;//in C++ use new/delete instead of malloc/free in C}voidTop(){if(top==NULL){printf("Stack is empty\n");return;}printf("%d\n",top->data);}voidIsEmpty(){if(top==NULL){printf("Stack is empty\n");return;}else{printf("Stack is not empty\n");}}voidPrint(){Node*temp=top;printf("Stack is : ");while(temp!=NULL){//traversal of the linked listprintf("%d ",temp->data);temp=temp->link;}printf("\n");}intmain(){Push(5);Print();IsEmpty();Pop();Print();IsEmpty();Push(6);Print();IsEmpty();Push(7);Print();IsEmpty();Push(8);Print();IsEmpty();return0;}
#include<stack> //stack from standard template library#include<iostream>usingnamespacestd;voidReverse(charC[],intn){stack<char>S;//create a stack//loop for pushfor(inti=0;i<n;i++){S.push(C[i]);}//loop for popfor(inti=0;i<n;i++){C[i]=S.top();//overwrite the character at index iS.pop();//perform pop}}intmain(){charC[51];printf("Eneter a string: ");gets(C);Reverse(C,strlen(C));printf("Output is %s",C);}
voidReverse(){if(head==NULL)return;stack<Node*>S;while(temp!=NULL){//To push all references (traversal)S.push(temp);temp=temp->next;}Node*temp=S.top();head=temp;S.pop;while(!S.empty()){temp->next=S.top();S.pop();temp=temp->next;}temp->next=NULL;}
#include<string.h>#include<stdbool.h>boolCheckforParentheses(char*expression){intn=strlen(expression);charS[100];inttop=-1;// Stack to store the parenthesesfor(inti=0;i<n;i++){if(expression[i]=='('||expression[i]=='['||expression[i]=='{'){S[++top]=expression[i];}elseif(expression[i]==')'||expression[i]==']'||expression[i]=='}'){if(top==-1){returnfalse;}if((expression[i]==')'&&S[top]=='(')||(expression[i]==']'&&S[top]=='[')||(expression[i]=='}'&&S[top]=='{')){top--;}else{returnfalse;}}}returntop==-1;//return true if all parentheses are matched}intmain(){charexpression[100];printf("Enter an expression: ");fgets(expression,sizeof(expression),stdin);expression[strcspn(expression,"\n")]='\0';//Remove \n from stringCheckforParentheses(expression);if(CheckforParentheses(expression)){printf(" The expression is valid\n");}else{printf("The expression is invalid\n");}return0;}