How to sort the whole linked list using merge sort?

Hi everyone. I met some problems when I want to sort the linked list using merger sort.My linkedlist contains 4 parts which are data,type,address,next. I want to sort the list order by the data(property number). What is the solution?
The sorting will work when i'm just sort data ONLY, if want to sort the whole linked list then it will no output at all, just stack on there.
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
void u::sort()
{
	  struct node* res = NULL;
  struct node* a = NULL;
	 ifstream infile;                     //read file
	infile.open("realestate.txt");

	if(infile.fail())                    //if the file doesn't exist
	{
		cout<<"Error"<<endl;
		exit(1);
	}
	node* tt= new node;
	int i=0;
	string zz;
	while(!infile.eof())
	{
		while(infile>>tt->data>>tt->type>>tt->add)
		{
			push(&a, tt->data,tt->type,tt->add);
			cout<<tt->data;
			tt->next=start;
			start=tt;
		}
	}
	infile.close();
	  /* Start with the empty list */


  
  /* Sort the above created Linked List */
	MergeSort(&a);
  
  printf("\n Sorted Linked List is: \n");
  printList(a);           
  
  getchar();
}

void u::push(struct node** head_ref, int new_data,int v,string z)
{
  /* allocate node */
  struct node* new_node =
            (struct node*) malloc(sizeof(struct node));
  
  /* put in the data  */
  new_node->data  = new_data;
  new_node->type = v;
  new_node->add = z;
  /* link the old list off the new node */
  new_node->next = (*head_ref);    
  
  /* move the head to point to the new node */
  (*head_ref)    = new_node;
} 

void u::printList(struct node *node)
{
  while(node!=NULL)
  {
	  cout<<node->data<<"\n";
	  node = node->next;
  }
}

void u::FrontBackSplit(struct node* source,struct node** frontRef, struct node** backRef)
{
  struct node* fast;
  struct node* slow;
  if (source==NULL || source->next==NULL)
  {
    /* length < 2 cases */
    *frontRef = source;
    *backRef = NULL;
  }
  else
  {
    slow = source;
    fast = source->next;
 
    /* Advance 'fast' two nodes, and advance 'slow' one node */
    while (fast != NULL)
    {
      fast = fast->next;
      if (fast != NULL)
      {
        slow = slow->next;
        fast = fast->next;
      }
    }
 
    /* 'slow' is before the midpoint in the list, so split it in two
      at that point. */
    *frontRef = source;
    *backRef = slow->next;
    slow->next = NULL;
  }
}

struct node* u::SortedMerge(struct node* a, struct node* b)
{
  struct node* result = NULL;
 
  /* Base cases */
  if (a == NULL)
     return(b);
  else if (b==NULL)
     return(a);
 
  /* Pick either a or b, and recur */
  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 u::MergeSort(struct node** headRef)
{
  struct node* head = *headRef;
  struct node* a;
  struct node* b;
 
  /* Base case -- length 0 or 1 */
  if ((head == NULL) || (head->next == NULL))
  {
    return;
  }
 
  /* Split head into 'a' and 'b' sublists */
  FrontBackSplit(head, &a, &b); 
 
  /* Recursively sort the sublists */
  MergeSort(&a);
  MergeSort(&b);
 
  /* answer = merge the two sorted lists together */
  *headRef = SortedMerge(a, b);
}
Last edited on
closed account (48bpfSEw)
Please give us the hole runable code with formation and detailed information where the bug is or should be.

If I get in a realy big confusion the best way is for me to throw the code away and start again!

Here are some help to sort linked lists with pointers:

http://stackoverflow.com/questions/19579587/sorting-linked-list-c-with-pointers

and a lot more help if needed:

https://www.google.de/search?q=cpp+sort+linked+list&ie=utf-8&oe=utf-8&client=firefox-b&gfe_rd=cr&ei=4ilJV7aUEorZ8AeH5IKIAQ

Last edited on
Topic archived. No new replies allowed.