Нужна помощь в поиске ошибки проверки времени выполнения № 2 - стек вокруг переменной 'list' поврежден - PullRequest
0 голосов
/ 28 марта 2012

Итак, я получаю ошибку проверки времени выполнения № 2 - стек вокруг переменной 'list' поврежден.Я копался в коде и не могу понять, где происходит эта ошибка.Может ли кто-нибудь просмотреть это и сказать мне, где я могу пойти не так.Прежде всего, это присвоение класса для структуры данных и класса алгоритма.Учитель принял мой код, но сказал, что мне нужно исправить эту ошибку.Я искал несколько дней и не могу найти, где это.Единственным ограничением этого проекта является то, что основное не может быть изменено.Спасибо за вашу помощь.

Джейсон

list.h

typedef char Titem;

// Interface of list
typedef struct node *Tpointer;
typedef struct node {
    Titem item;
    Tpointer next;
    Tpointer first;

} Tnode;
typedef Tpointer Tlist;

void initialize_list (Tlist *list);
void insert_to_list_end(Tlist *list, Titem data);
void print_list (Tlist *list);
void cleanup_list(Tlist *list);

list.c

#include <stdlib.h>
#include <stdio.h>
#include "list.h"

// Implementation of list (only obj is need in appl)
void initialize_list (Tlist list)
{
    list->first = NULL;
    list->next = NULL;
}

void cleanup_list(Tlist list)
{
    Tpointer aux1, aux2;

    aux1 = list->first;
    while (aux1 != NULL) 
   {
        aux2 = aux1->next;
        free(aux1);
        printf("\nDeleted"); //for testing purposes
        aux1 = aux2;
    }
    initialize_list(&list);
}

void insert_to_list_end(Tlist list, Titem data) 
{
Tpointer newnode;

newnode = (Tpointer) malloc(sizeof(Tnode));
newnode -> item = data;
if (list->first == NULL)
    list->first = newnode;       //first node
    else
    list->next->next = newnode;  //not first node
list->next = newnode;
list->next->next = NULL;
}

void print_list (Tlist list) 
{
    Tpointer what;

    printf("\nList 4 :");
    what = list->first;
    while (what != NULL)
    {
        printf("%c ", what->item);
        printf("\nNext is %d ", what->next);
        what = what->next;
    }
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "list.h"

// Application
int main (void)  {
    Tlist list;

    initialize_list(&list);
    insert_to_list_end(&list, 'a');
    insert_to_list_end(&list, 'b');
    insert_to_list_end(&list, 'c');
    insert_to_list_end(&list, 'd');

    print_list(&list);
    cleanup_list(&list);

    fflush(stdin); getchar();
    return 0;
}

1 Ответ

0 голосов
/ 21 марта 2014

Нормальный компилятор даже не компилирует ваш список.c -

list.c:6: error: conflicting types for 'initialize_list'
list.h:13: note: previous declaration of 'initialize_list' was here
list.c:12: error: conflicting types for 'cleanup_list'
list.h:16: note: previous declaration of 'cleanup_list' was here
list.c: In function 'cleanup_list':
list.c:24: warning: passing argument 1 of 'initialize_list' from incompatible pointer type
list.c:6: note: expected 'Tlist' but argument is of type 'struct node **'
list.c: At top level:
list.c:27: error: conflicting types for 'insert_to_list_end'
list.h:14: note: previous declaration of 'insert_to_list_end' was here
list.c:41: error: conflicting types for 'print_list'
list.h:15: note: previous declaration of 'print_list' was here

- вот исправленная версия:

#include <stdlib.h>
#include <stdio.h>
#include "list.h"

void initialize_list(Tlist *list)
{
    *list = NULL;
}

void cleanup_list(Tlist *list)
{
    Tpointer aux1, aux2;
    if (!*list) return;
    aux1 = (*list)->first;
    while (aux1 != NULL) 
    {
        aux2 = aux1->next;
        free(aux1);
        printf("\nDeleted");        //for testing purposes
        aux1 = aux2;
    }
    initialize_list(list);
}

void insert_to_list_end(Tlist *list, Titem data) 
{
    Tpointer newnode;
    newnode = malloc(sizeof(Tnode));
    newnode->item = data;
    if (*list == NULL)                      //first node
        newnode->first = newnode;
    else                                //not first node
        ((*list)->next = newnode)->first = (*list)->first;
    newnode->next = NULL;
    *list = newnode;
}

void print_list(Tlist *list) 
{
    Tpointer what;
    printf("\nList 4 :");
    if (!*list) return;
    what = (*list)->first;
    while (what != NULL)
    {
        printf("%c ", what->item);
        printf("\nNext is %d ", what->next);
        what = what->next;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...