[C][SOLVED] - wyświetlanie kolejki LIFO

hej. poniżej struktury, i funkcja push użyta do konstruowania kolejki:

struct record{

 int year,month,day,dvds;

 float nosnik,przychod,zarobek;

}wiersz;


struct base {

struct record node;

struct base *prev,*next;

}db;


struct base_holder{

struct base *head,*tail;

}holder;


(...)


void push(struct record wiersz){

struct base *newptr;

newptr=(struct base*)malloc(sizeof(struct base));

newptr->node=wiersz;

newptr->next=NULL;

newptr->prev=NULL;


if (holder.head==NULL && holder.tail==NULL){

    holder.head=newptr;

    holder.tail=newptr;

    }


if (holder.head!=NULL && holder.tail!=NULL){

    newptr->next=holder.head;

    holder.head->prev=newptr;

    holder.head=newptr;

    }

}

i funkcja wyświetlająca zawartość kolejki:

void queue_print(){

struct base *temp;

temp=(struct base*)malloc(sizeof(struct base));

temp=holder.head;


while (temp->next!=NULL)

//int i;

//for(i=1;i<200;i++)

{

    wiersz=temp->node;

    printf("%u-%-0.2u-%-0.2u%10u%10f%10f\n",wiersz.year,wiersz.month,wiersz.day,wiersz.dvds,wiersz.nosnik,wiersz.przychod,wiersz.zarobek);

    temp=temp->next;

    }

 }

ostatnia funkcja, a właściwie parametr pętli while jest niepoprawny - pętla nieskończona.

Jeżeli while zamienimy na FOR (wykomentowany), zawartość pętli wykonuje sie poprawnie.

Co zmienić w while, żeby działało?

Używasz zmiennych globalnych w funkcjach?

tak, tylko

struct base_holder holder;

struct record wiersz;

Dodane 12.11.2010 (Pt) 13:39 – trochę pozmieniałem i działa.:smiley:

void push(struct record wiersz){

struct base *newptr;

newptr=(struct base*)malloc(sizeof(struct base));

newptr->node=wiersz;

newptr->next=NULL;

newptr->prev=NULL;


if (holder.head==NULL && holder.tail==NULL){

    holder.head=newptr;

    holder.tail=newptr;

    }


if (holder.head!=NULL && holder.tail!=NULL){

    newptr->prev=holder.head;

    holder.head->next=newptr;

    holder.head=newptr;

    }

}


void queue_print(){

    struct base *temp;

    temp=holder.tail;


    while (temp->next!=NULL)

        {

        wiersz=temp->node;

        printf("%u-%-0.2u-%-0.2u%10u%10f%10f\n",wiersz.year,wiersz.month,wiersz.day,wiersz.dvds,wiersz.nosnik,wiersz.przychod,wiersz.zarobek);

        temp=temp->next;

        }

 }

Oj nie działa, nie może. Przeanalizuj pierwsze dwa if’a, robi ci się lista cykliczną.

Powinno być:

struct base *newptr;newptr=(struct base*)malloc(sizeof(struct base));newptr-node=wiersz;newptr-prev=NULL;newptr-next=holder.head;holder.head=(holder.head?holder.head-prev:holder.tail)=newptr; [/code]

zaś pętla:


[code=php]struct base *temp;temp=holder.head;temp)