Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctime>
- using namespace std;
- struct node
- {
- int key;
- double a;
- char b;
- node *next;
- node()
- {
- key = NULL;
- }
- };
- class list
- {
- public:
- node* head, *tail;
- list()
- {
- head = NULL;
- tail = NULL;
- }
- };
- //funkcje
- void stworz(list* lista, int n);
- void wstaw(list* lista, int n);
- void display(list* lista);
- void wyszukanie(list* lista, int n);
- void usun(list* lista, int n);
- int main()
- {
- int n = 20;
- list* lista = new list;
- stworz(lista, 1);
- wstaw(lista, n);
- display(lista);
- wyszukanie(lista, 1);
- usun(lista, 1);
- display(lista);
- system("pause");
- }
- void stworz(list* lista, int n)
- {
- node* temp = new node;
- node* poprzedni = lista->head;
- temp->a = rand() % 10000;
- temp->b = 'T';
- temp->next = NULL;
- temp->key = n;
- if (lista->head == NULL)
- {
- lista->head = temp;
- lista->tail = temp;
- temp = NULL;
- }
- else
- {
- if (lista->head->key == temp->key)
- {
- cout << "Node with function argument n already exists" << endl;
- return;
- }
- while(poprzedni->next != NULL)
- {
- if(poprzedni->next->key == temp->key)
- {
- cout << "Node with function argument n already exists" << endl;
- return;
- }
- if(poprzedni->next->key > temp->key)
- {
- break;
- }
- poprzedni = poprzedni->next;
- }
- temp->key = n;
- temp->next = poprzedni->next;
- poprzedni->next = temp;
- }
- }
- void wstaw(list* lista, int n)
- {
- node *check = new node;
- node *poprzedni = lista->head;
- check = lista->head;
- int tempo;
- srand(std::time(0));
- for (int i = 0; i < n; i++)
- {
- tempo = rand() % 100098 - 99;
- stworz(lista, tempo);
- }
- }
- void display(list* lista)
- {
- node *temp = new node;
- temp = lista->head;
- while (temp != NULL)
- {
- cout << temp->key << endl;
- temp = temp->next;
- }
- }
- void wyszukanie(list* lista, int n)
- {
- node *temp = new node;
- int czywyszukana = 0;
- temp = lista->head;
- while (temp != NULL)
- {
- if (temp->key == n)
- {
- cout << "Adres podanej wartosci kluczowej to: " << temp << endl;
- czywyszukana = 1;
- }
- temp = temp->next;
- }
- if (czywyszukana == 0)
- cout << "Nie znaleziono podanej wartosci kluczowej." << endl;
- }
- void usun(list* lista, int n)
- {
- node *temp = new node;
- node *poprzedni = new node;
- int czyusunieto = 0;
- temp = lista->head;
- while (temp != NULL)
- {
- poprzedni = temp;
- if (temp->key == n)
- {
- cout << "Usunieto node o adresie:" << temp << endl;
- delete temp;
- czyusunieto = 1;
- break;
- }
- temp = temp->next;
- lista->head = poprzedni;
- poprzedni->next = NULL;
- }
- if (czyusunieto == 0)
- cout << "Nie usunieto node'a o podanej wartosci kluczowej gdyz nie zostal on znaleziony." << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement