20
Dynamic Linked List Lesson xx

Dynamic Linked List Lesson xx

  • Upload
    kevlyn

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

Dynamic Linked List Lesson xx. Objectives. Memory from the heap Dynamic memory allocation using the new operator Build a dynamic linked list Another way of traversing a linked list Deallocating memory using delete operator Dynamic linked list variation. Memory Setup. Operating System. - PowerPoint PPT Presentation

Citation preview

Slide 1

Dynamic Linked List Lesson xx

In the previous modules we showed you how to build a static linked list. This presentation shows you basics of a dynamic linked list.

1ObjectivesMemory from the heapDynamic memory allocation using the new operatorBuild a dynamic linked listAnother way of traversing a linked listDeallocating memory using delete operator Dynamic linked list variation

Our goal is to build a dynamic linked list but before we do that you must know about the heap and the new operator. We will look at different code for traversing a linked list, deallocating memory and dynamic linked list variations.2Memory SetupProgramStackOperating SystemHeap

The memory of the computer is set up something like this picture. The operating system resides at the top, then your program. The stack is where your local variables are stored. The heap is a special area that you can use to obtain memory at run time. This process is called dynamic memory allocation and is done with operator new.3Operator newint * ptr;ptr = new int; Program StackOperating System Heap9abintptr9ab

Lets look at this code fragment. int * ptr means: set up a pointer to an integer and name the pointer ptr.ptr = new int; tells the computer go to the heap and find enough memory to store an integer. When the memory is found, the address of the memory is stored in ptr. In our illustration, ptr contains 9ab, the address of the int that was allocated from the heap. Another way to look at this, ptr is pointing to the memory that is dynamically allocated with the new command.4Program to Create a Dynamic Linked Liststruct entry { int value; entry* next; };

int main() { entry* temp, * head = 0; // initially empty list /*allocate the first node*/ head= new entry; temp = head;

// create 4 more nodes for (int i = 0; i < 4; i++) { cin >>temp>value; /*put address of next cell in .next member */ temp>next = new entry; temp = temp>next; //temp now points to last node } temp>value = 0; /*last node points to nothing*/ temp>next = 0; /*it's a dummy node*/

(Part 1)

Now that you know how to obtain memory from the heap, here is the code to create a dynamic linked list. Part 1 creates 5 nodes.5Program to Create a Dynamic Linked List // traverse the list for (entry* p = head; p; p = p->next) { cout next = new entry; temp = temp>next; //temp now points to last node }

The first part of this code sets up a for loop to execute 4 times. In the body of the loop, we read in input and create an additional node. After we execute the statement: cin >> temp->value; the first time, we will have the following picture: head and temp will be pointing to the first node. If the user enter the #42, head->value or temp->value will contain the # 42.9Adding Additional Nodes 76ctemp76chead88b42(76c) for (int i = 0; i < 4; i++) { cin >>temp>value; /*put address of next cell in .next member */ temp>next = new entry; temp = temp>next; //temp now points to last node } (88b)

Temp->next = new entry; allocates a structure of the form entry from the heap. The address of this newly created structure is stored in temp->next. From our diagram, temp->next is pointing to the new node. The other way you can look at this is that temp->next is pointing to a newly created node from the heap10Advancing temp to Next Node 88btemp76chead88b42(76c) for (int i = 0; i < 4; i++) { cin >>temp>value; /*put address of next cell in .next member */ temp>next = new entry; temp = temp>next; //temp now points to last node } (88b)

Temp = temp->next; advances the pointer called temp to the newly created node.11Repeating the Loop 4aftemp76chead88b42(76c) for (int i = 0; i < 4; i++) { cin >>temp>value; /*put address of next cell in .next member */ temp>next = new entry; temp = temp>next; //temp now points to last node } (88b)974af(4af)

Executing the body of the loop an addition time will produce the following picture. We read in a # into the second node, allocate the 3rd node and make temp point to the 3rd node.12End of the Loop42973517 headtemp

When we exit the loop, our linked list will have 5 nodes and temp will be pointing to the last node13Terminating the Linked List429735170 0headtemptemp>value = 0; /*last node points to nothing*/ temp>next = 0; /*it's a dummy node*/

The last node of a linked list always contains a null to indicate the end of the list. This is what temp->next = 0; does. Since this node is used to indicate the end of the list and does not contain any useful data, it is called a dummy node, you dont have to, but we put a zero in temp->value.14Printing the Linked List429735170 0headtemp // traverse the list for (entry* p = head; p; p = p->next) { cout next; delete head; head = n; }

After executing the statement delete head; the 1st node will be released Executing: head = n; makes head point to the second node of the original list. The picture drawn shows you how the memory looks after the 1st iteration of the loop. If we perform the loop again, n will point to the 3rd node, we delete the 2nd node and head is made to point to the 3rd node. We continue the loop until head contains a null at which time, all the nodes of the linked list are freed up.

18Variations of a Linked List // create 4 more nodes for (int i = 0; i < 4; i++) { cin >>temp>value; temp>next = new entry; temp = temp>next; } int n; cout > n;

// create n-1 more nodes for (int i = 0; i < n-1; i++) { cin >>temp>value; temp>next = new entry; temp = temp>next; }

In our program, we used a for loop to create 4 more nodes with the code shown in the left box. If we want our program to be truly dynamic, we can change our code so that we ask the user how many nodes they want in the linked list. The code on the right asks the user to input the number of nodes with the cout and the cin statement. The for loop runs from 0 to n-1. With this code, you can build a linked list that is any size were as in our 1st program, our linked list was always 5 elements long.19SummaryMemory from the heapDynamic memory allocation using the new operatorBuild a dynamic linked listAnother way of traversing a linked listDeallocating memory using delete operator Dynamic linked list variation

In this presentation, we built a dynamic linked list by using the new operator to allocate memory from the heap. We learned another way of traversing the list and about deallocating memory with the delete operator. In the next module, well talking about a different type of linked list called a stack.20