How do I output the other number in this Linked List?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<stdio.h>
#include<stdlib.h>
#include <iostream>

using namespace std;

struct node
{
    int data;
    struct node* next;
};

struct node* SortedMerge(struct node* a, struct node* b);
void Partition(struct node* source, struct node** frontRef, struct node** backRef);

void MergeSort(struct node** headRef){
	struct node* head = *headRef;
	struct node* a;
	struct node* b;
	
	if ((head == NULL) || (head->next == NULL)){
		return;
	}
 
	Partition(head, &a, &b); 
 
	MergeSort(&a);
	MergeSort(&b);
 
	*headRef = SortedMerge(a, b);
}

struct node* SortedMerge(struct node* a, struct node* b){
	struct node* result = NULL;
 
	if (a == NULL)
		return(b);
	else if (b==NULL)
		return(a);
 
	if (a->data <= b->data){
    	result = a;
    	result->next = SortedMerge(a->next, b);
	}
	else{
    	result = b;
    	result->next = SortedMerge(a, b->next);
	}
	return(result);
}

void Partition(struct node* source, struct node** frontRef, struct node** backRef){
	struct node* fast;
	struct node* slow;
	if (source==NULL || source->next==NULL){
		*frontRef = source;
    	*backRef = NULL;
	}
	else{
		slow = source;
		fast = source->next;
		while (fast != NULL){
    		fast = fast->next;
    		if (fast != NULL){
    			slow = slow->next;
        		fast = fast->next;
    		}
    	}
 		*frontRef = source;
   		*backRef = slow->next;
		slow->next = NULL;
	}
}

void printList(struct node *node){
	while(node!=NULL){
		printf("%d ", node->data);
		node = node->next;
	}
}
 
void push(struct node** head_ref, double new_data, double new_why){
	struct node* new_node = (struct node*) malloc(sizeof(struct node));
  	new_node->data  = new_data;
  	new_node->next = (*head_ref);    
	(*head_ref) = new_node;
	
} 

int main(){
	struct node* res = NULL;
	struct node* head = NULL;

	push(&head, 186, 15.0);
	push(&head, 699, 69.9);
	push(&head, 132, 6.5);
	push(&head, 272, 22.4);
	push(&head, 291, 28.4);
	push(&head, 331, 65.9);
	push(&head, 199, 19.4);
	push(&head, 1890, 198.7);
	push(&head, 788, 38.8);
	push(&head, 1601, 139.2);

	MergeSort(&head);
  
	cout << "Sorted Linked List is: " << endl;
	printList(head);          
  
	getchar();
	return 0;
}
Last edited on
Topic archived. No new replies allowed.