Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. //implementation of stack
  5. //we have declared array as a global variable with limited size.....u can easily modofy it for unknown number of members
  6. char ar[100000];
  7. struct node{
  8. struct node* link;
  9. char data;
  10. };
  11. struct node* HEAD=NULL;
  12. //push function
  13. void push(char add){
  14. struct node* temp=(struct node*)malloc(sizeof(struct node));
  15. temp->data = add;
  16. temp->link=HEAD;
  17. HEAD= temp;
  18. }
  19. void pop(){
  20. struct node* i=HEAD;
  21. i=HEAD->link;
  22. free(HEAD);
  23. HEAD=i;
  24.  
  25. }
  26. //function to check whether the stack is empty
  27. int empty(){
  28. if(HEAD==NULL){
  29. return 1;
  30. }
  31. else return 0;
  32. }
  33. //function returning the top of stack
  34. char top(){
  35. return HEAD->data ;
  36. }
  37. //function to check that opening and closing brackets are matching
  38. int matching(char a , char b){
  39. if((a=='(')&&(b==')')) return 1;
  40. else if((a=='[')&&(b==']')) return 1;
  41. else if((a=='{')&&(b=='}')) return 1;
  42. else return 0;
  43. }
  44. //function to finally check that string is balanced or not
  45. int balancing(){
  46. int n= strlen(ar);
  47. int i;
  48. char comp;
  49. for(i=0;i<n;i++){
  50. if((ar[i]=='(')||(ar[i]=='[')||(ar[i]=='{')){
  51. push(ar[i]);
  52. }
  53. else if((ar[i]==')')||(ar[i]==']')||(ar[i]=='}')){
  54. comp=top();
  55. if(((matching(comp,ar[i]))==0)||(empty()==1)){
  56. return 0;
  57. }
  58. else{
  59. pop();
  60. }
  61. }
  62. }
  63. if(empty()==1) return 1;
  64. else 0;
  65. }
  66. int main(){
  67.  
  68. printf("enter the string of brackets\n");
  69. gets(ar);
  70. printf("%s\n",ar);
  71. int k;
  72. k=balancing();
  73. if(k==1) printf("balnced");
  74. else printf("fuck u!!u hav entered unbalanced string!!!!");
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement