Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- //Реализация Stack на основе односвязного списка
- //Задача: попрано вывести индексы открывающих скобок и их закрывающих скокбок в строке
- struct el
- {
- int num;
- el* next;
- };
- el* b;
- el* newstack()
- {
- b = new el;
- b->next = NULL;
- return b;
- }
- void push_s(el*& a, int x)
- {
- a->num = x;
- b = a;
- a = new el;
- a->next = b;
- }
- void pop(el*& a)
- {
- b = a->next;
- a->next = b->next;
- delete b;
- }
- el* newqueue()
- {
- b = new el;
- b->next = b;
- return b;
- }
- void push_q(el*& a, int x)
- {
- a->num = x;
- b = a;
- a = new el;
- a->next = b->next;
- b->next = a;
- }
- int top(el*& a)
- {
- return a->next->num;
- }
- bool isempty(el* a)
- {
- return (a->next == NULL || a->next == a);
- }
- void out(el*& open, el*& close1, el*& close2)
- {
- while (!isempty(close1))
- {
- cout << top(open) << ' ' << top(close1) << "; ";
- pop(open); pop(close1);
- }
- while (!isempty(close2))
- {
- cout << top(open) << ' ' << top(close2) << "; ";
- pop(open); pop(close2);
- }
- }
- int main()
- {
- el* open, * close2, * close1;
- open = newqueue();
- close2 = newqueue();
- close1 = newstack();
- char c;
- int i = 0, k = 0;
- bool f = 0;
- while (cin >> c)
- {
- i++;
- if (c == '(')
- {
- push_q(open, i);
- f = 1;
- k++;
- }
- else
- {
- if (c == ')')
- {
- k--;
- if (f)
- {
- push_q(close2, i);
- f = 0;
- }
- else
- push_s(close1, i);
- }
- }
- if (!k)
- out(open, close1, close2);
- }
- out(open, close1, close2);
- return 0;
- }
- /*
- 5+2*(13+(14-2))=0?
- */
Advertisement
Add Comment
Please, Sign In to add comment