Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct element
- {
- int k;
- struct element *prev;
- struct element *next;
- };
- struct element *lista_dodaj(struct element *head,struct element *nowy);
- void lista_wyswietl(struct element*head);
- struct element *lista_szukaj(struct element *head,int x);
- struct element *lista_usun(struct element *head,struct element *x);
- struct element *lista_odwroc(struct element*head);
- struct element *lista_zwolnij(struct element *head);
- struct element *lista_min(struct element *head);
- struct element *lista_sort(struct element*head);
- main()
- {
- struct element *head=NULL,*nowy=NULL,*e=NULL;
- char z;
- int liczba;
- while(1)
- {
- printf("\nCo chcesz zrobic?");
- printf("\nd - dodac");
- printf("\ns - szukac");
- printf("\nu - usunac");
- printf("\no - odwrocic");
- printf("\nw - wyswietlic");
- printf("\nm - minimum");
- printf("\nv - sort");
- printf("\nq - wyjsc\n");
- fflush(stdin);
- z = getch();
- switch(z)
- {
- case 'd':
- nowy=(struct element*)malloc(sizeof(struct element));
- printf("\nPodaj wartosc elementu:");
- scanf("%d",&liczba);
- nowy->k=liczba;
- head=lista_dodaj(head,nowy);
- break;
- case 'w':
- printf("\n");
- lista_wyswietl(head);
- printf("\n");
- break;
- case 's':
- printf("Podaj wartosc szukana:");
- scanf("%d",&liczba);
- e=lista_szukaj(head,liczba);
- if(e!=NULL) printf("\n Szukany element znaleziony pod adresem 0x%x",e);
- else printf("\nBrak szukanego elementu\n");
- break;
- case 'u':
- printf("\nPodaj element do usuniecia");
- scanf("%d",&liczba);
- e=lista_szukaj(head,liczba);
- if(e!=NULL)
- {
- head=lista_usun(head,e);
- lista_wyswietl(head);
- } else printf("\n brak danego elementu na liscie\n");
- case 'o':
- head=lista_odwroc(head);
- printf("\nLista odwrocona");
- lista_wyswietl(head);
- break;
- case 'm':
- e=lista_min(head);
- if(e!=NULL)
- {
- printf("Minimum wynosi %d, adres: 0x%x",e->k,e);
- }else printf("Brak liczb");
- break;
- case 'v':
- head=lista_sort(head);
- lista_wyswietl(head);
- break;
- }
- }
- }
- struct element *lista_dodaj(struct element *head,struct element *nowy)
- {
- nowy->prev=NULL;
- nowy->next=head;
- if(head!=NULL) head->prev=nowy;
- head=nowy;
- return head;
- }
- void lista_wyswietl(struct element*head)
- {
- struct element *x=head;
- if(head==NULL)printf("lista pusta");
- while(x!=NULL)
- {
- printf("%d",x->k);
- x=x->next;
- }
- }
- struct element *lista_szukaj(struct element *head,int x)
- {
- struct element *e=head;
- while(e!=NULL)
- {
- if(e->k==x) return e;
- e=e->next;
- }
- return NULL;
- }
- struct element *lista_usun(struct element *head,struct element *x)
- {
- if(x->prev==NULL) head=x->next;
- else x->prev->next=x->next;
- if(x->next!=NULL) x->next->prev=x->prev;
- //free(x);
- return head;
- }
- struct element *lista_odwroc(struct element*head)
- {
- struct element *nowa=NULL,*x=head,*e=NULL;
- while(x!=NULL)
- {
- e=(struct element*)malloc(sizeof(struct element));
- e->k=x->k;
- nowa=lista_dodaj(nowa,e);
- x=x->next;
- }
- lista_zwolnij(head);
- return nowa;
- }
- struct element *lista_zwolnij(struct element *head)
- {
- struct element *e=head;
- while(head!=NULL)
- {
- e=head->next;
- free(head);
- head=e;
- }
- return head;
- }
- struct element *lista_min(struct element *head)
- {
- struct element *x=head,*min;
- min=(struct element*)malloc(sizeof(struct element));
- if (head==NULL)
- return NULL;
- min->k=x->k;
- while(x!=NULL)
- {
- if(min->k > x->k)
- {
- min->k=x->k;
- } else x=x->next;
- }
- return min;
- }
- struct element *lista_sort(struct element*head)
- {
- struct element*nowy,*min,*x=head;
- min=(struct element*)malloc(sizeof(struct element));
- while(x!=NULL)
- {
- min=lista_min(x);
- lista_usun(x,min);
- nowy=(struct element*)malloc(sizeof(struct element));
- lista_dodaj(nowy,min);
- x=x->next;
- }
- lista_odwroc(nowy);
- return nowy;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement