assembly language
Linked_List_Insertion_and_Display (Chapter 11, Pr 11)
Implement a singly linked list, using the dynamic memory allocation functions presented in this chapter. Each node should be a structure named Node containing an integer value and a pointer to the next link in the list like:
NODE STRUCT
intVal SDWORD ?
pNext DWORD ?
NODE ENDS
PNODE TYPEDEF PTR NODE
To create a node, simply call
INVOKE HeapAlloc, hHeap, HEAP_ZERO_MEMORY, SIZEOF NODE
Using a loop, prompt the user for as many integers as they want to enter. As each integer is entered, allocate a Node object, move the integer value in the Node, and append the Node to the linked list. Finally, display the entire list from beginning to end like this:
Number of integers to enter? 5
Signed integer node value? 123
Signed integer node value? 1
Signed integer node value? 22
Signed integer node value? 567
Signed integer node value? 888
Contents of linked list:
Dummy Head -> +123 -> +1 -> +22 -> +567 -> +888
Press any key to continue . . .
An alternative is as mentioned in the textbook to let the user control the number of values input. When zero is entered, stop the loop. So an iterative can be like this:
Enter a signed integer node value (zero to end): 11
Enter a signed integer node value (zero to end): -2
Enter a signed integer node value (zero to end): 333
Enter a signed integer node value (zero to end): 0
Contents of linked list:
Dummy Head -> +11 -> -2 -> +333
You can create a singly linked list with a dummy head that will simplify the insertion operation without you differentiating empty list and non-empty in insertion. You can define the following to use in the linked list. Using tail insertion is good for display sequence normally.
head NODE <0,0> ; Dummy head
pTailNode PNODE ?
pCurrNode PNODE ?
To be efficient, instead of pTailNode and pCurrNode, using two registers like ESI and EDI would be great. Finally, don't forget to free all dynamically allocated nodes when done. So the following steps are suggested:
Initialization to get or create a heap
Prompt for a signed integer and create a node
Display the list of nodes
Release all the heap memory created
If well designed, the last two steps can be done in one loop.