Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct element
- {
- int k;
- struct element*prev;
- struct element*next;
- };
- void lista_wyswietl(struct element *head);
- struct element *lista_dodaj(struct element *head, struct element *nowy);
- struct element *lista_szukaj(struct element *head, int sz);
- struct element *lista_odwroc(struct element *head);
- struct element *lista_usun(struct element *head, struct element *el);
- struct element *lista_zwolnij(struct element *head);
- main()
- {
- struct element *head=NULL, *nowy=NULL, *el=NULL;
- char z;
- int liczba;
- while(1)
- {
- printf("\nco chcesz zrobic?");
- printf("\nd - dodac");
- printf("\ns - szukac");
- printf("\nu - usunac");
- printf("\no – odwrocic liste");
- printf("\nw - wyswietlic");
- printf("\nq - wyjsc\n");
- fflush(stdin);
- z=getchar();
- switch(z)
- {
- case 'd':
- nowy=(struct element*)malloc(sizeof(struct element));
- printf("\npodaj wartosc elementu do wstawienia: ");
- scanf("%d",&liczba);
- nowy->k=liczba;
- head=lista_dodaj(head,nowy);
- break;
- case 'w': lista_wyswietl(head);
- break;
- case 's':
- if(head==NULL)
- printf("\nLista pusta");
- printf("Podaj wartosc do wyszukania: ");
- scanf("%d", &liczba);
- el=lista_szukaj(head, liczba);
- if(el==NULL)
- printf("\nNie znaleziono elementu");
- else
- printf("Znaleziono element: %d", el);
- break;
- case 'u':
- if(head==NULL)
- printf("\nLista pusta");
- printf("Podaj wartosc do usuniecia: ");
- scanf("%d", &liczba);
- el=lista_szukaj(head, liczba);
- if(el==NULL)
- printf("Nie znaleziono elementu");
- else
- {
- head=lista_usun(head,el);
- printf("\nElement poprawnie usuniety");
- }
- break;
- case 'o': head=lista_odwroc(head);
- break;
- case 'q':
- //head=lista_zwolnij(head);
- return 0;
- }
- }
- }
- 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;
- }
- struct element *lista_szukaj(struct element *head, int sz)
- {
- struct element *x=head;
- while((x!=NULL) && (x->k!=sz))
- {
- x=x->next;
- }
- return x;
- }
- void lista_wyswietl(struct element *head)
- {
- struct element*x=head;
- while(x!=NULL)
- {
- printf("%d ",x->k);
- x=x->next;
- }
- }
- struct element *lista_odwroc(struct element *head)
- {
- struct element *LP=NULL, *x=head, *y=NULL;
- while(x!=NULL)
- {
- y=(struct element*)malloc(sizeof(struct element));
- y->k=x->k;
- LP=lista_dodaj(LP,y);
- x=x->next;
- }
- return LP;
- }
- struct element *lista_usun(struct element *head, struct element *x)
- {
- if(x->prev!=NULL)
- x->prev->next=x->next;
- else
- head=x->next;
- if(x->next!=NULL)
- x->next->prev=x->prev;
- free(x);
- return head;
- }
- /*struct element *lista_zwolnij(struct element *head)
- {
- struct element *x;
- while(head!=NULL);
- {
- x=head->next;
- free(head);
- head=x;
- }
- return head;
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement