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*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 *s=head;
- while(s!=NULL && (s->k)!=liczba)
- {
- s=s->next;
- }
- return s;
- }
- struct element* lista_usun(struct element *head, int liczba)
- {
- struct element *u=NULL;
- u=lista_szukaj(head,liczba);
- while(u==NULL)
- {
- printf("Nie ma takiego elementu do usuniecia. Mozliwe elementy do usuniecia:\n");
- lista_wyswietl(head);
- printf("\nPodaj prawidlowy element do usuniecia:\n");
- scanf("%d",&liczba);
- u=lista_szukaj(head,liczba);
- }
- if(u->prev!=NULL)
- u->prev->next=u->next;
- else
- head=u->next;
- if(u->next!=NULL)
- u->next->prev=u->prev;
- free(u);
- return head;
- }
- struct element* lista_odwroc(struct element *head)
- {
- struct element *x=head;
- struct element *head2=NULL;
- struct element *y;
- while(x!=NULL)
- {
- y=(struct element*)malloc(sizeof(struct element));
- y->k=x->k;
- head2=lista_dodaj(head2,y);
- x=x->next;
- }
- head=head2;
- return head;
- }
- struct element * lista_zwolnij(struct element *head)
- {
- struct element *nast;
- while(head!=NULL)
- {
- nast=head->next;
- free(head);
- head=nast;
- }
- return head;
- }
- int main()
- {
- struct element *head=NULL, *nowy=NULL, *x=NULL;
- char z;
- int liczba;
- while(1)
- {
- system("cls");
- printf("Co 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 's':
- printf("Podaj liczbe do wyszukania: ");
- fflush(stdin);
- scanf("%d",&liczba);
- x=lista_szukaj(head,liczba);
- if(x==NULL)
- printf("Brak szukanego elementu.\n");
- else
- printf("Adres szukanego elementu:%p\n",x);
- getch();
- break;
- case 'u':
- printf("Podaj cyfre do usuniecia: ");
- scanf("%d",&liczba);
- head=lista_usun(head,liczba);
- printf("Element zostal usuniety.\n");
- getch();
- break;
- case 'o':
- head=lista_odwroc(head);
- printf("Lista zostala odwrocona.\n");
- getch();
- break;
- case 'w':
- lista_wyswietl(head);
- getch();
- break;
- case 'q':
- head=lista_zwolnij(head);
- return 0;
- default :
- printf("Podano zly znak. Podaj poprawny.");
- getch();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement