#include #include #include struct tl{ int key; tl *prev,*next; } *head=NULL,*x=NULL; struct tl *push(struct tl *head,struct tl*x){ x->prev=NULL; x->next=head; if(head) head->prev=x; return x; } // zwykle wypisanie listy void list(tl *head){ while(head){ printf("%d ",head->key); head=head->next; } } // rekurencyjne wypisanie listy void listr(tl *head){ if(head) { printf("%d ",head->key); listr(head->next); } } // wypisanie dlugosci listy int len(tl *head){ int l=0; while(head){ l++; head=head->next; } return l; } // rekurencyjne wypisanie dlugosci listy int lenr(tl *head){ if(head==NULL) return 0; else return lenr(head->next)+1; } // przeszukiwanie listy tl *search(tl *head,int k){ while((head!=NULL)&&(head->key!=k)) head=head->next; return head; } // usuniecie elementu listy tl *eject(tl *head, tl *x){ if(x->next) if(x->prev){ x->prev->next=x->next; x->next->prev=x->prev; } else{ x->next->prev=NULL; head=x->next; } else if(x->prev) x->prev->next=NULL; else head=NULL; free(x); return head; } // wskazanie na ogon listy tl *tail(tl *head){ while(head->next) head=head->next; return head; } // usuniecie ogonu listy tl *erasetail(tl *head){ tl *x=tail(head); if(x->prev==NULL) head=NULL; else x->prev->next=NULL; free(x); return head; } int m=head->key; head=head->next; while(head){ if(head->key->m)m=head->key head=head->next; } return m; // usuniecie listy void eraselist(tl *head){ while(head){ tl *x=head; head=head->next; free(x); } } int main(void) { int i=0,n=0; printf("---LISTY---\n\nliczba elementow listy="); scanf("%d",&n); for(i=1;i<=n;i++){ x=(tl*)malloc(sizeof(tl)); x->prev=x->next=NULL; x->key=i; head=push(head,x); } printf("\n---ZWYKLE WYPISANIE LISTY---\n"); list(head); printf("\n---REKURENCYJNE WYPISANIE LISTY---\n"); listr(head); printf("\n---PRZESZUKIWANIE LISTY---\nelement do wyszukania="); scanf("%d",&i); x=search(head,i); if (x)printf("jest=%d",x->key); else printf("nie ma"); printf("\n---WYPISANIE DLUGOSCI LISTY---\n"); printf("length=%d",len(head)); printf("\n---REKURENCYJNE WYPISANIE DLUGOSCI LISTY---\n"); printf("length=%d",lenr(head)); printf("\n---USUNIECIE ELEMENTU LISTY---\nelement do usuniecia="); scanf("%d",&i); x=search(head,i); if(x) head=eject(head,x); list(head); printf("\n---WSKAZANIE NA OGON LISTY---\n"); x=tail(head); printf("tail=%d",x->key); printf("\n---USUNIECIE OGONU LISTY---\n"); erasetail(head); list(head); printf("\n---WYPISANIE WARTOSCI MAKSYMALNEJ LISTY---\n"); max(head); printf("\n---USUNIECIE LISTY--- uszkodzone\n"); eraselist(head); list(head); getche(); return 0; }