1 / 41

Linked Lists

Linked Lists. 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.

jtrahan
Download Presentation

Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Lists

  2. 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)

  3. 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

  4. Ask for memory off Heap • One instance intPtr = new int myTypePtr = new myType; • Array or block intPtr = new int[1000]; myTypePtr = new myType[1000];

  5. 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;

  6. 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;

  7. Releasing memory to prevent memory leaks • single instances delete intPtr; delete myTypePtr; • arrays delete [] intPtr; delete [] myTypePtr;

  8. LINKED LISTS • Very important application of dynamic memory allocation

  9. 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.

  10. Linked List ADT Data: • A linked lists contains nodes Operations: • Create list • Insert a node • Delete a node • Display list • Delete a list

  11. Nodes • Contain a data field • Contain self reference (a pointer)

  12. A Node struct mydata { int age; float height; }; struct Node { mydata data; Node *next; }; Note reference to a node within node

  13. 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

  14. After declaration of pointers A.front NULL newNode NULL

  15. newNode = new Node;newNode->data.age = 10;newNode->data.height = 43.5;newNode->next = NULL; Insertion to list A.front NULL newNode

  16. A.front = newNode;A.nNodes++;newNode = NULL; Insertion to list A.front newNode NULL

  17. Insertion to list newNode = new Node; //reuse pointernewNode->data.age = 20;newNode->data.height = 63.5;newNode->next = NULL; A.front newNode

  18. Insertion to list A.front->next = newNode;A.nNodes++;newNode = NULL A.front newNode NULL

  19. Deletion from list (note requires another pointer) Deletion from front of list NodePtr current;current = A.front; A.front current

  20. A.front = A.front->next;A.nNodes--; current A.front

  21. delete current;current = NULL; current NULL A.front

  22. 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.

  23. 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.

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. Tidy up code necessary • Use functions! • CreateList(List) • AddNode(List, newNode) • DeleteNode(List, N) • DeleteList(List) • DisplayNode(List,N) • DisplayList(List); Demo 4

  41. 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.

More Related