Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming Practice 3 - Dynamic Data Structure - 2006. 05. 08."— Presentation transcript:

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

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

3 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

4 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

5 self-referential structures linked together 1015

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

7 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

8 Linked Lists

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

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

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

12

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

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

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

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

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

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

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

20 Linked Lists Codes

21

22

23

24

25

26

27

28 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

29 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

30 Stacks operations

31 Stacks Operations

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

33 fig12_08.c (Part 1 of 6)

34 fig12_08.c (Part 2 of 6)

35 fig12_08.c (Part 3 of 6)

36 fig12_08.c (Part 4 of 6)

37 fig12_08.c (Part 5 of 6)

38 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

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

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

41 Queues

42 Queues Operations

43

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

45 fig12_13.c (Part 1 of 7)

46 fig12_13.c (Part 2 of 7)

47 fig12_13.c (Part 3 of 7)

48 fig12_13.c (Part 4 of 7)

49 fig12_13.c (Part 5 of 7)

50 fig12_13.c (Part 6 of 7)

51 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

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

53 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

54 Trees

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

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

57 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

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

59 Binary Search Tree Operations 7 3 11 1 18 5 9 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

60 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”); }

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

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

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

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

65

66 Trees

67

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

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


Download ppt "Programming Practice 3 - Dynamic Data Structure - 2006. 05. 08."

Similar presentations


Ads by Google