Advertisement
Niloy007

Balanced Paranthesis

Apr 10th, 2021
1,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NULL 0
  5.  
  6. struct ListNode {
  7.     int data;
  8.     struct ListNode *next;
  9. };
  10.  
  11. typedef struct ListNode LN;
  12.  
  13. class SinglyLinkedList {
  14.   public:
  15.     // constructor
  16.     SinglyLinkedList() {
  17.         head = NULL;
  18.     }
  19.  
  20.     LN *head;
  21.  
  22.     void print() {
  23.         LN *temp = head;
  24.         while (temp != NULL) {
  25.             printf("%d ", temp->data);
  26.             temp = temp->next;
  27.         }
  28.         printf("\n");
  29.     }
  30.  
  31.     void insert_first(int item) {
  32.         LN *new_node = (LN *) malloc(sizeof(LN));
  33.         new_node->data = item;
  34.         new_node->next = head;
  35.         head = new_node;
  36.     }
  37.  
  38.     void remove_first() {
  39.         if (head == NULL)
  40.             return;
  41.         LN *temp = head;
  42.         head = head->next;
  43.         free(temp);
  44.     }
  45. };
  46.  
  47. class Stack {
  48.     SinglyLinkedList L;
  49.  
  50.   public:
  51.     // constructor
  52.     Stack() {
  53.     }
  54.  
  55.     void push(int item) {
  56.         L.insert_first(item);
  57.     }
  58.  
  59.     void pop() {
  60.         L.remove_first();
  61.     }
  62.  
  63.     int peek() {
  64.         if (L.head == NULL) {
  65.             printf("Stack Underflow\n");
  66.             return -1;
  67.         }
  68.         return L.head->data;
  69.     }
  70.  
  71.     void print() {
  72.         L.print();
  73.     }
  74. };
  75.  
  76. int main() {
  77.  
  78.     char str[100];
  79.     gets(str);
  80.  
  81.     Stack s;
  82.  
  83.     int i = 0;
  84.     int flag = 1;
  85.     while (str[i] != '\0') {
  86.         // write your code here
  87.         if (str[i] == '(' || str[i] == '{' || str[i] == '[') {
  88.             s.push(str[i]);
  89.         } else {
  90.             if (s.peek() == -1) {
  91.                 flag = 0;
  92.                 break;
  93.             } else {
  94.                 if (s.peek() == '(' && str[i] != ')') {
  95.                     flag = 0;
  96.                     break;
  97.                 } else if (s.peek() == '{' && str[i] != '}') {
  98.                     flag = 0;
  99.                     break;
  100.                 } else if (s.peek() == '[' && str[i] != ']') {
  101.                     flag = 0;
  102.                     break;
  103.                 } else {
  104.                     s.pop();
  105.                 }
  106.             }
  107.         }
  108.         i++;
  109.     }
  110.     if (flag && s.peek() == -1) {
  111.         printf("Balanced\n");
  112.     } else {
  113.         printf("Not Balanced\n");
  114.     }
  115.  
  116.     return 0;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement