1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees
2 Introduction Dynamic data structures –Grow and shrink at execution time –Several types Linked lists Stacks Queues Binary trees
3 Self-Referential Classes Self-referential class –Contains instance variable referring to object of same class class Node { private int data; private Node nextNode; // reference to next linked node } Member nextNode is a link –nextNode “links” a Node object to another Node object
4 Self-Referential Classes (cont.) Self-referential-class objects linked together. 1510
5 Dynamic Memory Allocation Dynamic memory allocation –Obtain more memory at execution time to store new objects Declaration and class-instance creation expression Node nodeToAdd = new Node ( 10 );
6 Linked Lists Linked list –Linear collection of self-referential classes (nodes) –Connected by reference links –Nodes can be inserted and deleted anywhere in linked list –Last node is set to null to mark end of list..\..\week14\linkedlist
7 Linked Lists (cont.) Linked list graphical representation. firstNode... HD Q lastNode
8 Linked Lists (cont.) Graphical representation of operation insertAtFront. firstNode new Listnode firstNode new Listnode (a) (b)
9 Linked Lists (cont.) Graphical representation of operation insertAtBack. firstNode 12 new Listnode (a) (b) firstNodenew Listnode lastNode
10 Linked Lists (cont.) Graphical representation of operation removeFromFront. firstNode 12 (a) (b) lastNode firstNode removeItem
11 Linked Lists (cont.) Graphical representation of operation removeFromBack. 12 (a) (b) lastNode lastNode firstNode removeItem firstNodecurrent
12 Stacks Stack –Constrained version of a linked list Add and remove nodes only to and from the top of the stack –Push method adds node to top of stack –Pop method removes node from top of stack..\..\week14\linkedlist
13 Queues Queue –Similar to a supermarket checkout line –Nodes inserted only at tail (back) Method enqueue –Nodes removed only from head (front) Method dequeue..\..\week14\queue