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