Find the maximum value stored in a linked list

Problem description:

  • A Node in a linked list has the following structure:

      struct Node
      {             
         int  value;
         struct Node *next;
      } 

  • Assumed that:

    • A linked list has been created and the list has ≥ 1 node

    • The variable head points to the first list element (= Node) of the list

  • Problem:

    • Find the maximum of the value field in the linked list that starts at the variable head.

Simplication:   we will assume that the linked list is not empty (i.e.: head ≠ null)

Find the maximum value stored in a linked list
 

Algorithm to find the maximum of a series of values:

   max = first value in the serie

   for nextValue = 2nd, 3rd, ...., last value in serie do 
   {
      if ( nextValue > max )
         max = nextValue;
   }
   

 

This algorithm will find the maximum value in any data structure

The only adaptation that you need to make is: use a data structure specific access method to access the value field.

Find the maximum value stored in a linked list
 

Find maximum algorithm for a linked list (adjust how to find the value);

    // Assume we have a NON-empty list at head !!!

    int  max;
    List ptr;    

    max = head->value;    // first value in linked list

    ptr = head->next;     // Prepare ptr to traverse the list 

    while ( ptr != NULL )
    {
       if ( ptr->value > max )                  
          max = ptr->value;

       ptr = ptr->next;
    }
   

(Draw the flow chart)

Find the maximum value stored in a linked list

Flow diagram of the find maximum program: (click to pull out)

Find the maximum value stored in a linked list
 

How I created a non-empty linked list in assembler:

head:   .4byte  p0 
      // head contains the address of the first list elem 
      // I created: head->[34]->[-9]->[99]->[78]->[55]

// list structure is: [value (int), next (link)]
p0: .4byte  34, p3      // p0 contains [34, p3]
p1: .4byte  99, p2      // p1 contains [99, p2]
p2: .4byte  78, p4      // p2 contains [78, p4]
p3: .4byte  -9, p1      // p3 contains [-9, p1]
p4: .4byte  55, 0       // p4 contains [55, null]
   

Find the maximum value stored in a linked list

main:
        // max = head.value;  - always execute the RHS first !!!
        movw    r0, #:lower16:head
        movt    r0, #:upper16:head
        ldr     r0,[r0]                 // r0 = head
        ldr     r0,[r0, #0]             // r0 = head.value
        movw    r1, #:lower16:max
        movt    r1, #:upper16:max
        str     r0,[r1]

        // ptr = head.next 
        movw    r0, #:lower16:head
        movt    r0, #:upper16:head
        ldr     r0,[r0]                 // r0 = head
        ldr     r0,[r0, #4]             // r0 = head.next
        movw    r1, #:lower16:ptr
        movt    r1, #:upper16:ptr
        str     r0,[r1]

while:  // START of while loop

        // Test while condition  ptr != null
        movw    r0, #:lower16:ptr
        movt    r0, #:upper16:ptr
        ldr     r0, [r0]                //r0 = ptr

        cmp     r0, #0
        beq     whileEnd                // If ptr == null: exit while loop

        /* ***********************************
           While body
           *********************************** */
        // if ( ptr.value > max )

        //// Get ptr.value  (use base + offset access technique !)
        movw    r0, #:lower16:ptr
        movt    r0, #:upper16:ptr
        ldr     r0, [r0]
        ldr     r0, [r0, #0]    // r0 = ptr.value

        //// Get max
        movw    r1, #:lower16:max
        movt    r1, #:upper16:max  // r1 = addr(max)
        ldr     r1, [r1]

        //// Compare  ptr.value (= r0) < max (= r2)
        cmp     r0, r1
        ble     ifEnd


        /* ********************************************
           Then-part: max = ptr.value
           ******************************************** */
        movw    r0, #:lower16:ptr
        movt    r0, #:upper16:ptr
        ldr     r0, [r0]
        ldr     r0, [r0, #0]    // r0 = ptr.value

        movw    r1, #:lower16:max
        movt    r1, #:upper16:max
        str     r0, [r1]

ifEnd:
        // ptr = ptr.next
        movw    r0, #:lower16:ptr
        movt    r0, #:upper16:ptr   // r0 = addr(ptr)
        ldr     r1, [r0]

        ldr     r1, [r1, #4]    // r1 = ptr.next
        str     r1, [r0]


        // End of while body --- branch back to test !!
        b       while

whileEnd:


/* --------------------------------------------------
   Begin of the permanent program variables
   -------------------------------------------------- */
        .data

ptr:    .skip   4
max:    .skip   4

head:   .4byte  p0      // head contains the address of the first list elem
                        // head->[34]->[-9]->[99]->[78]->[55]

// list structure is: [int value, next]
p0: .4byte  34, p3      // p0 contains [34, p3]
p1: .4byte  59, p2      // p1 contains [99, p2]
p2: .4byte  66, p4      // p2 contains [78, p4]
p3: .4byte  -9, p1      // p3 contains [-9, p1]
p4: .4byte  50, 0       // p4 contains [55, null]

        .end
   

/home/cs255001/demo/asm/7-while/max-list.s