410 likes | 427 Views
Learn about dynamic memory allocation, pointers, structured data, arrays, and the implementation of linked lists in C++ programming. Includes tutorials, examples, and tips on avoiding memory leaks.
E N D
Review Last week • Dynamic memory allocation two cases • Single instances (One thing) • atomic data (int char float) • structured data (structs and classes) • Arrays or blocks of things • atomic data • structured data (structs and classes)
Declaration of pointer • Pointer holds base address of object, consider.. struct myType { age; height; }; int * intPtr; //intPtr holds an address of an int myType * myTypePtr; //myTypePtr holds and address of //a myType
Ask for memory off Heap • One instance intPtr = new int myTypePtr = new myType; • Array or block intPtr = new int[1000]; myTypePtr = new myType[1000];
Dereference pointers • To access data referred to by the pointer • Case of simple atomic data, assuming y is an int. *intPtr = 8; y = *intPtr; • Case of structured data myTypePtr->age = 10; myTypePtr->height = 56;
Dereference Pointers • Case of array of simple atomic type for (i=0;i<nItems;i++) intPtr[i] = 10; • Case of array of structured data for (i=0;i<nItem;i++) myTypePtr[i].age = 10;
Releasing memory to prevent memory leaks • single instances delete intPtr; delete myTypePtr; • arrays delete [] intPtr; delete [] myTypePtr;
LINKED LISTS • Very important application of dynamic memory allocation
Linked List ADT • Is a dynamic data structure created using pointers. • They can grow and shrink according to need. • We have to specify all the operations that we have for array based lists.
Linked List ADT Data: • A linked lists contains nodes Operations: • Create list • Insert a node • Delete a node • Display list • Delete a list
Nodes • Contain a data field • Contain self reference (a pointer)
A Node struct mydata { int age; float height; }; struct Node { mydata data; Node *next; }; Note reference to a node within node
Creating a Linked List struct List { int nNodes; Node * front; }; typedef Node * NodePtr; List A; NodePtr newNode = NULL; A.front = NULL; //initialise “construct” A.nNodes = 0; // List A
After declaration of pointers A.front NULL newNode NULL
newNode = new Node;newNode->data.age = 10;newNode->data.height = 43.5;newNode->next = NULL; Insertion to list A.front NULL newNode
A.front = newNode;A.nNodes++;newNode = NULL; Insertion to list A.front newNode NULL
Insertion to list newNode = new Node; //reuse pointernewNode->data.age = 20;newNode->data.height = 63.5;newNode->next = NULL; A.front newNode
Insertion to list A.front->next = newNode;A.nNodes++;newNode = NULL A.front newNode NULL
Deletion from list (note requires another pointer) Deletion from front of list NodePtr current;current = A.front; A.front current
A.front = A.front->next;A.nNodes--; current A.front
delete current;current = NULL; current NULL A.front
Linked lists • They require very precise programming and a high level of understanding of manipulation of data by addresses. • Writing functions that manipulate linked lists also requires a very clear understanding of pointers. • This is HARD so take your time.
Non random access • An array allows us to go directly to any element • e.g. record data[5]; cout << data[3].age; • With a linked list this is not possible you have to start at the beginning an visit each node before you move to the next node. • This is called traversing.
Summary • You need 3 structs defined • Data • Node that contains a pointer to Node • List that contains pointer to first Node • You need some pointers e.g • newnodePtr • Current • Previous • You need to declare a list (List A) • You need to dynamically get node memory and fill as you need. • You must then insert this into list (at front, end anywhere) carefully. • You must delete nodes carefully • To get to data you must traverse list USE DIAGRAMS
To traverse and display every Node //Traverse through whole linked list and display every record current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } stop moving when no more Nodes to go to Visit Node….. then….. move to the next
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front current NULL
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front current NULL
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front current NULL
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front current NULL
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } current A.front NULL
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front current NULL
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front NULL current
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front NULL current
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front NULL current
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front NULL current
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front NULL current
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front NULL current
Linked list Traversal current = A.front; while (current != NULL){ cout << current->data.age << " “; cout << current->data.height << endl; current = current->next; } A.front NULL current
Traverse to Node n • To visit and display particular node say the 3rd node in the list (n = 3). Assume that nNodes is greater than 3! • initialise current Node pointer to First Node current = A.front; //set current to first for (i=1;i<3;i++) { current = current->next; //move forward two } cout << current->data.age << " “; //current now at third cout << current->data.height << endl; DEMO 3
Tidy up code necessary • Use functions! • CreateList(List) • AddNode(List, newNode) • DeleteNode(List, N) • DeleteList(List) • DisplayNode(List,N) • DisplayList(List); Demo 4
Summary • You and remove one element at a time dynamically. • You therefore must know the syntax for de-referencing single instances of objects. • You must understand how to insert, delete nodes and traverse lists. • This technique is the basis for many advanced data structures • Trees, graphs, stacks, queues. • You need to understand structs functions pointers, loops, selection …thoroughly! • Take your time: look at the demo programs • Linked Lists require a high degree of precision. • Use diagrams to help you.