Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct lista{
- int wartosc;
- struct lista *pref;
- struct lista *next;
- } lista;
- lista * head;
- void addElement (lista * pierwszyelement, int wartoscc){
- lista *current, *newElement;
- current = pierwszyelement;
- while(current-> next != NULL){
- current=current-> next;
- };
- newElement = malloc(sizeof(struct lista));
- newElement-> wartosc = wartoscc;
- newElement-> pref = current;
- newElement->next = current-> next;
- current-> next = newElement;
- }
- void view (lista * pierwszyelement){
- lista *current;
- current = pierwszyelement;
- while(current -> pref!= NULL){
- current=current -> pref;
- };
- while(current != NULL){
- printf("\n wartosc %d", current -> wartosc);
- printf("\n next %d", current -> next);
- printf("\n pref %d \n \n", current -> pref);
- current=current -> next;
- };
- }
- void sortChangeWartosc (lista * head){
- lista *current, *prev;
- current = head;
- while (current != NULL){
- printf("\n current current: %d \n \n", current->wartosc);
- prev = current ;
- int saveNextCurrent = current -> next;
- while (prev != NULL){
- printf("sprawdzenie pref prev -> pref = %d \n", prev-> wartosc);
- // prev = current ->pref ;
- if (current->wartosc < prev -> wartosc){
- printf("zamiana cur %d < pref %d \n", current->wartosc,prev -> wartosc);
- int warCurrent = current->wartosc;
- int warbefore = prev -> wartosc;
- prev -> wartosc = warCurrent;
- current -> wartosc = warbefore;
- current = prev;
- }
- //current ->pref = prev ->pref;
- // prev ->next = current ->next;
- // prev ->pref = current ;
- // current ->next = prev;
- prev = prev -> pref;
- };
- //tablica[j+1] = saveArray;
- current = saveNextCurrent;
- };
- }
- void sortChangeElement (lista * head){
- lista *current, *prev, *beforePrev, *currentNext;
- current = head;
- while (current != NULL){
- printf("\n current %d \n \n", current->wartosc);
- prev = current->pref;
- int saveNextCurrent = current -> next;
- while (prev != NULL){
- printf(" prev -> pref = %d \n", prev-> wartosc);
- // prev = current ->pref ;
- if (current->wartosc < prev -> wartosc){
- printf("zamiana current %d na prev %d \n", current->wartosc,prev -> wartosc);
- if (prev -> pref != NULL){
- beforePrev = prev -> pref;
- beforePrev -> next = current;
- };
- if (current -> next != NULL){
- currentNext = current -> next;
- currentNext -> pref = prev;
- };
- current -> pref = prev -> pref;
- prev -> next = current -> next;
- prev -> pref = current;
- current -> next = prev;
- }
- prev = prev -> pref;
- };
- //tablica[j+1] = saveArray;
- current = saveNextCurrent;
- };
- }
- int main()
- {
- head = malloc(sizeof(struct lista));
- head -> wartosc = 7 ;
- head -> pref = NULL;
- head -> next = NULL;
- addElement (head, 5);
- addElement (head, 4);
- addElement (head, 6);
- addElement (head, 22);
- addElement (head, 55);
- addElement (head, 1);
- addElement (head, 3);
- addElement (head, 2);
- addElement (head, 62);
- addElement (head, 9);
- view(head);
- //sortChangeWartosc(head);
- sortChangeElement(head);
- view(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement