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_usun(struct element *head, struct element *x);
- struct element *lista_szukaj(struct element *head, int liczba);
- struct element *lista_dodaj(struct element *head, struct element *nowy);
- //struct element *lista_odwroc(struct element *head);
- //void zwolnij_pamiec(struct element *head);
- main()
- {
- struct element *head=NULL, *nowy=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': 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': case 'W':
- lista_wyswietl(head);
- break;
- case 's': case 'S':
- printf("\nPodaj wartosc elementu do znalezienia: ");
- scanf("%d",&liczba);
- nowy = lista_szukaj(head, liczba);
- if(nowy != NULL) printf("\nElement znaleziono pod adresem %p", nowy);
- else printf("\nNie znaleziono elementu");
- break;
- case 'u': case 'U':
- printf("\nPodaj wartosc elementu do usuniecia: ");
- scanf("%d",&liczba);
- nowy = lista_szukaj(head, liczba);
- if(nowy != NULL){
- head = lista_usun(head, nowy);
- }
- else printf("\nNie usunieto elementu");
- break;
- case 'q': case 'Q':
- return 0;
- default:
- printf("\nBlad wyboru");
- break;
- }
- }
- }
- void lista_wyswietl(struct element *head)
- {
- struct element *x=head;
- while(x!=NULL)
- {
- printf("%d ",x->k);
- x=x->next;
- }
- }
- 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 liczba)
- {
- struct element *x=head;
- while(x!=NULL && x->k!=liczba)
- {
- x=x->next;
- }
- return x;
- }
- 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;
- }
- /*void zwolnij_pamiec(struct element *head)
- {
- free(head);
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement