• Tidak ada hasil yang ditemukan

Building a Sorted Linked List

Dalam dokumen Pelajari tentang Advanced topics in Java (Halaman 85-88)

Linked Lists

3.7 Building a Sorted Linked List

As a third possibility, suppose we want to build the list so that the numbers are always sorted in ascending order.

Suppose the incoming numbers are as follows (0 terminates the data):

36 15 52 23 0

We would like to build the following linked list:

top

23 52 15

top

curr prev

23 52 15 36

top

curr prev

23 52 15 36

top

15 23 36 52

When a new number is read, it is inserted in the existing list (which is initially empty) in its proper place. The first number is simply added to the empty list.

Each subsequent number is compared with the numbers in the existing list. As long as the new number is greater than a number in the list, we move down the list until the new number is smaller than, or equal to, an existing number or we come to the end of the list.

To facilitate the insertion of the new number, before we leave a node and move on to the next one, we must save the pointer to it in case the new number must be inserted after this node. However, we can know this only when we compare the new number with the number in the next node.

To illustrate these ideas, consider the following sorted list, and suppose we want to add a new number (30, say) to the list so that it remains sorted:

Assume the number above a node is the address of the node. Thus, the value of top is 400.

First, we compare 30 with 15. It is bigger, so we move on to the next number, 23, remembering the address (400) of 15.

Next, we compare 30 with 23. It is bigger, so we move on to the next number, 36, remembering the address (200) of 23. We no longer need to remember the address (400) of 15.

Next, we compare 30 with 36. It is smaller, so we have found the number before which we must insert 30. This is the same as inserting 30 after 23. Since we have remembered the address of 23, we can now perform the insertion.

We will use the following code to process the new number, n:

prev = null;

curr = top;

while (curr != null && n > curr.num) { prev = curr;

curr = curr.next;

}

Initially, prev is null and curr is 400. The insertion of 30 proceeds as follows:

•฀ 30 is compared with curr.num, 15. It is bigger, so we set prev to curr (400) and set curr to curr.next, 200; curr is not null.

•฀ 30 is compared with curr.num, 23. It is bigger, so we set prev to curr (200) and set curr to curr.next, 800; curr is not null.

•฀ 30 is compared with curr.num, 36. It is smaller, so we exit the while loop with prev being 200 and curr being 800.

We have the following situation:

If the new number is stored in a node pointed to by np, we can now add it to the list (except at the head;

see the next section) with the following code:

600 800

200 400

top

15 23 36 52

prev curr

600 800

200 400

top

15 23 36 52

np.next = curr; //we could also use prev.next for curr prev.next = np;

This will change the following:

to this:

As an exercise, verify that this code will work if the number to be added is bigger than all the numbers in the list.

Hint: when will the while loop exit?

If the number to be added is smaller than all the numbers in the list, it must be added at the head of the list and becomes the new first node in the list. This means that the value of top has to be changed to the new node.

The while loop shown earlier will work in this case as well. The while condition will be false on the very first test (since n will be smaller than curr.num). On exit, we simply test whether prev is still null; if it is, the new node must be inserted at the top of the list.

If the list were initially empty, the while loop will exit immediately (since curr will be null). In this case also, the new node must be inserted at the top of the list, becoming the only node in the list.

Program P3.3 contains all the details. The insertion of a new node in its proper position in the list is delegated to the function addInPlace. This function returns a pointer to the top of the modified list.

Program P3.3

import java.util.*;

public class BuildList3 {

public static void main(String[] args) { Scanner in = new Scanner(System.in);

Node top, np, last = null;

top = null;

System.out.printf("Enter some integers ending with 0\n");

int n = in.nextInt();

while (n != 0) {

top = addInPlace(top, n);

n = in.nextInt();

}

printList(top);

} //end main

public static Node addInPlace(Node top, int n) {

// This functions inserts n in its ordered position in a (possibly empty) // list pointed to by top, and returns a pointer to the new list

np

prev curr

top

15 23 36 52

30

curr np

prev

52 top

15 23 30 36

Node np, curr, prev;

np = new Node(n);

prev = null;

curr = top;

while (curr != null && n > curr.num) { prev = curr;

curr = curr.next;

}

np.next = curr;

if (prev == null) return np; //top of list is now the new node prev.next = np;

return top; //the top of the list has not changed } //end addInPlace

public static void printList(Node top) {

while (top != null) { //as long as there's a node System.out.printf("%d ", top.num);

top = top.next; //go on to the next node }

System.out.printf("\n");

} //end printList } //end class BuildList3 class Node {

int num;

Node next;

public Node(int n) { num = n;

next = null;

}

} //end class Node

When run, Program P3.3 builds a sorted linked list from the numbers provided and then prints the numbers in the order that they appear in the list. The following shows some sample output:

Enter some integers ending with 0 9 1 8 2 7 3 6 4 5 0

1 2 3 4 5 6 7 8 9

Dalam dokumen Pelajari tentang Advanced topics in Java (Halaman 85-88)