Удаление скобок максимальной вложенности
сделано для проверки на правильность ввода, помогите с удалением скобок макс вложенности, понятно что нужно считать заполнение стека и запоминать указатели, но не очень понятна реализация
ввод:(5+д)+[ () [] ]+([])
вывод: (5+д)+ [ ]+([])
полное задание:
Для каждой строки, представляющей собой запись некоторой математической формулы, проверить ее корректность с точки зрения соответствия друг другу открывающих и закрывающих круглых и квадратных скобок, а также, в случае корректности ввода, сформировать новую строку, из которой должна быть максимальной исключена подстрока, ограниченная круглыми скобками максимальной вложенности.
#include <stdio.h>
#include <stdlib.h>
typedef struct Item{
char symb;
struct Item *next;
}Item;
typedef struct List {
Item *head;
Item *tail;
} List;
typedef struct Stack {
Item *head;
} Stack;
Stack *stack_new() {
return (Stack *) calloc(1, sizeof(Stack));
}
List *list_new() {
return (List *)calloc(1, sizeof(List));
}
int stack_push(Stack *stack, char symb) {
Item *new = (Item *) malloc(sizeof(Item));
if (!new) {
return -1;
}
new->symb = symb;
new->next = stack->head;
stack->head = new;
return 0;
}
void stack_print(const List *stack) {
Item *ptr = stack->head;
while (ptr) {
printf("%c ", ptr->symb);
ptr = ptr->next;
}
printf("*\n");
}
int list_push_back(List *list, char symb) {
Item *ptr = (Item*) malloc(sizeof(Item));
if (!ptr){
return -1;
}
ptr->symb = symb;
ptr->next = NULL;
if (!list->head) {
list->head = ptr;
list->tail = ptr;
} else {
list->tail->next = ptr;
list->tail = ptr;
}
return 18;
}
void stack_delete_one(Stack *stack) {
Item *ptr = stack->head;
if(stack->head->next){
stack->head= stack->head->next;
free(ptr);}
else if ((stack->head->next)== NULL){
stack->head = NULL;
free(ptr);
}
}
void queue_delete(List *queue) {
Item *ptr = queue->head, *ptr_prev;
while (ptr) {
ptr_prev = ptr;
ptr = ptr->next;
free(ptr_prev);
}
free(queue);
}
void queue_delete1(Stack *queue) {
Item *ptr = queue->head, *ptr_prev;
while (ptr) {
ptr_prev = ptr;
ptr = ptr->next;
free(ptr_prev);
}
free(queue);
}
void move(Item *p1){
while( p1->symb && ( ((p1->symb) != ']') && ((p1->symb) != ')')&& ((p1->symb) != '(') && ((p1->symb) != '[') )){
p1 = p1->next;
}
}
int prov(List *l)
{
Stack *s = stack_new();
Item *p1 = (Item*) malloc(sizeof(Item));
p1 = l->head;
move(p1);
if( ((p1->symb) == ']') || ((p1->symb) == ')') ){
printf("rrrr");
return 88;
}
while(p1)
{ if ( p1 && ( ((p1->symb) != ']') && ((p1->symb) != ')')&& ((p1->symb) != '(') && ((p1->symb) != '[') )){
p1 = p1->next;
}
if( ((p1->symb) == '[') || ((p1->symb) == '(') )
{
stack_push(s, p1->symb);stack_print(s);
p1 = p1->next;
}
else if( (p1->symb) == ')')
{
printf("stack^1");
if(s->head && (s->head->symb) == '(')
{
stack_delete_one(s);
p1 = p1->next;
}else {
printf("wronng1"); return 281;}
}
else if( (p1->symb) == ']')
{
if(s->head && (s->head->symb) == '[')
{
stack_delete_one(s);
p1= p1 -> next;
}
else {
printf("wronngg2"); return 282;}
}
}
if((s->head) != NULL )
{
printf("\nERROOOORR\n");
return 24;
}
queue_delete(l);
queue_delete1(s);
free(p1);
return 55;
}
int main()
{ int count_sk=0;
char curr_cymb;
List *l = list_new();
while ((curr_cymb = getchar()) != '0' )
{
// if( (curr_cymb==')') || (curr_cymb=='(') || (curr_cymb==']') || ((curr_cymb=='[') ) )
{list_push_back(l, curr_cymb);}
/*if( (curr_cymb=='(') || ((curr_cymb=='[') ) ){
stack_push(s, curr_cymb);}*/
}
stack_print(l);
prov(l);
/*int m = prov(l);
if(m==55){
delete_long(l);
}*/
return 27;
}