typedef struct Node{
int data;
struct Node* next;
}Node;
Node* initNode(){
Node* newN = malloc(sizeof(Node));
newN->data = rand() % (1000 -0);
newN->next = NULL;
}
void addNodes(int amount, Node* first){
Node* cur = first;
while(cur->next != NULL){
cur = cur->next;
}
for(int i = 0; i <= amount; i++){
cur->next = initNode();
cur = cur->next;
}
}
void printNodes(Node* first){
Node* cur = first;
int loopC = 0;
while(cur->next != NULL){
printf("%d-", cur->data);
loopC++;
cur = cur->next;
}
}
beginning of areas causing problem I think that it is something wrong with the sort or swap functions causing the shortening of the linked list, not sure though.
void swap(Node* prev, Node* cur, Node* first){
Node* next = cur->next;
if(next==NULL){return;}
Node* nextO = next->next;
cur->next = nextO;
if(prev != NULL){prev->next = next;}
else{first = next;}
next->next = cur;
}
void bubbleSort(Node* first){
int switchC = -1;
while(switchC != 0){
switchC = 0;
Node* cur = first;
Node* prev = NULL;
Node* next = NULL;
while(cur->next != NULL){
next = cur->next;
if(next->data <= cur->data){
swap(prev, cur, first);
switchC++;
}
prev = cur;
cur = next;
}
}
}
end of areas causing problem
void main(){
Node* first = initNode();
addNodes(10, first);
printNodes(first);
bubbleSort(first);
printNodes(first);
}
inputed linked list: Node 0: value:383-Node 1: value:886-Node 2: value:777-Node 3: value:915-Node 4: value:793-Node 5: value:335-Node 6: value:386-Node 7: value:492-Node 8: value:649-Node 9: value:421-Node 10: value:362
Outputed Linked List: Node 0: value:383-Node 1: value:386-Node 2: value:421-Node 3: value:492-Node 4: value:649-Node 5: value:777-Node 6: value:793-Node 7: value:886