문제

I made a program that implements a circular and doubly linked list.

/*
 * listfech2.c
 * Last Change:  2014-04-23
 * Maintainer:   Lucas de Sena <contact@seninha.net>
 * License:      This file is placed in public domain.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct elemento {
   char dado;
   struct elemento * ant, * prox;
   };

int main (int argc, char **argv) 
   {
   char zero='a';
   int i;
   struct elemento * lista;
   struct elemento *p1, *pinicio;

   p1 = NULL;
   lista = NULL;

   pinicio = malloc (sizeof (struct elemento));
   pinicio -> dado = zero;
   pinicio -> ant = NULL;
   pinicio -> prox = NULL;

   p1 = pinicio;

   for (i=1; i<10; i++) {
      zero++;
      lista = malloc (sizeof (struct elemento));
      lista -> dado = zero;
      lista -> ant = p1;
      lista -> prox = pinicio;
      p1 -> prox = lista;
      printf ("dado atual: %c\n", lista->dado);
      printf ("próximo dado: %c\n", lista->prox->dado);
      printf (" p1->prox->dado: %c\n", p1->prox->dado);
      printf ("===========\n");
      p1 = lista;
   }

   pinicio->ant = lista;
   printf("pinicio->ant: %c\n", pinicio->ant->dado);
   printf("pinicio->dado: %c\n", pinicio->dado);
   printf("pinicio->prox: %c\n", pinicio->prox->dado);
   free (lista);
   p1=pinicio;

   do {
      printf("%c\n", p1->dado);
      p1=p1 -> prox;
   } while (p1 != pinicio );

   return 0;
   }

It mount a list of chars from 'a' to 'j'

And after, it prints the characters of the list.

But it doesn't printf 'j', instead it be in the list.

도움이 되었습니까?

해결책

The critical problem is on this line:

  free (lista);

Below shows your doubly-linked list when the above 'free()' is called:

enter image description here

Perhaps this 'free()' line should be deleted?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top