Сравнение указателей на элементы списка
нужно сравнить в одно ли место указывают два указателя функция int delete_max(List *l) нужно взять один список, и не копируя те элементы, которые заключены между ptr_5 и now_p1, записать их в новый список l_af_del.
в основной части в циклах while происходит ошибка, он не может почему то сравнить два указателя
как это решить? если пробовать
(Item*)(p1) != (Item*) (n->ptr_5)
то не работает вре равно
p1 раньше определяется как Item*
#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 88;}
}
else if( (p1->symb) == ']')
{
if(s->head && (s->head->symb) == '[')
{
stack_delete_one(s);
p1= p1 -> next;
}
else {
printf("wronngg2"); return 88;}
}
}
if((s->head) != NULL )
{
printf("\nERROOOORR\n");
return 88;
}
queue_delete1(s);
free(p1);
return 55;
}
typedef struct sk{
int num_open;
Item * ptr_5;
Item * now_p1;
}sk;
//вот здесь начинаются проблемы я думаю
int func_count( Item * p1 ){
sk *str_sk = (sk*) malloc(sizeof(sk));
int count_all = 0;
str_sk->ptr_5 = p1;
str_sk->num_open = 0;
while(p1){
if( ((p1->symb) == '(') || ((p1->symb) == '[') ){
(str_sk->num_open) ++;
count_all++;
p1 = p1->next;
} else if ( ((p1->symb) == ')') || ((p1->symb) == ']') ){
count_all --;
p1 = p1->next;
}
if( count_all == 0 ){
str_sk->now_p1 = p1;
return str_sk;
}
}
}
int delete_max(List *l)
{
int flag = 1;
List *l_af_del = list_new();
Item *p1_af_del = (Item*) malloc(sizeof(Item));
p1_af_del = l_af_del->head;
Item *p1 = (Item*) malloc(sizeof(Item));
p1 = l->head;
sk* m = (sk*) malloc(sizeof(sk));
sk* n = (sk*) malloc(sizeof(sk));
move(p1);
m->num_open = 0;
n->num_open = 0;
while(p1)
{ if ( p1 && ( ((p1->symb) != ']') && ((p1->symb) != ')')&& ((p1->symb) != '(') && ((p1->symb) != '[') )){
p1 = p1->next;
}
else if( ((p1->symb) == '[') || ((p1->symb) == ']') ){
if((p1)){
p1 = p1->next;}
}
else if ((p1->symb) == '('){
if( (flag == 1) && (m->num_open)==0 ){
m = (sk*)func_count(p1);
p1 = m->now_p1;
printf("\n m->num_open:%d\n", (m->num_open));
flag = 0;
} else if ( flag == 0 && (n->num_open)== 0 ){
n = (sk*)func_count(p1);
p1 = n->now_p1;
printf("\n n->num_open:%d\n", (n->num_open));
flag = 1;
}
if( (n->num_open!=0)&&(m->num_open!=0) ){
if( (m->num_open) >= (n->num_open) ){
n->num_open = 0;
flag = 0;
} else if( (m->num_open) < (n->num_open) ){
m->num_open = 0;
flag = 1;
}
}
}
}
if (m->num_open == 0){//в n макс вложенность
p1 = l->head;
//вот в этот цикл не заходит
while( (p1) != (n->ptr_5) ){
p1_af_del->symb = p1->symb;
p1 = p1->next;
p1_af_del = p1_af_del->next;
}
p1 = n->now_p1;
while(p1){
p1_af_del->symb = p1->symb;
p1 = p1->next;
p1_af_del = p1_af_del->next;
}
}
if (n->num_open == 0){
// //в m макс вложенность
p1 = l->head;
//то же самое
while(p1 != m->ptr_5 ){
(p1_af_del->symb) = (p1->symb);
p1 = p1->next;
p1_af_del = p1_af_del->next;
}
p1 = m->now_p1;
while(p1){
p1_af_del->symb = p1->symb;
p1 = p1->next;
p1_af_del = p1_af_del->next;
}
stack_print(l_af_del);
}
}
int main()
{
int flag_pr = 0;
int count_sk=0;
char curr_cymb;
List *l = list_new();
while ((curr_cymb = getchar()) != '0' )
{
{list_push_back(l, curr_cymb);}
}
stack_print(l);
delete_max(l);
/*
if( (prov(l)) == 55 ){
printf("\n*\n");
delete_max(l);
}*/
queue_delete(l);
return 27;