Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- Node* next;
- };
- Node* arrayToList(const int arr[], size_t size) {
- Node *pierwsza = new Node();
- pierwsza->data = arr[0];
- Node *current = pierwsza;
- for (int i = 1; i < size; i++) {
- Node *nowa = new Node();
- nowa->data = arr[i];
- current->next = nowa;
- current = nowa;
- }
- return pierwsza;
- }
- void showList(const Node* head) {
- if (head != NULL) {
- while (head != NULL) {
- cout << head->data << endl;
- head = head->next;
- }
- }
- else {
- cout << "List is empty" << endl;
- }
- }
- void deleteList(Node*& head) {
- Node *temp;
- temp = head;
- while (head != NULL)
- {
- head = head->next;
- delete temp;
- temp = head;
- std::cout << "Spadam stad" << std::endl;
- }
- }
- Node* removeOdd(Node* head)
- {
- Node *aktualny = head;
- Node *poprzedni = NULL;
- while (aktualny != NULL)
- {
- if (aktualny->data % 2 != 0)
- {
- if (aktualny == head)
- head = head->next;
- else
- poprzedni->next = aktualny->next;
- Node* tmp = aktualny;
- aktualny = aktualny->next;
- delete tmp;
- }
- else
- {
- poprzedni = aktualny;
- aktualny = aktualny->next;
- };
- };
- return head;
- };
- int main() {
- int arr[] = { 1,2,3,4,5,6 };
- size_t size = sizeof(arr) / sizeof(*arr);
- Node* head = arrayToList(arr, size);
- showList(head);
- head = removeOdd(head);
- showList(head);
- deleteList(head);
- showList(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement