Download presentation
Presentation is loading. Please wait.
1
COMP 110 Introduction to Programming Mr. Joshua Stough
2
What are Data Structures? Abstract data types –data that is “more” (quantity and complexity) than a primitive –dynamic –ADT encapsulates data and related services –same as a class? yes, classes with the special purpose of primarily maintaining data Example: String class
3
Data Structures @ UNC COMP 410 –prereq: COMP 401 (after 110) Research –researchers develop new data structures for efficiently storing and retrieving data Data structures are used in all areas of computer science. Knowledge of efficient ways of storing information is essential.
4
What Do We Want? Example Maintain a collection of addresses (Strings) –first, middle, and last name –telephone number –email address Services –new, find, insert, delete, read, replace, sort “Infinite” (unspecified) length
5
Arrays? Perhaps but… –need to specify the length at some point once specified, can’t change need to create a new array, copy, etc. –insertion is “impossible” (hard) –deletion is wasteful
6
Linked Lists Three classes (typically) working together –an “item” class one atomic unit of the aggregate data e.g., a “Name” class (item) might have two instance variables String first, last; –a “node” class one “item” and a reference to the next “node” the next reference is the “link” in “linked list” –a “list” class reference to the first “node”—head of the list
7
Linked Lists data Item nextNode Node List head Node
8
Linked Lists New List head Node List head Node
9
Linked Lists Find List head Node
10
Linked Lists Insert Node List head Node
11
Linked Lists Delete List head Node
12
Stacks Like a stack of paper (or cafeteria trays) –“last-in first-out” (LIFO) can only add to the top - push can only remove from the top - pop Why? –Often used to keep track of execution in a program when returning from a method call, the computer has to remember where it last was
13
Stack top Node Only have access to the top of the stack May be implemented as a linked list
14
Stacks Example 10 public shuffle() { 11 int ind1 = nextInt(NUM_CARDS); } 1 public static void main (String[] args) { 2 deck = new Deck(); 3 deck.shuffle(); 4 System.out.println (deck); } 20 public int nextInt (int num) { 21 return 0; } Call Stack 3 3 11 top
15
Stack Applet http://www.cs.hope.edu/~alganim/jvall/applet/stack.html
16
Queues Standing in line –“first-in first-out” (FIFO) add to the “tail” of the list (back of line) - enqueue remove from the “head” (head of line) - dequeue Why? –often used to keep things in the order that they arrived –processes to be scheduled on the CPU –packets arriving to a router
17
Queue head Node Only have access to the head and tail of the queue May be implemented as a linked list tail
18
Queue Applet http://courses.cs.vt.edu/csonline/DataStructures/Lessons/ QueuesImplementationView/applet.html
19
Trees Nodes can have more than one link Links are bi-directional –forward link points to children –backward link points to parent Why? –keeping items in sorted order easy to add things in sorted order –fast search –also often used to parse arithmetic expressions (order of operations)
20
Binary Trees Every node has a parent, except head Every node has at most two children root - has no parent leaf - has no children root leaves
21
Binary Trees And Expressions (3 + 2) * 5 * 5 + 3 2 Called a parse tree 3 + 2 * 5 + * 3 2 5 Evaluate deepest expressions first.
22
Binary Search Tree The first item added is the root Items less than the root go on the left branch Items greater than the root go on the right branch Makes searches very efficient 3 51 4 7 possible order added: 3 1 5 4 7 3 1 5 7 4 3 5 1 7 4 3 5 1 4 7
23
Binary Search Tree Applet http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html
24
What To Expect in COMP 401 Learn more about object-oriented programming –inheritance, interfaces Learn to use 2D arrays Learn to program sorting algorithms Learn to program data structures
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.