Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /*
- listy dwukierunkowe
- 1. dodawanie elementow
- a) na koniec
- b) na poczatek
- c) po elemencie o podanej wartosci data
- 2. usuwanie elementow
- a) z konca
- b) z poczatku
- c) o podanej wartosci
- 3. wyswietlanie
- a) od konca
- b) od poczatku
- 4. wyszukiwanie elementu o podanym numerze
- 5. menu w mainie
- */
- struct element
- {
- int data;
- struct element *next;
- struct element *prev;
- };
- struct lista
- {
- struct element *head;
- struct element *tail;
- int count;
- };
- void pushBack(int newdata, struct lista* list);
- void pushFront (int newdata, struct lista* list);
- void pushAfter (int newdata, struct lista* list);
- void popBack(struct lista* list);
- void popFront(struct lista* list);
- void popAfter(struct lista* list);
- void wyswietlFront(struct lista* list);
- void wyswietlBack(struct lista* list);
- struct element *szukaj2(struct lista* list);
- int szukaj (struct lista* list);
- int main()
- {
- int wybor1,wartosc;
- char wybor2;
- struct lista* list = (struct element*)malloc(sizeof(struct element));
- list->head = NULL;
- list->tail = NULL;
- list->count = 0;
- do
- {
- printf("count %d\n\n", list->count);
- printf("LISTY DWUKIERUNKOWE:\n1.Dodanie elementu\n2.Usuniecie elementu\n3.Wyswietlenie\n4.Wyszukiwanie elementu o podanym numerze\n5.Zakonczenie dzialania programu\nWybor: ");
- scanf("%d", &wybor1);
- if (wybor1 == 1)
- {
- printf("\na. na koniec listy\nb. na poczatek listy\nc. po elemencie o podanej wartosci\nWybor (a/b/c):");
- getchar();
- scanf("%c", &wybor2);
- switch (wybor2)
- {
- case 'a':
- printf("Podaj wartosc dodawanego elementu: ");
- scanf("%d", &wartosc);
- pushBack(wartosc, list);
- break;
- case 'b':
- printf("Podaj wartosc dodawanego elementu: ");
- scanf("%d", &wartosc);
- pushFront(wartosc, list);
- break;
- case 'c':
- printf("Podaj wartosc dodawanego elementu: ");
- scanf("%d", &wartosc);
- pushAfter(wartosc, list);
- break;
- default:
- printf("Podano wartosc spoza zakresu");
- break;
- }
- }
- if (wybor1 == 2)
- {
- printf("\na. z konca listy\nb. z poczatku listy\nc. elementu o podanej wartosci\nWybor (a/b/c):");
- getchar();
- scanf("%c", &wybor2);
- switch (wybor2)
- {
- case 'a':
- popBack(list);
- break;
- case 'b':
- popFront(list);
- break;
- case 'c':
- popAfter(list);
- break;
- default:
- printf("Podano wartosc spoza zakresu");
- break;
- }
- }
- if (wybor1 == 3)
- {
- printf("\na. od konca\nb. od poczatku\nWybor (a/b/c):");
- getchar();
- scanf("%c", &wybor2);
- switch (wybor2)
- {
- case 'a':
- wyswietlBack(list);
- break;
- case 'b':
- wyswietlFront(list);
- break;
- default:
- printf("Podano wartosc spoza zakresu");
- break;
- }
- }
- if (wybor1 == 4)
- {
- int indeks=szukaj(list);
- if (indeks == 0) printf("Szukany element nie znajduje sie na liscie.\n");
- else printf("Szukany element jest elementem nr %d.\n", indeks);
- }
- if (wybor1 == 5) printf("Koncze dzialanie programu");
- getchar();
- getchar();
- system("cls");
- } while (wybor1 != 5);
- return 0;
- }
- void pushBack(int newdata, struct lista* list)
- {
- struct element* newEl = (struct element*)malloc(sizeof(struct element));
- newEl->data = newdata;
- newEl->next = NULL;
- if (list->count == 0)
- {
- list->head=newEl;
- list->tail=newEl;
- newEl->prev=NULL;
- }
- else
- {
- newEl->prev=list->tail;
- list->tail->next=newEl;
- list->tail=newEl;
- }
- list->count++;
- }
- void pushFront (int newdata, struct lista* list)
- {
- struct element* newEl = (struct element*)malloc(sizeof(struct element));
- newEl->data = newdata;
- newEl->prev = NULL;
- if (list->count == 0)
- {
- list->head=newEl;
- list->tail=newEl;
- newEl->next = NULL;
- }
- else
- {
- newEl->next=list->head;
- list->head->prev=newEl;
- list->head=newEl;
- }
- list->count++;
- }
- void pushAfter(int newdata, struct lista* list)
- {
- struct element* newEl = (struct element*)malloc(sizeof(struct element));
- struct element* szukany = (struct element*)malloc(sizeof(struct element));
- szukany=szukaj2(list);
- if (szukany == NULL) pushBack(newdata, list);
- else
- {
- if (szukany == list->tail) pushBack(newdata, list);
- else
- {
- newEl->data = newdata;
- newEl->prev = szukany;
- newEl->next = szukany->next;
- szukany->next->prev = newEl;
- szukany->next = newEl;
- list->count++;
- }
- }
- }
- void popBack(struct lista* list)
- {
- if (list->count == 0) printf("Lista jest pusta\n");
- if (list->count == 1)
- {
- free(list->tail);
- free(list->head);
- list->count=0;
- return;
- }
- if (list->count > 1)
- {
- list->tail=list->tail->prev;
- free(list->tail->next);
- list->tail->next=NULL;
- list->count--;
- }
- }
- void popFront(struct lista* list)
- {
- if (list->count == 0) printf("Lista jest pusta\n");
- if (list->count == 1)
- {
- free(list->head);
- free(list->tail);
- list->count=0;
- return;
- }
- if (list->count > 1)
- {
- list->head=list->head->next;
- free(list->head->prev);
- list->head->prev=NULL;
- list->count--;
- }
- }
- void popAfter(struct lista* list)
- {
- struct element* usuwany = (struct element*)malloc(sizeof(struct element));
- usuwany=szukaj2(list);
- if (usuwany == NULL) return;
- else
- {
- if (usuwany->prev == NULL)
- {
- popFront(list);
- return;
- }
- if (usuwany->next == NULL)
- {
- popBack(list);
- return;
- }
- if (usuwany->prev && usuwany->next)
- {
- usuwany->next->prev = usuwany->prev;
- usuwany->prev->next = usuwany->next;
- free(usuwany);
- list->count--;
- }
- }
- }
- void wyswietlFront(struct lista* list)
- {
- if (list->count == 0) printf("Lista jest pusta\n");
- else
- {
- struct element* el = (struct element*)malloc(sizeof(struct element));
- el = list->head;
- while (el)
- {
- printf("%d, ", el->data);
- el = el->next;
- }
- }
- }
- void wyswietlBack(struct lista* list)
- {
- if (list->count == 0) printf("Lista jest pusta\n");
- else
- {
- struct element* el = (struct element*)malloc(sizeof(struct element));
- el = list->tail;
- while (el)
- {
- printf("%d, ", el->data);
- el = el->prev;
- }
- }
- }
- struct element *szukaj2(struct lista* list)
- {
- int szukana;
- printf("Podaj wartosc elementu o ktory Ci chodzi: ");
- scanf("%d", &szukana);
- struct element* el = (struct element*)malloc(sizeof(struct element));
- el = list->head;
- if (list->count == 0) return NULL;
- else
- {
- while (1)
- {
- if (el->data == szukana) break;
- if (el->next) el = el->next;
- else return NULL;
- }
- return el;
- }
- }
- int szukaj (struct lista* list)
- {
- int szukana,indeks=1;
- printf("Podaj wartosc elementu ktorego szukasz: ");
- scanf("%d", &szukana);
- if (list->count == 0) return 0;
- else
- {
- struct element* el = (struct element*)malloc(sizeof(struct element));
- el = list->head;
- while (1)
- {
- if (el->data == szukana) break;
- if (el->next)
- {
- el = el->next;
- indeks++;
- }
- else return 0;
- }
- return indeks;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement