2015-05-27 159 views
-1

麻烦我有问题,当我尝试打印双向链表从头部到尾部。它从中间的某个地方开始,然后从头开始,等等。存在与功能“插入件”,并在端部(“nadji BLOK”)一个功能被打印的列表的C代码。关注函数“insert”和“nadji blok-(find block)”。Doubley列表,打印

#include "dodatno.h" 
#include "algoritam.h" 
#include<string.h> 
#include<stdio.h> 
#include <stdlib.h> 

struct at { 
    char ime_prezime[51]; 
    long pocetak_bloka; 
    struct at *next, *before; 
}; 
typedef struct at atom; 

struct st{ 
    struct at *head, *tail; 
    char medijan[51]; 
} indeks; 

int insert(char *ime_prezime, long pocetak_bloka) 
{ 
    atom *new; 
    if ((new = (atom*)malloc(sizeof(atom)))) 
    { 
     strcpy(new->ime_prezime, ime_prezime); 
     new->pocetak_bloka = pocetak_bloka; 
     printf(" DODAJ U LISTU : %s \n", new->ime_prezime); /* control */ 

     new->next = NULL; 
     new->before = NULL; 

     if (indeks.head == NULL) 
     { 
      indeks.head = new; 
      indeks.tail = new; 
     } 
     else{ 
      indeks.tail->next = new; 
      new->before = indeks.tail; 
      indeks.tail = new; 
     } 
     return 1; 
    } 
    return 0; 
} 

void inicijaliziraj(FILE *ulaz) 
{ 
    int i; long pocetak; 
    struct stanovnik buff; 
    for (i = 0; i<100; i++) 
    { 
     fseek(ulaz, i*sizeof(buff)*VELICINA, SEEK_SET); 
     fread(&buff, sizeof(buff), 1, ulaz); 
     /* printf(" FREAD : %s", buff.ime_prezime);*/ 
     fseek(ulaz, -1 * sizeof(buff), SEEK_CUR); 
     pocetak = ftell(ulaz); 
     DodajUListu(buff.ime_prezime, pocetak); 
    } 
    return; 
} 

long nadji_blok(char *ime_prezime, FILE *ulaz) 
{ 
    struct stanovnik vel; 
    atom *p = indeks.head; 

    for (; p; p = p->next) 
    { 
     printf("%s\n ", p->ime_prezime); 
     if (strcmp(ime_prezime, p->ime_prezime) < 0) 
      /* in this for loop it goes indefenetly */ 
     { 
      return (p->pocetak_bloka) - (VELICINA*sizeof(vel)); 
      break; 
     } 
    } 
    return 1; 
} 

char *izl_naziv() 
{ 
    char *s = "indeksna_pretraga.rez"; 
    return s; 
} 
+0

为什么不一步通过在调试器的代码,看看这是怎么回事上 ? –

+1

你忘了问一个问题。你需要什么帮助? –

+0

函数“nadji BLOK”虽然我尽量走线槽列表(在for循环)它会很长,它不会停止 –

回答

1

看起来好像你在这里有几个问题。首先是

struct st{ 
    struct at *head, *tail; 
    char medijan[51]; 
} indeks; 

应该

struct st{ 
    struct at head, tail; 
    char medijan[51]; 
} indeks; 

,因为你要headtail是用自己的nextbefore指针实际元素。

现在,你需要在你的代码(一些初始化代码)这样​​的点indeks.headindeks.tail

indeks.head->next = &indeks.tail; 
indeks.head->before = &indeks.tail; 
indeks.tail->next = &indeks.head; 
indeks.tail->before = &indeks.head; 

接下来,我会单独拿出你的“新”对象的分配和从加油吧插入到列表中。这只是更清洁。

现在,当你要插入你会做这样的事情:

void foo(char *ime_prezime, long pocetak_bloka) 
{ 
    atom *new; 

    if ((new = (atom*)malloc(sizeof(atom)))) { 
     strcpy(new->ime_prezime, ime_prezime); 
     new->pocetak_bloka = pocetak_bloka; 
     printf(" DODAJ U LISTU : %s \n", new->ime_prezime); /* control */ 
     insert(&indeks.head, new); 
    } 
} 

void insert(atom *at_location, atom *element) 
{ 
    element->next = at_location->next; 
    element->before = element->next->before; 
    element->next->before = element; 
    at_location->next = element; 
} 

打印的清单:

void print_list(void) 
{ 
    atom *element; 

    element = &indeks.head->next; 

    while (element != &indeks.head) { 
     // print whatever info 
     element = element->next; 
    } 
}