Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "stdio.h"
- #include "stdlib.h"
- struct lista {
- int liczba;
- struct lista *next;
- };
- lista* create(int value) //tworzenie listy
- \\MIEJSCE NA TWOJ KOD
- void wyswietl(lista* head)//wyswietlanie listy
- {
- lista * p = head;
- while (p != NULL)
- {
- printf("%d->", p->liczba);
- p = p->next;
- }
- }
- lista* get_tail(lista* head)//wyswietlanie ostatniego elementu
- {
- lista * p = head;
- if (head == nullptr)
- {
- printf("Lista jest pusta");
- return nullptr;
- }
- while (true)
- {
- if (p->next == nullptr)
- break;
- p = p->next;
- }
- return p;
- }
- void add(lista*& root, int value)
- {
- lista * p = root;
- while (true)
- {
- if (root == nullptr)
- {
- lista *obj = (lista*)malloc(sizeof(lista));
- obj->liczba = value;
- obj->next = nullptr;
- root = obj;
- break;
- }
- if (p->next == nullptr)
- {
- lista *obj = (lista*)malloc(sizeof(lista));
- obj->liczba = value;
- obj->next = nullptr;
- p->next = obj;
- break;
- }
- p = p->next;
- }
- }
- lista* find(lista* root, int value)
- {
- lista * p = root;
- if (root == nullptr)
- {
- printf("Lista jest pusta ");
- return nullptr;
- }
- while (true)
- {
- if (p->liczba == value)
- {
- printf("Znaleziono elemenent!\n");
- return p;
- }
- if (p->next == nullptr)
- {
- printf("Elementu nie znaleziono!");
- return nullptr;
- }
- p = p->next;
- }
- }
- lista* remove(lista*& head, int value) //usuwanie elementu
- {
- lista *p = head; //pierwszy element
- lista *poprzednik = NULL;
- while (p != nullptr)
- {
- if (p->liczba == value)
- {
- if (p == head) //element jest pierwszy
- {
- head = p->next;
- delete p;
- }
- else //element jest w srodku albo koncu
- {
- poprzednik->next = p->next;
- delete p;
- }
- return head;
- }
- poprzednik = p;
- p = p->next;
- }
- printf("Nie ma elementu");
- return nullptr;
- }
- void free_all(lista*& root)
- {
- while (root != nullptr)
- {
- const auto del = get_tail(root);
- if (del != nullptr)
- remove(root, del->liczba);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement