Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node
- {
- int val;
- node *next, *prev;
- };
- void del(node*& head, node*& tail, node*& p, int &count)
- {
- if (p->prev)
- p->prev->next = p->next;
- else
- head = p->next;
- if (p->next)
- p->next->prev = p->prev;
- else
- tail = p->prev;
- delete p;
- count--;
- }
- void ins_tail(node*& head, node*&tail, int x, int &count)
- {
- node* nowy = new node;
- nowy->next = NULL;
- nowy->prev = NULL;
- nowy->val = x;
- if (!head)
- {
- head = nowy;
- tail = nowy;
- }
- else
- {
- nowy->prev = tail;
- tail->next = nowy;
- tail = nowy;
- }count++;
- }
- void ins_head(node*& head, node*& tail, int x, int &count)
- {
- node* nowy = new node;
- nowy->next = NULL;
- nowy->prev = NULL;
- nowy->val = x;
- if (!head)
- {
- head = nowy;
- tail = nowy;
- }
- else
- {
- nowy->next = head;
- head->prev = nowy;
- head = nowy;
- }count++;
- }
- void ins_after(node*& p, int x, int &count)
- {
- node* nowy = new node;
- nowy->val = x;
- nowy->next = p->next;
- nowy->prev = p;
- p->next->prev = nowy;
- p->next = nowy;
- count++;
- }
- void ins_before(node*& p, int x, int &count)
- {
- node* nowy = new node;
- nowy->val = x;
- nowy->next = p;
- nowy->prev = p->prev;
- p->prev->next = nowy;
- p->prev = nowy;
- count++;
- }
- void show(node* head)
- {
- while (head)
- {
- cout << head->val << " ";
- head = head->next;
- }cout << endl;
- }
- int main()
- {
- node* head = NULL;
- node* tail = NULL;
- int wybor=0, liczba=0, numer=0, count=0;
- node* p = NULL;
- do
- {
- p = head;
- system("CLS");
- cout << "Elementy listy: " << endl;
- show(head);
- cout << "Ilosc elementow w liscie: " << count << endl;
- cout << "Menu listy: " << endl << "1. Dodaj na poczatek" << endl << "2. Dodaj na koniec" << endl << "3. Dodaj po danym elemencie" << endl << "4. Dodaj przed danym elementem" << endl << "5. Usun dowolny element" << endl << "Wybor: ";
- cin >> wybor;
- switch (wybor)
- {
- case 1:
- cout << "Podaj liczbe: ";
- cin >> liczba;
- ins_head(head, tail, liczba, count);
- break;
- case 2:
- cout << "Podaj liczbe: ";
- cin >> liczba;
- ins_tail(head, tail, liczba, count);
- break;
- case 3:
- cout << "Podaj numer elementu: ";
- cin >> numer;
- cout << "Podaj liczbe: ";
- cin >> liczba;
- for (int i = 1; i < numer; i++)
- p = p->next;
- ins_after(p, liczba, count);
- break;
- case 4:
- cout << "Podaj numer elementu: ";
- cin >> numer;
- cout << "Podaj liczbe: ";
- cin >> liczba;
- for (int i = 1; i < numer; i++)
- p = p->next;
- ins_before(p, liczba, count);
- break;
- case 5:
- cout << "Podaj numer elementu: ";
- cin >> numer;
- for (int i = 1; i < numer; i++)
- p = p->next;
- del(head, tail, p, count);
- break;
- }
- } while (wybor != 10);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement