Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ALGO2 IS1 210A LAB01
- //Grzegorz Cendrowski
- //cg44284@zut.edu.pl
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctime>
- #include <time.h>
- 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 pokaz(list* lista, int n);
- void wyszukanie(list* lista, int n);
- void ostatnie(list* lista, int n);
- void podlicz(list* lista);
- void usunjeden(list* lista, int n);
- void usunwszystko(list* lista);
- int main()
- {
- srand(time(NULL));
- int n = 60;
- list* lista = new list;
- stworz(lista, 1);
- wstaw(lista, n);
- pokaz(lista, 20);
- wyszukanie(lista, 1);
- podlicz(lista);
- stworz(lista, 99999);
- ostatnie(lista, 20);
- usunjeden(lista, 1);
- usunwszystko(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 << "Error: Node z kluczem o wartosci n juz istnieje." << endl;
- return;
- }
- while (poprzedni->next != NULL)
- {
- if (poprzedni->next->key == temp->key)
- {
- cout << "Error: Node z kluczem o wartosci n juz istnieje." << 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;
- for (int i = 0; i < n; i++)
- {
- tempo = rand() % 100098 - 99;
- stworz(lista, tempo);
- }
- }
- void pokaz(list* lista, int n)
- {
- node *temp = new node;
- temp = lista->head;
- for (int a = 0; a < n; a++)
- {
- 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 o podanej wartosci kluczowej to: " << temp << endl;
- czywyszukana = 1;
- }
- temp = temp->next;
- }
- if (czywyszukana == 0)
- cout << "Error: Nie znaleziono podanej wartosci kluczowej." << endl;
- }
- void ostatnie(list* lista, int n)
- {
- node *temp = new node;
- temp = lista->head;
- int suma = 0, cur = 0;
- while (temp != NULL)
- {
- suma++;
- temp = temp->next;
- }
- suma = suma - n;
- temp = lista->head;
- while (temp != NULL)
- {
- if (suma <= cur)
- cout << temp->key << endl;
- temp = temp->next;
- cur++;
- }
- }
- void podlicz(list* lista)
- {
- node *temp = new node;
- temp = lista->head;
- int n = 0;
- while (temp != NULL)
- {
- n++;
- temp = temp->next;
- }
- cout << "Jest " << n << " node'ow" << endl;
- }
- void usunjeden(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;
- }
- void usunwszystko(list* lista)
- {
- node *temp = lista->head;
- while (lista->head != NULL)
- {
- usunjeden(lista, lista->head->key);
- }
- lista->head = NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement