Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- using namespace std;
- struct node{
- node * next;
- int value;
- };
- void DELL(node *& H){
- if(H!=NULL){
- node * temp=new node;
- temp = H;
- H=temp->next;
- delete temp;
- }
- }
- void ADD( node *& H, int val){
- node * temp = new node;
- temp->next=H;
- temp->value=val;
- H=temp;
- }
- void show(node * H){
- cout <<"H->";
- node *temp=H;
- while(temp!=NULL){
- cout << temp->value<<"->";
- temp=temp->next;
- }
- cout << "NULL"<<endl;
- }
- void usunpomax(node *&H)
- {
- int x=0;
- if(H!=NULL)
- {
- node *temp = new node;
- temp=H;
- while(temp!=NULL)
- {
- if(temp->value>x)
- {
- x=temp->value;
- temp=temp->next;
- }
- else
- temp=temp->next;
- }
- }
- if(H!=NULL)
- {
- node *tmp= new node;
- tmp=H;
- while(tmp!=NULL)
- {
- if(tmp->value==x)
- {
- tmp->next=tmp->next->next;
- break;
- }
- else
- tmp=tmp->next;
- }
- }
- }
- void przed(node *&H)
- {
- int x=0;
- if(H!=NULL)
- {
- node *temp= new node;
- temp=H;
- while(temp!=NULL)
- {
- if(temp->next->next==NULL)
- {
- x=temp->next->value;
- temp->next=temp->next->next;
- break;
- }
- else
- temp=temp->next;
- }
- }
- ADD(H,x);
- }
- /*void lacz(node *&H, node *&D)
- {
- int i=1;
- node *temp=new node;
- node *tmp=new node;
- temp=H;
- tmp=D;
- temp->next=tmp;
- while(temp!=NULL && tmp!=NULL)
- {
- temp->next=tmp;
- tmp->next=temp->next;
- }
- }*/
- void zamien(node *&H, int x)
- {
- if(H!=NULL)
- {
- node *temp = new node;
- temp=H;
- while(temp->next!=NULL)
- {
- if(x==temp->next->value)
- {
- if(temp->next->next==NULL)
- {
- cout<<"nastepnikiem jest NULL"<<endl;
- break;
- }
- node *tmp=new node;
- tmp=temp->next;
- node *tmp2=new node;
- tmp2=temp->next->next;
- tmp->next=tmp2->next;
- temp->next=tmp2;
- temp->next->next=tmp;
- break;
- }
- else
- temp=temp->next;
- }
- }
- }
- void zamprzed(node *&H, int x)
- {
- if(H!=NULL)
- {
- node *temp=new node;
- temp=H;
- while(temp->next!=NULL)
- {
- if(x==H->value)
- {
- cout<<"Nie ma poprzednika"<<endl;
- break;
- }
- if(x==temp->next->next->value)
- {
- node *tmp= new node;
- tmp=temp->next;
- node *tmp2=new node;
- tmp2=temp->next->next;
- tmp->next=tmp2->next;
- temp->next=tmp2;
- temp->next->next=tmp;
- break;
- }
- else
- temp=temp->next;
- }
- }
- }
- void kasn(node *&H, int x, int n)
- {
- if(H!=NULL)
- {
- node *temp=new node;
- temp=H;
- while(temp->next!=NULL)
- {
- if(temp->value==x)
- break;
- else
- temp=temp->next;
- }
- for(int i=0;i<n;i++)
- temp->next=temp->next->next;
- }
- }
- void odwroc(node *&H)
- {
- if(H!=NULL)
- {
- node *temp = H;
- node *tmp=NULL;
- while(temp->next!=NULL)
- {
- tmp=temp->next;
- temp->next=temp->next->next;
- tmp->next=H;
- H=tmp;
- }
- }
- }
- void co2(node *&H)
- {
- if(H!=NULL)
- {
- node *temp= new node;
- temp=H;
- H=H->next;
- delete temp;
- temp=H;
- while(temp->next!=NULL )
- {
- if(temp->next!=NULL && temp->next->next==NULL)
- {
- temp->next=NULL;
- break;
- }
- temp->next=temp->next->next;
- temp=temp->next;
- }
- }
- }
- void parz(node *&H)
- {
- node *tmp = H;
- node *wsk1 = H;
- node *wsk2 = H;
- while (H->next != NULL && H->value % 2 == 0)
- {
- H = H->next;
- delete tmp;
- tmp = H;
- }
- wsk2 = H;
- wsk1 = H;
- while (tmp->next != NULL)
- {
- if (tmp->value % 2 == 0)
- {
- wsk1 = tmp;
- tmp = tmp->next;
- wsk2->next = tmp;
- delete wsk1;
- }
- else
- {
- wsk2 = tmp;
- tmp = tmp->next;
- }
- }
- }
- //ZADANIE NR1
- void srednia(node*&H)
- {
- if (H != NULL)
- {
- node *temp = H;
- node *tmp = NULL;
- node *tmp2 = NULL;
- float x = 0, i = 0;
- float srednia = 0;
- while (temp != NULL)
- {
- if (temp->next == NULL)
- tmp = temp;
- x = x + temp->value;
- i++;
- temp = temp->next;
- }
- int z = 0;
- srednia = x / i;
- delete temp;
- temp = H;
- cout<<"srednia: " << srednia << endl;
- while(z < i)
- {
- z++;
- if(H->value > srednia)
- {
- tmp->next=temp;
- tmp=tmp->next;
- H=H->next;
- temp=H;
- }
- else
- {
- if(temp->value > srednia)
- {
- tmp->next=temp;
- tmp=tmp->next;
- temp=tmp2;
- temp->next=temp->next->next;
- temp=temp->next;
- }
- else
- {
- tmp2=temp;
- temp=temp->next;
- }
- }
- }
- tmp->next=NULL;
- }
- }
- void mnoz(node*&H)
- {
- if(H!=NULL)
- {
- node *temp=H;
- node *tmp=NULL;
- while(temp!=NULL)
- {
- for(int i=1;i<temp->value;i++)
- {
- tmp=new node;
- tmp->value=temp->value;
- tmp->next=temp->next;
- temp->next=tmp;
- temp=temp->next;
- }
- temp=temp->next;
- }
- }
- }
- void zderzaki(node*&H, int x, int y)
- {
- if(H!=NULL)
- {
- node *temp=H;
- node *tmp=NULL;
- node *tmp2=NULL;
- node *tmp3=NULL;
- int licz=0;
- while(temp->value!=x)
- {
- temp=temp->next;
- tmp3=temp;
- }
- while(temp->value!=y)
- {
- licz++;
- temp=temp->next;
- }
- for(int i=2;i<licz;licz--)
- {
- temp=tmp3;
- while(i<licz)
- {
- i++;
- tmp=temp->next->next;
- tmp2=temp->next;
- tmp2->next=tmp->next;
- tmp->next=tmp2;
- temp->next=tmp;
- temp=temp->next;
- }
- i=2;
- }
- }
- }
- void copy(node *&H, node *&D)
- {
- if(H!=NULL)
- {
- node *tmp = H;
- node *tmp2 = D;
- while(tmp->next !=NULL)
- tmp=tmp->next;
- tmp->next=tmp2;
- }
- }
- void bubblesort(node*&H)
- {
- if(H!=NULL)
- {
- int i=0;
- node *temp=H, *tmp=NULL, *tmp2=NULL, *tmp3=NULL;
- while(temp->next->next!=NULL)
- {
- i=0;
- if(H->value>H->next->value)
- {
- i++;
- tmp=H;
- tmp2=H->next;
- tmp3=H->next->next;
- tmp->next=tmp3;
- tmp2->next=tmp;
- H=tmp2;
- temp=H;
- }
- while(temp->next->next!=NULL)
- {
- if(temp->next->value>temp->next->next->value)
- {
- i++;
- tmp=temp->next;
- tmp2=temp->next->next;
- tmp3=temp->next->next->next;
- tmp->next=tmp3;
- tmp2->next=tmp;
- temp->next=tmp2;
- temp=temp->next;
- }
- else
- temp=temp->next;
- }
- if(i!=0)
- temp=H;
- }
- }
- }
- int main()
- {
- node * H = NULL;
- ADD(H, 13);
- ADD(H, 8);
- ADD(H, 5);
- ADD(H,14);
- ADD(H,2);
- ADD(H,12);
- ADD(H,22);
- ADD(H,1);
- ADD(H,11);
- show(H);
- //srednia(H);
- //show(H);
- //mnoz(H);
- //show(H);
- //odwroc(H);
- //zderzaki(H, 22, 8);
- copy(H, H);
- show(H);
- system("pause");
- }
- void sort(node *&H, node**&tab)
- {
- if (H != NULL)
- {
- node *temp = H;
- for (int i = 0; i < N / 100; i++)
- {
- while (temp->next != NULL)
- {
- if (temp->value <= (i + 1) * 100 && temp->value>i*100)
- {
- ADD(tab[i], temp->value);
- temp = temp->next;
- }
- else
- temp = temp->next;
- }
- temp = H;
- }
- }
- }
- void bubblesort(node*&H)
- {
- if (H != NULL)
- {
- int i = 0;
- node *temp = H, *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL;
- while (temp->next->next != NULL)
- {
- i = 0;
- if (H->value>H->next->value)
- {
- i++;
- tmp = H;
- tmp2 = H->next;
- tmp3 = H->next->next;
- tmp->next = tmp3;
- tmp2->next = tmp;
- H = tmp2;
- temp = H;
- }
- while (temp->next->next != NULL)
- {
- if (temp->next->value>temp->next->next->value)
- {
- i++;
- tmp = temp->next;
- tmp2 = temp->next->next;
- tmp3 = temp->next->next->next;
- tmp->next = tmp3;
- tmp2->next = tmp;
- temp->next = tmp2;
- temp = temp->next;
- }
- else
- temp = temp->next;
- }
- if (i != 0)
- temp = H;
- }
- }
- }
- void lacz(node *&p, node **&tab)
- {
- node* temp = NULL;
- temp = tab[0];
- for (int i = 0; i < N / 100; i++)
- {
- while (temp->next != NULL)
- {
- temp = temp->next;
- }
- temp->next = tab[i + 1];
- if (i == (N / 100) - 1)
- temp->next = NULL;
- }
- p = tab[0];
- }
- int main()
- {
- clock_t s, f;
- double czas=0.0, czas2=0.0;
- node *p = NULL;
- int x = N / 100;
- node **tab = new node*[x];
- for (int i = 0; i < x; i++)
- {
- tab[i] = NULL;
- }
- srand(time(NULL));
- node * H = NULL, *H2=NULL;
- for (int i = 0; i < N; i++)
- {
- ADD(H, rand() % 10000);
- ADD(H2, rand() % 10000);
- }
- //show(H);
- sort(H, tab);
- s = clock();
- for (int i = 0; i < x; i++)
- {
- bubblesort(tab[i]);
- //cout << "DB[" << i << "]->";
- //show(tab[i]);
- }
- lacz(p, tab);
- f = clock();
- czas += (double)(f - s) / CLOCKS_PER_SEC;
- s = clock();
- bubblesort(H2);
- f = clock();
- czas2 += (double)(f - s) / CLOCKS_PER_SEC;
- //show(p);
- cout << czas << endl;
- cout << czas2 << endl;
- system("pause");
- }
Add Comment
Please, Sign In to add comment