Programming Practice 3 - Dynamic Data Structure - 2006. 05. 08.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 12 – Data Structures
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
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.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 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.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Introduction to Data Structures Systems Programming.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 22 November 17, 2009.
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.
Data Structures Systems Programming. 22 Data Structures  Queues –Queuing System Models –Queue Data Structures –A Queue Example  Trees –Binary Trees.
CSC 211 Data Structures Lecture 23
Pointers & Dynamic Data Structures Chapter Dynamic Data Structures t Arrays & structs are static (compile time) t Dynamic expand as program executes.
Programming Practice Introduction Tree Operations. Binary Search Tree. File Processing Create, read, write and update files. Sequential.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
 2002 Prentice Hall, Inc. All rights reserved. Chapter 19 – Data Structures Outline 19.1 Introduction 19.2 Self-Referential Classes 19.3 Dynamic Memory.
Introduction to Data Structures Systems Programming Concepts.
C Program Design C Data Structures 主講人:虞台文. Content Introduction Self-Referential Structures Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Queues.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Data Structures Dale Roberts, Lecturer Computer Science,
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.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
COSC 2P03 Week 21 Stacks – review A Last-In First-Out (LIFO) structure Basic Operations: –push : insert data item onto top of stack –pop : remove data.
© 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.
CPT: Lists/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to describe linked lists in C 15. Lists.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
12 C Data Structures.
12 C Data Structures.
Elementary Data Structures
Chapter 22 Custom Generic Data Structures
Introduction to Data Structures
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Chapter 21: Binary Trees.
Chapter 19 – Data Structures
Review & Lab assignments
Pointers & Dynamic Data Structures
Presentation transcript:

Programming Practice 3 - Dynamic Data Structure

Objectives Allocate and free memory dynamically for data objects. Form linked data structures using pointers, self- referential structures and recursion. Create and manipulate linked lists, queues, stacks and binary trees. Understand various important applications of linked data structures.

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

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

self-referential structures linked together 1015

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 );

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

Linked Lists

Linked List Operations Loop to find specific data value in the list Allocate new listnode ListNode * currentPtr ; currentPtr = startPtr ; while( currentPtr != NULL ) { if(value == currentPtr->data) break; currentPtr = currentPtr->nextPtr; } ListNode * newPtr; newPtr = malloc(sizeof(ListNode));

Linked List Operations insert listnode at beginning of list newPtr->nextPtr = startPtr; startPtr = newPtr; startPtr newPtr

Linked List Operations insert listnode between prevPtr and currentPtr prevPtr->nextPtr = newPtr; newPtr->nextPtr =currentPtr; prevPtr newPtr currentPtr

Linked List Operations insert listnode after currentPtr newPtr->nextPtr =currentPtr->nextPtr; currentPtr->nextPtr = newPtr; currentPtr newPtr

Linked List Operations Delete first node of the linked list tempPtr = startPtr; startPtr = startPtr->nextPtr; free(tempPtr); tempPtr = NULL; startPtr tempPtr

Linked List Operations Delete node at currentPtr tempPtr = currentPtr; prevPtr->nextPtr = currentPtr->nextPtr; free(tempPtr); tempPtr = NULL; prevPtr tempPtr currentPtr

Linked List Operations iterate each node of linked list While(currentPtr != NULL) { printf(); currentPtr = currentPtr->nextPtr; }

Operating and maintaining a list Insert –Insert a new value into the list in sorted order. Delete –Delete a list element isEmpty –Return 1 if the list is empty, 0 otherwise. printList –Print the list. Instructions –Display program instructions to user.

Linked lists Design Data Structure and Function Prototype. struct listnode { char data; /*define data as char */ struct listnode * nextPtr ; /* list node pointer */ }; typedef struct listnode ListNode; typedef ListNode * ListNodePtr; void insert (ListNodePtr *sPtr, char value) ; char delete (ListNodePtr * sPtr, char value); int isEmpty (ListNodePtr * sPtr); void printList (ListNodePtr currentPtr); void instructions(void);

Considerations Insert –You must compare input value with that of linked lists. (increasing order) Delete –Check if the startPtr is null. If so, return error message. (call isEmpty function) Instructions –1 : insert, 2: delete, 3: end.

Linked Lists Codes

Debugging using GDB Compile –g option –gcc ex.c –g –o ex Running –gdb./ex Command line mode –(gdb) run : Start debugged program –(gdb) next : Step program –(gdb) show : Generic command for showing things about the debugger –(gdb) where : Print backtrace of all stack frames, or innermost COUNT frames

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

Stacks operations

Stacks Operations

Stacks Design Data Structure and function prototype struct stacknode { int data; /*define data as int */ struct stacknode * nextPtr ; /* stack node pointer */ }; typedef struct stacknode StackNode; typedef StackNode * StackNodePtr; void push (StackNodePtr *sPtr, int value) ; int pop (StackNodePtr * sPtr, int value); int isEmpty (StackNodePtr topPtr); void printStack (StackNodePtr currentPtr); void instructions(void);

fig12_08.c (Part 1 of 6)

fig12_08.c (Part 2 of 6)

fig12_08.c (Part 3 of 6)

fig12_08.c (Part 4 of 6)

fig12_08.c (Part 5 of 6)

fig12_08.c (Part 6 of 6) Program Output (Part 1 of 2) Enter choice: 1 to push a value on the stack 2 to pop a value off the stack 3 to end program ? 1 Enter an integer: 5 The stack is: 5 --> NULL ? 1 Enter an integer: 6 The stack is: 6 --> 5 --> NULL

Program Output (Part 2 of 2) ? 1 Enter an integer: 4 The stack is: 4 --> 6 --> 5 --> NULL ? 2 The popped value is 4. The stack is: 6 --> 5 --> NULL ? 2 The popped value is 6. The stack is: 5 --> NULL ? 2 The popped value is 5. The stack is empty. ? 2 The stack is empty. ? 4 Invalid choice. Enter choice: 1 to push a value on the stack 2 to pop a value off the stack 3 to end program ? 3 End of run.

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)

Queues

Queues Operations

Queues Design Data Structure and function prototype struct queuenode { int data; /*define data as int */ struct queuenode * nextPtr ; /* queue node pointer */ }; typedef struct queuenode QueueNode; typedef QueueNode * QueueNodePtr; void enqueue (QueueNodePtr *headerPtr, QueueNodePtr * tailPtr, int value) ; int dequeue (QueueNodePtr * headerPtr, QueueNodePtr * tailPtr); int isEmpty (QueueNodePtr headerPtr); void printQueue (QueueNodePtr currentPtr); void instructions(void);

fig12_13.c (Part 1 of 7)

fig12_13.c (Part 2 of 7)

fig12_13.c (Part 3 of 7)

fig12_13.c (Part 4 of 7)

fig12_13.c (Part 5 of 7)

fig12_13.c (Part 6 of 7)

fig12_13.c (Part 7 of 7) Program Output (Part 1 of 2) Enter your choice: 1 to add an item to the queue 2 to remove an item from the queue 3 to end ? 1 Enter a character: A The queue is: A --> NULL ? 1 Enter a character: B The queue is: A --> B --> NULL ? 1 Enter a character: C The queue is: A --> B --> C --> NULL

Program Output (Part 2 of 2) ? 2 A has been dequeued. The queue is: B --> C --> NULL ? 2 B has been dequeued. The queue is: C --> NULL ? 2 C has been dequeued. Queue is empty. ? 2 Queue is empty. ? 4 Invalid choice. Enter your choice: 1 to add an item to the queue 2 to remove an item from the queue 3 to end ? 3 End of run.

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

Trees

Trees Data Structure struct treenode { int data; /*define data as int */ struct treenode * leftPtr ; /* tree node pointer */ struct treenode * rightPtr ; /* tree node pointer */ }; typedef struct treenode TreeNode; typedef TreeNode * TreeNodePtr; TreeNode * rootPtr ;

Tree Operations Insert when tree is empty. Insert when tree is not empty. if (treePtr == NULL) treePtr = malloc(); treePtr->data = value; treePtr->leftPtr = NULL; treePtr->rightPtr = NULL; If( treePtr != NULL && condition) treePtr->leftPtr = currentPtr ; If( treePtr != NULL && condition) treePtr->rightPtr = currentPtr;

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

Binary search tree Operations Insert –Recursive function. –When tree is empty, allocate treenode. ( finish condition ) –When tree is not empty currentPtr’s value is bigger than your value, –Call Insert function whose argument is currentPtr->leftPtr recursively. currentPtr’s value is less than your value, –Call insert function whose argument is currentPtr->rightPtr recursively. Delete is omitted because it is difficult.

Binary Search Tree Operations Insert(root,7) 7 Insert(root,3)7>3 empty Insert(root->leftPtr, 1) 3 Insert(root,11)7<11 Insert(root->rightPtr, 3) empty 11 Insert(root,1)7>1 3>1 Insert(root->leftPtr->leftPtr, 1)empty 1

Binary Search Tree Operations Void insertNode(TreeNodePtr *treePtr, int value) { if(*treePtr = NULL ) /* finish condition */ malloc(); /* tree node initialization */ return ; if((*treePtr)->value > value ) insertNode(); // recursive call if((*treePtr)->value < value ) insertNode(); // recursive call if((*treePtr)->value == value ) printf(“dup”); }

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 Traversals can be implemented using recursive functions.

Inorder Void inOrder (TreeNodePtr treePtr) { /* end condition */ if(treePtr == NULL) return; if(treePtr != NULL) { inOrder(treePtr->leftPtr) ; printf(); inOrder(treePtr->rightPtr); } 2 13

PreOrder Void preOrder (TreeNodePtr treePtr) { /* end condition */ if(treePtr == NULL) return; if(treePtr != NULL) { printf(); preOrder(treePtr->leftPtr) ; preOrder(treePtr->rightPtr); } 1 23

PostOrder Void postOrder (TreeNodePtr treePtr) { /* end condition */ if(treePtr == NULL) return; if(treePtr != NULL) { postOrder(treePtr->leftPtr) ; postOrder(treePtr->rightPtr); printf(); } 3 12

Trees

Homework Implement the binary search tree –insertNode, inOrder, preOrder, postOrder –You can implement these functions using recursive technique. –No deleteNode. Test minimum 3 input cases. One week.

Project Change code design using linked list. –Sorted insert is recommended.