Download presentation
Presentation is loading. Please wait.
1
C Program Design C Data Structures
主講人:虞台文
2
Content Introduction Self-Referential Structures
Dynamic Memory Allocation Linked Lists Stacks Queues Trees
3
C Program Design C Data Structures
Introduction
4
Introduction Dynamic data structures Linked lists Stacks Queues
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
5
C Program Design C Data Structures
Self-Referential Structures
6
List List Linked Lists List List List 100 102 235 440 888
7
Linked Lists struct node { int data; struct node *nextNode; };
Structure that contains a pointer to a structure of the same type. 100 102 235 440 888
8
struct treeNode { int data; struct treeNode. left; struct treeNode
struct treeNode { int data; struct treeNode *left; struct treeNode *right; }; Tree 300 140 282 119 230 290 750 405 510 809 760 900
9
C Program Design C Data Structures
Dynamic Memory Allocation
10
Dynamic Memory Allocation
Obtain and release memory during execution Allocate Memory Release Memory void *malloc(size_t size); void free(void *ptr);
11
void *malloc(size_t size);
How to determine the size needed? Record size #records 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. Use the sizeof operator to determine the size of a structure. Test for a NULL pointer return value. Print an error message if the requested memory is not allocated. When the allocated memory no longer needed, be sure to release it by calling free(). Cause memory leak if don’t do so.
12
void free(void *ptr); 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.” Freeing memory not allocated dynamically with malloc is an error. Referring to memory that has been freed is an error.
13
void *malloc(size_t size);
#define NUM_RECORDS 100 struct XXX{ }; struct XXX *p = NULL; p = (struct XXX *) malloc(sizeof(struct XXX) * NUM_RECORDS); if(p == NULL){ printf("Memory allocation fail!\n"); return; } free(p); p = NULL; // a good habit to do so
14
Example #include <stdio.h> #include <stdlib.h> int main ()
{ int i,n; char * buffer; printf ("How long do you want the string? "); scanf ("%d", &i); buffer = (char*) malloc (i+1); if (buffer==NULL) exit (1); for (n=0; n<i; n++) buffer[n]=rand()%26+'a'; buffer[i]='\0'; printf ("Random string: %s\n",buffer); free (buffer); return 0; } Example Generate random string of desired length.
15
Example Randomly generate 50 integers for guess.
#include <stdio.h> #include <stdlib.h> #include <time.h> main() { int array[50]; int i, guess; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0; // set false at game beginning for(i = 0; i<50; i++) array[i] = rand() % ; printf("Enter your guess (1-100) and 0 to end:"); scanf("%d", &guess); if(guess == 0) break; for(i=0; i<50 && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; // set hit so as to break the loop } if(!hit) printf("You guess a wrong number.\n"); Example Randomly generate 50 integers for guess.
16
Example Randomly generate 50 integers for guess.
#include <stdio.h> #include <stdlib.h> #include <time.h> main() { int array[50]; int i, guess; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0; // set false at game beginning for(i = 0; i<50; i++) array[i] = rand() % ; printf("Enter your guess (1-100) and 0 to end:"); scanf("%d", &guess); if(guess == 0) break; for(i=0; i<50 && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; // set hit so as to break the loop } if(!hit) printf("You guess a wrong number.\n"); Example Randomly generate 50 integers for guess.
17
#include <stdio.h>
#include <stdlib.h> #include <time.h> main() { int* array=NULL; // store dynamic pointer int i, guess, size; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0; // set false at game beginning printf("How many number do you want to generate or 0 to end?"); scanf("%d", &size); if(size==0) break; if((array = (int *) malloc(sizeof(int) * size)) == NULL){ printf("Memory allocation fail.\n"); exit(1); } for(i = 0; i<size; i++) array[i] = rand() % ; printf("Enter your guess (1-100):"); scanf("%d", &guess); for(i=0; i < size && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; if(!hit) printf("You guess a wrong number.\n"); free(array); Example Randomly generate the number of integers specified by the user for guess.
18
#include <stdio.h>
#include <stdlib.h> #include <time.h> main() { int* array=NULL; // store dynamic pointer int i, guess, size; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0; // set false at game beginning printf("How many number do you want to generate or 0 to end?"); scanf("%d", &size); if(size==0) break; if((array = (int *) malloc(sizeof(int) * size)) == NULL){ printf("Memory allocation fail.\n"); exit(1); } for(i = 0; i<size; i++) array[i] = rand() % ; printf("Enter your guess (1-100):"); scanf("%d", &guess); for(i=0; i < size && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; if(!hit) printf("You guess a wrong number.\n"); free(array); Example Randomly generate the number of integers specified by the user for guess.
19
C Program Design C Data Structures
Linked Lists
20
Linked Lists 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 100 102 235 440 888
21
When use linked lists? You have an unpredictable number of data elements Your list needs to be sorted quickly Data comes and goes frequently 100 102 235 440 888
22
Nodes struct node { int data; struct node *nextNode; };
// start pointer of a list node* rootList; rootList 100 102 235 440 888
23
Example A sorted list
24
Example A sorted list // ListManagement.h typedef struct node {
int data; struct node *nextNode; } Node; Node* GetNode(); void FreeNode(Node* ptr); Node* Insert(Node* ptrRoot, int itemData); Node* Delete(Node* ptrRoot, int itemData); void PrintList(Node* ptrRoot); void FreeList(Node* ptrRoot);
25
Example A sorted list // ListManagement.c #include <stdlib.h>
#include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example A sorted list
26
Example A sorted list Node* GetNode() { Node *pNode;
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example A sorted list Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; }
27
Example A sorted list void FreeNode(Node* ptr) { free(ptr); }
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example A sorted list void FreeNode(Node* ptr) { free(ptr); }
28
Example A sorted list rootList 145 100 235 102 440 888
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example A sorted list 145 100 235 102 440 888 rootList
29
Example A sorted list rootList 145 100 102 235 440 888
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example A sorted list 145 rootList 100 102 235 440 888
30
Example A sorted list rootList 95 100 235 102 440 888
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example A sorted list 95 100 235 102 440 888 rootList
31
Example A sorted list rootList 95 100 102 235 440 888
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example A sorted list 95 rootList 100 102 235 440 888
32
Example A sorted list Node* Insert(Node* ptrRoot, int itemData) {
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Node* Insert(Node* ptrRoot, int itemData) { Node* pNodeInsert = GetNode(); Node* pCurNode, *pPreviousNode; pCurNode = ptrRoot; pPreviousNode = NULL; pNodeInsert->data = itemData; while(pCurNode != NULL && itemData > pCurNode->data){ pPreviousNode = pCurNode; pCurNode = pCurNode->nextNode; }; if(pPreviousNode != NULL){ pPreviousNode->nextNode = pNodeInsert; pNodeInsert->nextNode = pCurNode; return ptrRoot; } else{ return pNodeInsert; Example A sorted list
33
Example A sorted list rootList Delete(rootList, 235) 100 235 102 440
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example 100 235 102 440 888 rootList Delete(rootList, 235) A sorted list
34
Example A sorted list rootList Delete(rootList, 235) 100 102 235 440
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example rootList Delete(rootList, 235) A sorted list 100 102 235 440 888
35
Example A sorted list rootList Delete(rootList, 100) 100 235 102 440
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example 100 235 102 440 888 rootList Delete(rootList, 100) A sorted list
36
Example A sorted list rootList Delete(rootList, 100) 100 102 235 440
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example rootList Delete(rootList, 100) A sorted list 100 102 235 440 888
37
Example A sorted list Node* Delete(Node* ptrRoot, int itemData) {
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Node* Delete(Node* ptrRoot, int itemData) { Node *pCurNode, *pPreviousNode; pCurNode = ptrRoot; pPreviousNode = NULL; while(pCurNode != NULL && itemData != pCurNode->data){ pPreviousNode = pCurNode; pCurNode = pCurNode->nextNode; }; if(pCurNode == NULL) return ptrRoot; if(pPreviousNode != NULL){ pPreviousNode->nextNode = pCurNode->nextNode; FreeNode(pCurNode); return ptrRoot; } else{ ptrRoot = pCurNode->nextNode; Example A sorted list
38
Example A sorted list void PrintList(Node* ptrRoot) {
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) void PrintList(Node* ptrRoot) { while(ptrRoot != NULL){ printf("%d%s", ptrRoot->data, ptrRoot->nextNode ? "-->" : ""); ptrRoot = ptrRoot->nextNode; } printf("\n"); Example A sorted list
39
Example A sorted list void FreeList(Node* ptrRoot) { Node* pTemp;
// ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) Node* Insert(Node* ptrRoot, int itemData) Node* Delete(Node* ptrRoot, int itemData) void PrintList(Node* ptrRoot) void FreeList(Node* ptrRoot) Example A sorted list void FreeList(Node* ptrRoot) { Node* pTemp; while(ptrRoot != NULL){ pTemp = ptrRoot->nextNode; FreeNode(ptrRoot); ptrRoot = pTemp; }
40
Example A sorted list //main.c #include <stdio.h>
#include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: Example A sorted list
41
Example A sorted list printf("Enter data for insertion:");
//main.c #include <stdio.h> #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: Example A sorted list printf("Enter data for insertion:"); scanf("%d", &data); rootList = Insert(rootList, data); PrintList(rootList); break;
42
Example A sorted list printf("Enter data for deletion:");
//main.c #include <stdio.h> #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: Example A sorted list printf("Enter data for deletion:"); scanf("%d", &data); rootList = Delete(rootList, data); PrintList(rootList); break;
43
Example A sorted list FreeList(rootList); end = 1; break; //main.c
#include <stdio.h> #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: Example A sorted list FreeList(rootList); end = 1; break;
44
Example A sorted list This is a non-recursive version.
ListManagement.h ListManagement.c Main.c LinkList.exe
45
RecursiveListManagement.c
Recursive version. Example A sorted list ListManagement.h RecursiveListManagement.c Main.c LinkList.exe
46
C Program Design C Data Structures
Stacks
47
Stacks Stack New nodes can be added and removed only at the top Similar to a pile of dishes Last-in, first-out (LIFO) Can be implemented using array or 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 Push Pop
48
Stack Implementation Array
Memory stack . sizeStack stack = (int*) malloc(sizeof(int) * sizeStack); sp = stack + sizeStack; numItems = 0; numItems=0 sp
49
Stack Implementation Array
Push(50); Memory stack . sizeStack numItems=1 sp 50
50
Stack Implementation Array
Push(50); Memory stack Push(41); . sizeStack numItems=2 sp 41 50
51
Stack Implementation Array
Push(50); Memory stack Push(41); . sizeStack Push(38); numItems=3 sp 38 41 50
52
Stack Implementation Array
Push(50); Memory stack Push(41); . sizeStack Push(38); int Push(int val) { if(numItems==sizeStack){ return 0; } else{ *--sp = val; numItems++; return 1; numItems=3 sp 38 41 50
53
Stack Implementation Array
Push(50); Memory stack Push(41); . sizeStack Push(38); Pop(&data); data38 numItems=2 38 38 sp 41 50
54
Stack Implementation Array
Push(50); Memory stack Push(41); . sizeStack Push(38); Pop(&data); data38 int Pop(int* ptrData) { if(numItems==0){ return 0; } else{ *ptrData = *sp++; numItems--; return 1; numItems=2 38 38 sp 41 50
55
Stack Implementation Array
Memory stack . sizeStack arrayStack.c numItems=2 38 38 arrayStack.exe sp 41 50
56
Stack Implementation Linked List
45 62 27 155 42 stack
57
Stack Implementation Linked List
Push(&stack, 145) 145 stack 45 27 62 155 42
58
Stack Implementation Linked List
Push(&stack, 888) 888 145 stack 45 27 62 155 42
59
Stack Implementation Linked List
data888 Stack Implementation Linked List Pop(&stack, &data) 888 145 stack 45 27 62 155 42
60
Stack Implementation Linked List
data888 Stack Implementation Linked List Pop(&stack, &data) 145 stack 45 27 62 155 42
61
Stack Implementation Linked List
// stacknode.h typedef struct node { int data; struct node *nextNode; } Node; Node* GetNode(); void FreeNode(Node*); void Push(Node**, int); int Pop(Node**, int*); int IsStackEmpty(); void PrintStack(Node*); void FreeStack(Node*);
62
Stack Implementation Linked List
#include <stdlib.h> #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) void Push(Node** ptrStack, int val) int Pop(Node** ptrStack, int* ptrData) int IsStackEmpty(Node* stack) return stack == NULL; void PrintStack(Node* stack) void FreeStack(Node* stack) Stack Implementation Linked List
63
Stack Implementation Linked List
#include <stdlib.h> #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) void Push(Node** ptrStack, int val) int Pop(Node** ptrStack, int* ptrData) int IsStackEmpty(Node* stack) return stack == NULL; void PrintStack(Node* stack) void FreeStack(Node* stack) Stack Implementation Linked List Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; }
64
Stack Implementation Linked List
#include <stdlib.h> #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) void Push(Node** ptrStack, int val) int Pop(Node** ptrStack, int* ptrData) int IsStackEmpty(Node* stack) return stack == NULL; void PrintStack(Node* stack) void FreeStack(Node* stack) Stack Implementation Linked List void FreeNode(Node* ptr) { free(ptr); }
65
Stack Implementation Linked List
#include <stdlib.h> #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) void Push(Node** ptrStack, int val) int Pop(Node** ptrStack, int* ptrData) int IsStackEmpty(Node* stack) return stack == NULL; void PrintStack(Node* stack) void FreeStack(Node* stack) Stack Implementation Linked List void Push(Node** ptrStack, int val) { Node* pNode = GetNode(); if(NULL==pNode){ printf("No node available.\n"); return; } pNode->data = val; pNode->nextNode = *ptrStack; *ptrStack = pNode;
66
Stack Implementation Linked List
#include <stdlib.h> #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) void Push(Node** ptrStack, int val) int Pop(Node** ptrStack, int* ptrData) int IsStackEmpty(Node* stack) return stack == NULL; void PrintStack(Node* stack) void FreeStack(Node* stack) Stack Implementation Linked List int Pop(Node** ptrStack, int* ptrData) { Node* pNode; pNode = *ptrStack; if(pNode == NULL) return NULL; *ptrData = pNode->data; *ptrStack = pNode->nextNode; FreeNode(pNode); return 1; }
67
Stack Implementation Linked List
StackManagement.h StackManagement.c StackMain.c Stack.exe
68
Application: Reverse Polish Calculator
Infix notation: (1 - 2) * (4 + 5) Reverse Polish notation: *
69
Reverse Polish Calculator
* 1
70
Reverse Polish Calculator
* 2 1
71
Reverse Polish Calculator
* 2 1 - 2 -1 1
72
Reverse Polish Calculator
* 1 - 2 -1 -1
73
Reverse Polish Calculator
* 4 -1
74
Reverse Polish Calculator
* 5 4 -1
75
Reverse Polish Calculator
* 5 4 4 + 5 9 -1
76
Reverse Polish Calculator
* 9 9 4 + 5 -1
77
Reverse Polish Calculator
* -9 9 -1 * 9 -1
78
Reverse Polish Calculator
* -1 * 9 -9 -9
79
Reverse Polish Calculator
* -9 -9
80
C Program Design C Data Structures
Queues
81
Queues Queue Operations 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 Operations enqueue (insert) dequeue (remove)
82
Queue Implementation Array
Memory numItems = 0 queue . sizeQueue queueHead 1 2 queueTail queue = (int*) malloc(sizeof(int) * sizeQueue); queueHead = queueTail = 0; numItems = 0; sizeQueue - 1
83
Queue Implementation Array
Enqueue(35); Queue Implementation Array Memory numItems = 1 = 0 queue . 35 sizeQueue queueHead 1 2 queueTail int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } sizeQueue - 1
84
Queue Implementation Array
Enqueue(35); Enqueue(99); Queue Implementation Array Memory numItems = 2 = 1 queue . 35 sizeQueue queueHead 99 1 2 queueTail int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } sizeQueue - 1
85
Queue Implementation Array
Dequeue(&data); data35 Queue Implementation Array Memory numItems = 2 = 1 queue 35 . sizeQueue queueHead 99 1 2 queueTail int Dequeue(int* ptrData) { if(numItems==0) return 0; *ptrData = queue[queueHead++]; queueHead %= sizeQueue; numItems--; return 1; } sizeQueue - 1
86
Queue Implementation Array
arrayQueue.c arrayQueue.exe
87
Queue Implementation Linked List
typedef struct _queue { Node* queueHead; Node* queueTail; } Queue; 45 62 27 155 42 queueHead queueTail
88
Queue Implementation Linked List
Enqueue(&queue, 145) queueTail 145 queueHead 45 27 62 155 42
89
Queue Implementation Linked List
Enqueue(&queue, 145) queueTail Enqueue(&queue, 67) 145 67 queueHead 45 27 62 155 42
90
Queue Implementation Linked List
Dequeue(&queue, &data) queueTail data45 145 67 queueHead 45 27 62 155 42
91
Queue Implementation Linked List
QueueManagement.h QueueManagement.c ListQueue.c ListQueue.exe
92
C Program Design C Data Structures
Trees
93
Trees Tree nodes contain two or more links Binary trees
root 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
94
Trees Tree nodes contain two or more links Binary trees
root 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
95
Binary Trees TL TR Data Root left right
struct treeNode { int data; struct treeNode *left; struct treeNode *right; }; TL TR Data left right
96
Binary Search Trees root
97
Tree Traversals Infix: A B C D E F G H I Prefix: F B A D C E G I H
Postfix: A C E D B H I G F
98
Tree Traversals
99
Binary Tree Implementation
// TreeManagement.h typedef struct treeNode { int data; struct treeNode* left; struct treeNode* right; } TreeNode; TreeNode* GetTreeNode(); void FreeTreeNode(TreeNode*); void FreeTree(TreeNode*); void InsertTree(TreeNode**, int); int BinSearch(TreeNode*, int); void DeleteTree(TreeNode**, int); void InfixOrder(TreeNode*); void PrefixOrder(TreeNode*); void PostfixOrder(TreeNode*); Tree node maintenance Main operations Tree Traversal
100
Binary Tree Implementation
TreeManagement.h TreeManagement.c TreeMain.c Tree.exe
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.