 2007 Pearson Education, Inc. All rights reserved. 1 12 C Data Structures.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
 2009 Pearson Education, Inc. All rights reserved Data Structures Many slides modified by Prof. L. Lilien (even many without an explicit message).
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Trees.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Grade 12 Computer Studies HG
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Chapter 8 Data Abstractions Introduction to CS 1 st Semester, 2015 Sanghyun Park.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
 Pearson Education, Inc. All rights reserved Data Structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
Introduction to Data Structures Systems Programming.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Data Structures Systems Programming. 22 Data Structures  Queues –Queuing System Models –Queue Data Structures –A Queue Example  Trees –Binary Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
ACM/JETT Workshop - August 4-5, 2005 Using Visualization Tools To Teach Data Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury,
 2002 Prentice Hall, Inc. All rights reserved. Chapter 19 – Data Structures Outline 19.1 Introduction 19.2 Self-Referential Classes 19.3 Dynamic Memory.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
Introduction to Data Structures Systems Programming Concepts.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Programming Practice 3 - Dynamic Data Structure
Copyright © 2002 Pearson Education, Inc. Slide 1.
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Final Exam –Date: Aug 27 th –Time: 9:00am – 12:00pm –Location: TEL 0014.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Data Structures - Prabir Sarkar. AGENDA Stack Queue Linked List Trees Graphs Searching and Sorting Algorithm.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Data Structure By Amee Trivedi.
17 Data Structures.
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
12 C Data Structures.
12 C Data Structures.
Chapter 22 Custom Generic Data Structures
Introduction to Data Structures
Chapter 17 Object-Oriented Data Structures
Review & Lab assignments
Presentation transcript:

 2007 Pearson Education, Inc. All rights reserved C Data Structures

 2007 Pearson Education, Inc. All rights reserved. 2 Much that I bound, I could not free; Much that I freed returned to me. — Lee Wilson Dodd ‘Will you walk a little faster?’ said a whiting to a snail, ‘There’s a porpoise close behind us, and he’s treading on my tail. — Lewis Carroll

 2007 Pearson Education, Inc. All rights reserved. 3 There is always room at the top. — Daniel Webster Push on — keep moving — Thomas Morton I think that I shall never see A poem lovely as a tree. — Joyce Kilmer

 2007 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  To allocate and free memory dynamically for data objects.  To form linked data structures using pointers, self-referential structures and recursion.  To create and manipulate linked lists, queues, stacks and binary trees.  Various important applications of linked data structures.

 2007 Pearson Education, Inc. All rights reserved Introduction 12.2Self-Referential Structures 12.3Dynamic Memory Allocation 12.4Linked Lists 12.5Stacks 12.6Queues 12.7Trees

 2007 Pearson Education, Inc. All rights reserved Introduction  Dynamic data structures – Data structures that grow and shrink during execution  Linked lists – Allow insertions and removals anywhere  Stacks – Allow insertions and removals only at top of stack  Queues – Allow insertions at the back and removals from the front  Binary trees – High-speed searching and sorting of data and efficient elimination of duplicate data items

 2007 Pearson Education, Inc. All rights reserved Self-Referential Structures  Self-referential structures – Structure that contains a pointer to a structure of the same type – Can be linked together to form useful data structures such as lists, queues, stacks and trees – Terminated with a NULL pointer ( 0 ) struct node { int data; struct node *nextPtr; }  nextPtr – Points to an object of type node – Referred to as a link - Ties one node to another node

 2007 Pearson Education, Inc. All rights reserved. 8 Common Programming Error 12.1 Not setting the link in the last node of a list to NULL can lead to runtime errors.

 2007 Pearson Education, Inc. All rights reserved. 9 Fig | Self-referential structures linked together.

 2007 Pearson Education, Inc. All rights reserved Dynamic Memory Allocation  Dynamic memory allocation – Obtain and release memory during execution  malloc – Takes number of bytes to allocate - Use sizeof to determine the size of an object – Returns pointer of type void * - A void * pointer may be assigned to any pointer - If no memory available, returns NULL – Example newPtr = malloc( sizeof( struct node ) );  free – Deallocates memory allocated by malloc – Takes a pointer as an argument – free ( newPtr );

 2007 Pearson Education, Inc. All rights reserved. 11 Portability Tip 12.1 A structure’s size is not necessarily the sum of the sizes of its members. This is so because of various machine-dependent boundary alignment requirements (see Chapter 10).

 2007 Pearson Education, Inc. All rights reserved. 12 Common Programming Error 12.2 Assuming that the size of a structure is simply the sum of the sizes of its members is a logic error.

 2007 Pearson Education, Inc. All rights reserved. 13 Good Programming Practice 12.1 Use the sizeof operator to determine the size of a structure.

 2007 Pearson Education, Inc. All rights reserved. 14 Error-Prevention Tip 12.1 When using malloc, test for a NULL pointer return value. Print an error message if the requested memory is not allocated.

 2007 Pearson Education, Inc. All rights reserved. 15 Common Programming Error 12.3 Not returning dynamically allocated memory when it is no longer needed can cause the system to run out of memory prematurely. This is sometimes called a “memory leak.”

 2007 Pearson Education, Inc. All rights reserved. 16 Good Programming Practice 12.2 When memory that was dynamically allocated is no longer needed, use free to return the memory to the system immediately.

 2007 Pearson Education, Inc. All rights reserved. 17 Common Programming Error 12.4 Freeing memory not allocated dynamically with malloc is an error.

 2007 Pearson Education, Inc. All rights reserved. 18 Common Programming Error 12.5 Referring to memory that has been freed is an error.

 2007 Pearson Education, Inc. All rights reserved Linked Lists  Linked list – Linear collection of self-referential class objects, called nodes – Connected by pointer links – Accessed via a pointer to the first node of the list – Subsequent nodes are accessed via the link-pointer member of the current node – Link pointer in the last node is set to NULL to mark the list’s end  Use a linked list instead of an array when – You have an unpredictable number of data elements – Your list needs to be sorted quickly

 2007 Pearson Education, Inc. All rights reserved. 20 Performance Tip 12.1 An array can be declared to contain more elements than the number of data items expected, but this can waste memory. Linked lists can provide better memory utilization in these situations.

 2007 Pearson Education, Inc. All rights reserved. 21 Performance Tip 12.2 Insertion and deletion in a sorted array can be time consuming—all the elements following the inserted or deleted element must be shifted appropriately.

 2007 Pearson Education, Inc. All rights reserved. 22 Performance Tip 12.3 The elements of an array are stored contiguously in memory. This allows immediate access to any array element because the address of any element can be calculated directly based on its position relative to the beginning of the array. Linked lists do not afford such immediate access to their elements.

 2007 Pearson Education, Inc. All rights reserved. 23 Fig | Linked list graphical representation.

 2007 Pearson Education, Inc. All rights reserved. 24 Performance Tip 12.4 Using dynamic memory allocation (instead of arrays) for data structures that grow and shrink at execution time can save memory. Keep in mind, however, that the pointers take up space, and that dynamic memory allocation incurs the overhead of function calls.

 2007 Pearson Education, Inc. All rights reserved. 25 Outline fig12_03.c (1 of 8 ) Each node in the list contains a data element and a pointer to the next node

 2007 Pearson Education, Inc. All rights reserved. 26 Outline fig12_03.c (2 of 8 ) Function insert inserts data into the list Function delete removes data from the list

 2007 Pearson Education, Inc. All rights reserved. 27 Outline fig12_03.c (3 of 8 )

 2007 Pearson Education, Inc. All rights reserved. 28 Outline fig12_03.c (4 of 8 ) To insert a node into the list, memory must first be allocated for that node while loop searches for new node’s place in the list

 2007 Pearson Education, Inc. All rights reserved. 29 Outline fig12_03.c (5 of 8 ) If there are no nodes in the list, the new node becomes the “start” node Otherwise, the new node is inserted between two others (or at the end of the list) by changing pointers

 2007 Pearson Education, Inc. All rights reserved. 30 Outline fig12_03.c (6 of 8 ) while loop searches for node’s place in the list Once the node is found, it is deleted by changing pointers and freeing the node’s memory

 2007 Pearson Education, Inc. All rights reserved. 31 Outline fig12_03.c (7 of 8 ) If the start node is NULL, there are no nodes in the list

 2007 Pearson Education, Inc. All rights reserved. 32 Outline fig12_03.c (8 of 8 )

 2007 Pearson Education, Inc. All rights reserved. 33 Outline

 2007 Pearson Education, Inc. All rights reserved. 34 Outline

 2007 Pearson Education, Inc. All rights reserved. 35 Error-Prevention Tip 12.2 Assign NULL to the link member of a new node. Pointers should be initialized before they are used.

 2007 Pearson Education, Inc. All rights reserved. 36 Fig | Inserting a node in order in a list.

 2007 Pearson Education, Inc. All rights reserved. 37 Fig | Deleting a node from a list.

 2007 Pearson Education, Inc. All rights reserved Stacks  Stack – New nodes can be added and removed only at the top – Similar to a pile of dishes – Last-in, first-out (LIFO) – Bottom of stack indicated by a link member to NULL – Constrained version of a linked list  push – Adds a new node to the top of the stack  pop – Removes a node from the top – Stores the popped value – Returns true if pop was successful

 2007 Pearson Education, Inc. All rights reserved. 39 Common Programming Error 12.6 Not setting the link in the bottom node of a stack to NULL can lead to runtime errors.

 2007 Pearson Education, Inc. All rights reserved. 40 Fig | Stack graphical representation.

 2007 Pearson Education, Inc. All rights reserved. 41 Outline fig12_08.c (1 of 5 ) Each node in the stack contains a data element and a pointer to the next node

 2007 Pearson Education, Inc. All rights reserved. 42 Outline fig12_08.c (2 of 5 )

 2007 Pearson Education, Inc. All rights reserved. 43 Outline fig12_08.c (3 of 5 ) To insert a node into the stack, memory must first be allocated for that node

 2007 Pearson Education, Inc. All rights reserved. 44 Outline fig12_08.c (4 of 5 ) Stack nodes are always inserted at the top, so there is no need to search for the node’s place Inserted node becomes the new top Stack nodes are always removed from the top, so there is no need to search for the node’s place Second node becomes the new top Free the memory of the popped node

 2007 Pearson Education, Inc. All rights reserved. 45 Outline fig12_08.c (5 of 5 )

 2007 Pearson Education, Inc. All rights reserved. 46 Outline

 2007 Pearson Education, Inc. All rights reserved. 47 Outline

 2007 Pearson Education, Inc. All rights reserved. 48 Fig | push operation.

 2007 Pearson Education, Inc. All rights reserved. 49 Fig | pop operation.

 2007 Pearson Education, Inc. All rights reserved Queues  Queue – Similar to a supermarket checkout line – First-in, first-out (FIFO) – Nodes are removed only from the head – Nodes are inserted only at the tail  Insert and remove operations – Enqueue (insert) and dequeue (remove)

 2007 Pearson Education, Inc. All rights reserved. 51 Common Programming Error 12.7 Not setting the link in the last node of a queue to NULL can lead to runtime errors.

 2007 Pearson Education, Inc. All rights reserved. 52 Fig | Queue graphical representation.

 2007 Pearson Education, Inc. All rights reserved. 53 Outline fig12_13.c (1 of 6 ) Each node in the queue contains a data element and a pointer to the next node Note that unlike linked lists and stacks, queues keep track of the tail node as well as the head

 2007 Pearson Education, Inc. All rights reserved. 54 Outline fig12_13.c (2 of 6 )

 2007 Pearson Education, Inc. All rights reserved. 55 Outline fig12_13.c (3 of 6 )

 2007 Pearson Education, Inc. All rights reserved. 56 Outline fig12_13.c (4 of 6 ) To insert a node into the queue, memory must first be allocated for that node Queue nodes are always inserted at the tail, so there is no need to search for the node’s place If the queue is empty, the inserted node becomes the new head in addition to the new tail Inserted node becomes the new tail

 2007 Pearson Education, Inc. All rights reserved. 57 Outline fig12_13.c (5 of 6 ) Queue nodes are always removed from the head, so there is no need to search for the node’s place Free the memory of the removed node Second node becomes the new head If the removed node is the only node in the queue, it is the tail as well as the head of the queue, so tailPtr must be set to NULL

 2007 Pearson Education, Inc. All rights reserved. 58 Outline fig12_13.c (6 of 6 )

 2007 Pearson Education, Inc. All rights reserved. 59 Outline

 2007 Pearson Education, Inc. All rights reserved. 60 Outline

 2007 Pearson Education, Inc. All rights reserved. 61 Fig | enqueue operation.

 2007 Pearson Education, Inc. All rights reserved. 62 Fig | dequeue operation.

 2007 Pearson Education, Inc. All rights reserved Trees  Tree nodes contain two or more links – All other data structures we have discussed only contain one  Binary trees – All nodes contain two links - None, one, or both of which may be NULL – The root node is the first node in a tree. – Each link in the root node refers to a child – A node with no children is called a leaf node

 2007 Pearson Education, Inc. All rights reserved. 64 Fig | Binary tree graphical representation.

 2007 Pearson Education, Inc. All rights reserved. 65 Common Programming Error 12.8 Not setting to NULL the links in leaf nodes of a tree can lead to runtime errors.

 2007 Pearson Education, Inc. All rights reserved Trees  Binary search tree – Values in left subtree less than parent – Values in right subtree greater than parent – Facilitates duplicate elimination – Fast searches - for a balanced tree, maximum of log n comparisons

 2007 Pearson Education, Inc. All rights reserved. 67 Fig | Binary search tree.

 2007 Pearson Education, Inc. All rights reserved Trees  Tree traversals: – Inorder traversal – prints the node values in ascending order 1. Traverse the left subtree with an inorder traversal 2. Process the value in the node (i.e., print the node value) 3. Traverse the right subtree with an inorder traversal – Preorder traversal 1. Process the value in the node 2. Traverse the left subtree with a preorder traversal 3. Traverse the right subtree with a preorder traversal – Postorder traversal 1. Traverse the left subtree with a postorder traversal 2. Traverse the right subtree with a postorder traversal 3. Process the value in the node

 2007 Pearson Education, Inc. All rights reserved. 69 Outline fig12_19.c (1 of 5 ) Each node in the tree contains a data element and a pointer to the left and right child nodes

 2007 Pearson Education, Inc. All rights reserved. 70 Outline fig12_19.c (2 of 5 )

 2007 Pearson Education, Inc. All rights reserved. 71 Outline fig12_19.c (3 of 5 ) To insert a node into the tree, memory must first be allocated for that node If the inserted node’s data is less than the current node’s, the program will attempt to insert the node at the current node’s left child.

 2007 Pearson Education, Inc. All rights reserved. 72 Outline fig12_19.c (4 of 5 ) If the inserted node’s data is greater than the current node’s, the program will attempt to insert the node at the current node’s right child. The inorder traversal calls an inorder traversal on the node’s left child, then prints the node itself, then calls an inorder traversal on the right child.

 2007 Pearson Education, Inc. All rights reserved. 73 Outline fig12_19.c (5 of 5 ) The preorder traversal prints the node itself, then calls a preorder traversal on the node’s left child, then calls a preorder traversal on the right child. The postorder traversal calls an postorder traversal on the node’s left child, then calls an postorder traversal on the right child, then prints the node itself.

 2007 Pearson Education, Inc. All rights reserved. 74 Outline

 2007 Pearson Education, Inc. All rights reserved. 75 Fig | Binary search tree with seven nodes.