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*pev;
- struct element*next;
- }
- struct element *lista_dodaj(struct element *head, struct element *nowy);
- struct element *lista_usun(struct element*head, struct element*x)
- main()
- {
- struct element *head=NULL, *nowy=NULL, *szukana=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':
- printf("\npodaj element ktory chcesz wyszukac: ");
- scanf("%d", &liczba);
- lista_wyszukaj(head, szukana);
- break;
- case 'u':
- lista_usun(head);
- 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_wyszukaj(struct element*head , int sz)
- {
- struct element*x=head;
- while(x!=NULL && x->k!=sz)
- x=x->next;
- else
- return sz;
- }
- struct element *lista_usun(struct element*head, struct element*x)
- {
- if(x->prev != NULL)
- x-> next -> prev = x-> prev;
- else
- head=x->prev;
- if(x->next != NULL)
- x-> prev -> next = x-> next;
- free(x);
- return head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement