Linked Lists Review Last week Dynamic memory allocation

  • Slides: 41
Download presentation
Linked Lists

Linked Lists

Review Last week • Dynamic memory allocation two cases • Single instances (One thing)

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

Declaration of pointer • Pointer holds base address of object, consider. . struct my. Type { age; height; }; int * int. Ptr; //int. Ptr holds an address of an int my. Type * my. Type. Ptr; //my. Type. Ptr holds and address of //a my. Type

Ask for memory off Heap • One instance int. Ptr = new int my.

Ask for memory off Heap • One instance int. Ptr = new int my. Type. Ptr = new my. Type; • Array or block int. Ptr = new int[1000]; my. Type. Ptr = new my. Type[1000];

Dereference pointers • To access data referred to by the pointer • Case of

Dereference pointers • To access data referred to by the pointer • Case of simple atomic data, assuming y is an int. *int. Ptr = 8; y = *int. Ptr; • Case of structured data my. Type. Ptr->age = 10; my. Type. Ptr->height = 56;

Dereference Pointers • Case of array of simple atomic type for (i=0; i<n. Items;

Dereference Pointers • Case of array of simple atomic type for (i=0; i<n. Items; i++) int. Ptr[i] = 10; • Case of array of structured data for (i=0; i<n. Item; i++) my. Type. Ptr[i]. age = 10;

Releasing memory to prevent memory leaks • single instances delete int. Ptr; delete my.

Releasing memory to prevent memory leaks • single instances delete int. Ptr; delete my. Type. Ptr; • arrays delete [] int. Ptr; delete [] my. Type. Ptr;

LINKED LISTS • Very important application of dynamic memory allocation

LINKED LISTS • Very important application of dynamic memory allocation

Linked List ADT • Is a dynamic data structure created using pointers. • They

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

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)

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

A Node struct mydata { int age; float height; }; struct Node { mydata;

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

Creating a Linked List struct List { int n. Nodes; Node * front; };

Creating a Linked List struct List { int n. Nodes; Node * front; }; typedef Node * Node. Ptr; List A; Node. Ptr new. Node = NULL; A. front = NULL; //initialise “construct” A. n. Nodes = 0; // List A

After declaration of pointers A. front NULL new. Node NULL

After declaration of pointers A. front NULL new. Node NULL

Insertion to list new. Node = new Node; new. Node->data. age = 10; new.

Insertion to list new. Node = new Node; new. Node->data. age = 10; new. Node->data. height = 43. 5; new. Node->next = NULL; A. front NULL data->age new. Node data->height next 10 43. 5 NULL

Insertion to list A. front = new. Node; A. n. Nodes++; new. Node =

Insertion to list A. front = new. Node; A. n. Nodes++; new. Node = NULL; A. front data->age data->height next new. Node NULL 10 43. 5 NULL

Insertion to list new. Node = new Node; //reuse pointer new. Node->data. age =

Insertion to list new. Node = new Node; //reuse pointer new. Node->data. age = 20; new. Node->data. height = 63. 5; new. Node->next = NULL; A. front data->age data->height next data->age new. Node data->height next 10 43. 5 NULL 20 63. 5 NULL

Insertion to list A. front->next = new. Node; A. n. Nodes++; new. Node =

Insertion to list A. front->next = new. Node; A. n. Nodes++; new. Node = NULL data->age A. front data->height 10 43. 5 next data->age data->height next new. Node NULL 20 63. 5 NULL

Deletion from list (note requires another pointer) Deletion from front of list Node. Ptr

Deletion from list (note requires another pointer) Deletion from front of list Node. Ptr current; current = A. front; data->age A. front data->height 10 43. 5 next current data->age data->height next 20 63. 5 NULL

A. front = A. front->next; A. n. Nodes--; data->age current data->height 10 43. 5

A. front = A. front->next; A. n. Nodes--; data->age current data->height 10 43. 5 next data->age A. front data->height next 20 63. 5 NULL

delete current; current = NULL; current NULL data->age A. front data->height next 20 63.

delete current; current = NULL; current NULL data->age A. front data->height next 20 63. 5 NULL

Linked lists • They require very precise programming and a high level of understanding

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

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

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 – newnode. Ptr – 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 USE DIAGRAMS • To get to data you must traverse list

To traverse and display every Node //Traverse through whole linked list and display every

To traverse and display every Node //Traverse through whole linked list and display every record stop moving current = A. front; when no more while (current != NULL){ Nodes to go to cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } Visit Node…. . then…. . move to the next

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height current 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next 50 data->age 60 83. 5 NULL 40 13. 5

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height current 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next 50 data->age 60 83. 5 NULL 40 13. 5

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height current 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next 50 data->age 60 83. 5 NULL 40 13. 5

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height current 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next 50 data->age 60 83. 5 NULL 40 13. 5

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } current A. front data->age data->height 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next 50 data->age 60 83. 5 NULL 40 13. 5

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height current 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next 50 data->age 60 83. 5 NULL 40 13. 5

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->height next 50 data->age 60 data->height 83. 5 next current NULL 40 13. 5

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->height next 50 data->age 60 data->height next current 83. 5 NULL 40 13. 5

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next 40 data->height 13. 5 next 50 data->age 60 83. 5 NULL current

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next 40 data->height 13. 5 next 50 data->age 60 83. 5 NULL current

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next data->height 40 13. 5 next 50 data->age 60 83. 5 NULL current

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next data->height 40 13. 5 next 50 data->age 60 83. 5 NULL current

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data.

Linked list Traversal current = A. front; while (current != NULL){ cout << current->data. age << " “; cout << current->data. height << endl; current = current->next; } A. front data->age data->height 20 63. 5 next data->age data->height next data->age 30 data->height 3. 5 27. 5 data->age data->height next data->height 40 13. 5 next 50 data->age 60 83. 5 NULL current

Traverse to Node n • To visit and display particular node say the 3

Traverse to Node n • To visit and display particular node say the 3 rd node in the list (n = 3). Assume that n. Nodes 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! – Create. List(List) – Add. Node(List, new.

Tidy up code necessary • Use functions! – Create. List(List) – Add. Node(List, new. Node) – Delete. Node(List, N) – Delete. List(List) – Display. Node(List, N) – Display. List(List); Demo 4

Summary • You and remove one element at a time dynamically. • You therefore

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.