Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- class node
- {
- public:
- int key;
- double d_variable;
- char ch_variable;
- node* next;
- };
- class my_list
- {
- public:
- node * head;
- int size;
- my_list()
- {
- head = NULL;
- size = 0;
- }
- void AddOneElement(int value) //adding one element with value provided by user
- {
- node* temp1,* temp2;
- node** temp3;
- temp3 = &head;
- temp1 = new node;
- temp1->key = value;
- temp1->d_variable = rand();
- temp1->ch_variable = 'T';
- temp2 = head;
- while (temp2 != NULL && temp2->key <= temp1->key)
- {
- if (temp2->key == temp1->key)
- return;
- temp3 = &temp2->next;
- temp2 = temp2->next;
- }
- *temp3 = temp1;
- temp1->next = temp2;
- size++;
- }
- void AddManyElements(int amount) //adding many elements with random key value
- {
- for (int i = 0; i < amount; i++)
- {
- node* temp1,* temp2;
- node** temp3;
- temp3 = &head;
- temp1 = new node;
- temp1->key = ( 4 *rand()) % 99901 + 99;
- temp1->d_variable = rand();
- temp1->ch_variable = 'T';
- temp2 = head;
- while (temp2 != NULL && temp2->key <= temp1->key)
- {
- if (temp2->key == temp1->key)
- {
- temp1->key = (4 * rand()) % 99901 + 99;
- temp2 = head;
- }
- else
- {
- temp3 = &temp2->next;
- temp2 = temp2->next;
- }
- }
- *temp3 = temp1;
- temp1->next = temp2;
- size++;
- }
- }
- /*void ShowElements() //function that shows all elements, thats for me for testing purposes
- {
- node* temp;
- temp = head;
- int i = 0;
- if(temp!=NULL)
- {
- while (temp!=NULL)
- {
- cout << "Key number "<<i+1<<" :" << temp->key << endl;
- temp = temp->next;
- i++;
- }
- }
- else //case in which list is empty
- {
- cout << endl << "This list is empty!" << endl;
- }
- }*/
- void ListSize()
- {
- cout << endl << "Size:" << size<<endl;
- }
- void DeleteElement(int value)
- {
- node *temp1, *temp2,*temp3;
- temp1 = new node;
- temp3 = new node;
- temp1->key = value;
- temp2 = head;
- if (temp2 == NULL) //list is empty
- {
- cout << "There is no list" << endl;
- return;
- }
- if (temp2 != NULL && temp2->key == temp1->key) //element is on the first place
- {
- temp3 = temp2->next;
- delete temp2;
- head = temp3;
- size--;
- return;
- }
- while (temp2 != NULL && temp2->key != temp1->key) //searching for the element through list
- {
- temp3 = temp2;
- temp2 = temp2->next;
- }
- if (temp2 == NULL)return; //there is no element
- temp3->next = temp2->next; //element has been found
- delete temp2;
- size--;
- }
- void DeleteList()
- {
- node* temp1;
- temp1 = head;
- if (temp1 == NULL)return;
- else
- {
- while (temp1 != NULL)
- {
- node* temp2;
- temp2 = temp1;
- temp1 = temp1->next;
- delete temp2;
- size--;
- }
- head = temp1;
- }
- }
- void ShowFirstElements(int value)
- {
- if (value > size)return;
- else
- {
- node* temp;
- temp = head;
- if (temp == NULL)return;
- else
- {
- for (int i = 0; i < value; i++)
- {
- cout << "Key:" << temp->key << endl;
- temp = temp->next;
- }
- }
- }
- }
- void ShowLastElements(int value)
- {
- node* temp;
- temp = head;
- if (head == NULL)return;
- if (value > size)return;
- int j = size - value;
- for (int i = 0; i <j; i++)
- {
- temp = temp->next;
- }
- while (temp != NULL)
- {
- cout << endl << "Key:" << temp->key;
- temp = temp->next;
- }
- }
- void FindKey(int value)
- {
- node* temp;
- temp = head;
- if (temp == NULL)
- {
- cout << endl << "List is empty" << endl;
- }
- else
- {
- while (temp != NULL && temp->key != value)
- {
- temp = temp->next;
- }
- if (temp == NULL)
- {
- cout << endl << "Element does not exist" << endl;
- }
- else
- {
- cout << endl << "Element does exist";
- cout<< endl << "key:" << temp->key;
- cout << endl << "d_variable:" << temp->d_variable;
- cout << endl << "ch_variable:" << temp->ch_variable;
- }
- }
- }
- };
- int main()
- {
- clock_t begin, end;
- double time_spent;
- begin = clock();
- ifstream my_file;
- int amount=0, key1=0, key2=0, key3=0, key4=0, key5=0;
- my_list testing;
- srand(time(NULL));
- my_file.open("inlab02.txt");
- if (my_file.good())
- {
- cout << "File opened succesfully, loading variables..." << endl;
- my_file >> amount;
- my_file >> key1;
- my_file >> key2;
- my_file >> key3;
- my_file >> key4;
- my_file >> key5;
- }
- else
- {
- cout << endl << "Could not open the file, ending program..." << endl;
- }
- testing.FindKey(key1);
- testing.AddManyElements(amount);
- testing.ListSize();
- testing.ShowFirstElements(20);
- cout << endl;
- testing.AddOneElement(key2);
- cout << endl;
- testing.ShowFirstElements(20);
- testing.AddOneElement(key3);
- cout << endl;
- testing.ShowFirstElements(20);
- testing.AddOneElement(key4);
- cout << endl;
- testing.ShowFirstElements(20);
- testing.AddOneElement(key5);
- testing.DeleteElement(key3);
- cout << endl;
- testing.ShowFirstElements(20);
- testing.DeleteElement(key2);
- cout << endl;
- testing.ShowFirstElements(20);
- testing.DeleteElement(key5);
- testing.ListSize();
- testing.FindKey(key5);
- cout << endl;
- testing.ShowLastElements(11);
- testing.DeleteList();
- testing.ShowLastElements(11);
- testing.ListSize();
- my_file.close();
- end = clock();
- time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- cout << endl << "Execution time:" << time_spent;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement