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 szukana);
- struct element *lista_usun(struct element *head, struct element *do_usuniecia);
- struct element *lista_odwroc(struct element *head);
- int 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': 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 jest pusta");
- else
- {
- printf("\nWpisz wartosc szukana:");
- scanf("%d", &liczba);
- nowy=lista_szukaj(head, liczba);
- if (nowy != NULL)
- printf("\nznaleziono pod adresem %d", nowy);
- else
- printf("\nnie znaleziono");
- }
- break;
- case 'u': if (head==NULL)
- printf("\nLista jest pusta");
- else
- {
- printf("\nWpisz wartosc do usuniecia:");
- scanf("%d",&liczba);
- if(lista_szukaj(head,liczba)==NULL)
- printf("nie znaleziono takiego elementu");
- else
- lista_usun(head,lista_szukaj(head,liczba) );
- }
- break;
- case 'o': if(head==NULL)
- printf("\nLista jest pusta");
- else head=lista_odwroc(head);
- break;
- case 'q': return 0;
- }
- }
- }
- 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 szukana)
- {
- struct element *x=head;
- while((x!=NULL) && (x->k!=szukana))
- {
- x=x->next;
- }
- return x;
- }
- struct element *lista_usun(struct element *head, struct element *do_usuniecia)
- {
- if(do_usuniecia->prev!=NULL)
- do_usuniecia->prev->next=do_usuniecia->next;
- else head=do_usuniecia->next;
- if(do_usuniecia->next!=NULL)
- do_usuniecia->next->prev=do_usuniecia->prev;
- free(do_usuniecia);
- return head;
- }
- struct element *lista_odwroc(struct element *head)
- {
- struct element *lp=NULL;
- struct element *x=head;
- struct element *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;
- }
Add Comment
Please, Sign In to add comment