public class List { int value; List next; } |
head +--------+ +--------+ -->+--------+ -->+--------+ | |---->| value1 | / | value2 | / | value3 | +--------+ | | / | | / | | +--------+ / +--------+ / +--------+ | next1 |- | next2 |- | null | +--------+ +--------+ +--------+ |
And you have a linked list element at "elem":
elem +--------+ +--------+ | |---->| valueX | (information has been filled in) +--------+ | | +--------+ | ???? | (don't care, because we know there +--------+ is no "next" element) |
Memory: +---------------+ head: | 8000 |-------------------------------+ +---------------+ | | | | ....... | ....... | +---------------+ | 8000 | value1 | Linked list element 1 <-----+ +---------------+ | next1=10000 |-------------------------------------+ +---------------+ | | | | +---------------+ | ....... | ....... | +---------------+ | 9000 | value3 | Linked list element 3 <----+ | +---------------+ | | | next3=0 (null | | | +---------------+ | | ....... | | ....... | | +---------------+ | | 10000 | value2 | Linked list element 2 | <-----+ +---------------+ | | next2=9000 |-----------------------------+ +---------------+ ....... ....... +---------------+ elem: | 20000 |-----------------------------+ +---------------+ | ....... | ....... | +---------------+ | 20000 | valueX | New linked list element <---+ +---------------+ | nextX=???? | +---------------+ |
Note:
|
|
Graphically:
|
Memory: +---------------+ head: | 20000 |---------------------------------------+ +---------------+ | | | | ....... | ....... | +---------------+ | 8000 | value1 | Linked list element 1 <------+ | +---------------+ | | | next1=10000 |-------------------------------------+ | +---------------+ | | | | | | | | +---------------+ | | | ....... | | | ....... | | | +---------------+ | | | 9000 | value3 | Linked list element 3 <----+ | | | +---------------+ | | | | | next3=0 (null)| | | | | +---------------+ | | | | ....... | | | | ....... | | | | +---------------+ | | | | 10000 | value2 | Linked list element 2 | <-----+ | +---------------+ | | | | next2=9000 |-----------------------------+ | | +---------------+ | | ....... | | ....... | | +---------------+ | | elem: | 20000 |-----------------------------+ | | +---------------+ | | | ....... | | | ....... | | | +---------------+ | | | 20000 | valueX | new linked list element <---+---------+ +---------------+ | | nextX=8000 |----------------------------------+ +---------------+ |
List head; // Assume head has a list already List elem; main( ) { ptr = new List( ); // Make a new list element // Effect: // 1. reserve 8 bytes of memory // to store a List object // 2. return the address of the // location of the reserved memory ptr.value = 1234; // Assignment some value head = Insert(head, ptr); // Insert into list } |
static List Insert(List head, List newelem) { newelem.next = head; return(newelem); } |
How to run the program:
|
|
|
ptr = new List(); // Make a new list element // Effect: // 1. reserve 8 bytes of memory // to store a List oject // 2. return the address of the // location of the reserved memory ptr.value = 1234; // Assignment some value head = Insert(head, ptr); // Insert into list |
Main( ) in M68000 code:
**** ptr = new List(); move.l #8, d0 ; 8 byte used to store a List object jsr malloc ; allocate (8 bytes) of memory move.l a0, ptr ; ptr = address of the allocated memory **** ptr.value = 1234; move.l #1234, (a0) **** head = Insert(head, ptr); move.l head, -(a7) ; pass head on stack move.l ptr, -(a7) ; pass ptr on stack bsr InsertList ; call InsertList adda.l #8, a7 ; clean up parameters move.l d0, head ; head = return value |
static List Insert(List head, List newelem) { newelem.next = head; return(newelem); } |
Stack Frame: (for your reference) +--------------+ <------ a6 | saved a6 | 0(a6) +--------------+ | return addr | 4(a6) +--------------+ | newelem | 8(a6) +--------------+ | head | 12(a6) +--------------+ |