Download presentation
Presentation is loading. Please wait.
Published byCharity Rose Modified over 9 years ago
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
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
5
C Program Design C Data Structures Self-Referential Structures
6
Linked Lists 100235102440888 List
7
Linked Lists struct node { int data; struct node *nextNode; }; Structure that contains a pointer to a structure of the same type. 100235102440888
8
Tree 300 140 282119 230290 750 405 510 809 760900 struct treeNode { int data; struct treeNode *left; struct treeNode *right; };
9
C Program Design C Data Structures Dynamic Memory Allocation
10
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 #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 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; } #include 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; } Generate random string of desired length.
15
Example #include 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() % 100 + 1; 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"); } #include 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() % 100 + 1; 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"); } } Randomly generate 50 integers for guess.
16
Example #include 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() % 100 + 1; 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"); } #include 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() % 100 + 1; 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"); } Randomly generate 50 integers for guess.
17
Example #include 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() % 100 + 1; 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); } #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() % 100 + 1; 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); } } Randomly generate the number of integers specified by the user for guess.
18
Example #include 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() % 100 + 1; 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); } #include 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() % 100 + 1; 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); } Randomly generate the number of integers specified by the user for guess.
19
C Program Design C Data Structures Linked Lists
20
100235102440888 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
21
When use linked lists? 100235102440888 You have an unpredictable number of data elements Your list needs to be sorted quickly Data comes and goes frequently
22
Nodes 100235102440888 struct node { int data; struct node *nextNode; }; // start pointer of a list node* rootList; struct node { int data; struct node *nextNode; }; // start pointer of a list node* rootList; rootList
23
Example A sorted list
24
Example // 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); // 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); A sorted list
25
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list
26
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; } Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; }
27
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list void FreeNode(Node* ptr) { free(ptr); } void FreeNode(Node* ptr) { free(ptr); }
28
Example // ListManagement.c #include #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) {............................ } // 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) {............................ } A sorted list 100235102440888 rootList 145
29
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list 100235102440888 rootList 145
30
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list 100235102440888 rootList 95
31
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list 100235102440888 rootList 95
32
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list 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{ pNodeInsert->nextNode = pCurNode; return pNodeInsert; } 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{ pNodeInsert->nextNode = pCurNode; return pNodeInsert; } }
33
Example // ListManagement.c #include #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) {............................ } // 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) {............................ } A sorted list 100235102440888 rootList Delete(rootList, 235)
34
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list 100102440888 rootList 235 Delete(rootList, 235)
35
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list 100235102440888 rootList Delete(rootList, 100)
36
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list 235102440888 rootList 100 Delete(rootList, 100)
37
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list 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; FreeNode(pCurNode); return 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; FreeNode(pCurNode); return ptrRoot; } }
38
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list void PrintList(Node* ptrRoot) { while(ptrRoot != NULL){ printf("%d%s", ptrRoot->data, ptrRoot->nextNode ? "-->" : ""); ptrRoot = ptrRoot->nextNode; } printf("\n"); } void PrintList(Node* ptrRoot) { while(ptrRoot != NULL){ printf("%d%s", ptrRoot->data, ptrRoot->nextNode ? "-->" : ""); ptrRoot = ptrRoot->nextNode; } printf("\n"); }
39
Example // ListManagement.c #include #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) {............................ } // ListManagement.c #include #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) {............................ } A sorted list void FreeList(Node* ptrRoot) { Node* pTemp; while(ptrRoot != NULL){ pTemp = ptrRoot->nextNode; FreeNode(ptrRoot); ptrRoot = pTemp; } void FreeList(Node* ptrRoot) { Node* pTemp; while(ptrRoot != NULL){ pTemp = ptrRoot->nextNode; FreeNode(ptrRoot); ptrRoot = pTemp; } }
40
Example A sorted list //main.c #include #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:................... } //main.c #include #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:................... } } }
41
Example A sorted list //main.c #include #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:................... } //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:................... } } } printf("Enter data for insertion:"); scanf("%d", &data); rootList = Insert(rootList, data); PrintList(rootList); break; printf("Enter data for insertion:"); scanf("%d", &data); rootList = Insert(rootList, data); PrintList(rootList); break;
42
Example A sorted list //main.c #include #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:................... } //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:................... } } } printf("Enter data for deletion:"); scanf("%d", &data); rootList = Delete(rootList, data); PrintList(rootList); break; printf("Enter data for deletion:"); scanf("%d", &data); rootList = Delete(rootList, data); PrintList(rootList); break;
43
Example A sorted list //main.c #include #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:................... } //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:................... } } } FreeList(rootList); end = 1; break; FreeList(rootList); end = 1; break;
44
Example A sorted list ListManagement.h ListManagement.c Main.c LinkList.exe This is a non-recursive version.
45
Example A sorted list ListManagement.h RecursiveListManagement.c Main.c LinkList.exe Recursive version.
46
C Program Design C Data Structures Stacks
47
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
Memory Stack Implementation Array sizeStack sp stack numItems=0...... stack = (int*) malloc(sizeof(int) * sizeStack); sp = stack + sizeStack; numItems = 0; stack = (int*) malloc(sizeof(int) * sizeStack); sp = stack + sizeStack; numItems = 0;
49
Memory Stack Implementation Array sizeStack sp stack numItems=1...... Push(50); 50
50
Memory Stack Implementation Array sizeStack sp stack numItems=2...... Push(50); 50 Push(41); 41
51
Memory Stack Implementation Array sizeStack sp stack numItems=3...... Push(50); 50 Push(41); 41 Push(38); 38
52
Memory Stack Implementation Array sizeStack sp stack numItems=3...... Push(50); 50 Push(41); 41 Push(38); 38 int Push(int val) { if(numItems==sizeStack){ return 0; } else{ *--sp = val; numItems++; return 1; } int Push(int val) { if(numItems==sizeStack){ return 0; } else{ *--sp = val; numItems++; return 1; } }
53
Memory Stack Implementation Array sizeStack sp stack numItems=2...... Push(50); 50 Push(41); 41 Push(38); 38 Pop(&data); 38 data 38
54
Memory Stack Implementation Array sizeStack sp stack numItems=2...... Push(50); 50 Push(41); 41 Push(38); 38 Pop(&data); 38 data 38 int Pop(int* ptrData) { if(numItems==0){ return 0; } else{ *ptrData = *sp++; numItems--; return 1; } int Pop(int* ptrData) { if(numItems==0){ return 0; } else{ *ptrData = *sp++; numItems--; return 1; } }
55
Memory Stack Implementation Array sizeStack sp stack numItems=2...... 50 41 38 arrayStack.c arrayStack.exe
56
Stack Implementation Linked List 45622715542 stack
57
Stack Implementation Linked List 45622715542 stack 145 Push(&stack, 145)
58
Stack Implementation Linked List 45622715542 stack 145 Push(&stack, 888) 888
59
Stack Implementation Linked List 45622715542 stack 145 Pop(&stack, &data) 888 data 888
60
Stack Implementation Linked List 45622715542 stack 145 Pop(&stack, &data) data 888
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*); // 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 #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) {............... } #include #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) {............... }
63
Stack Implementation Linked List #include #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) {............... } #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) {............... } Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; } Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; }
64
Stack Implementation Linked List #include #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) {............... } #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) {............... } void FreeNode(Node* ptr) { free(ptr); } void FreeNode(Node* ptr) { free(ptr); }
65
Stack Implementation Linked List #include #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) {............... } #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) {............... } 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; } 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 #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) {............... } #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) {............... } 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; } 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
StackManagement.h StackManagement.c StackMain.c Stack.exe Stack Implementation Linked List
68
Application: Reverse Polish Calculator Infix notation: (1 - 2) * (4 + 5) Reverse Polish notation: 1 2 - 4 5 + *
69
Reverse Polish Calculator 1 2 - 4 5 + * 1
70
Reverse Polish Calculator 1 2 - 4 5 + * 1 2
71
Reverse Polish Calculator 1 2 - 4 5 + * 1 21 2- -1
72
Reverse Polish Calculator 1 2 - 4 5 + * 1 2- -1
73
Reverse Polish Calculator 1 2 - 4 5 + * 4
74
Reverse Polish Calculator 1 2 - 4 5 + * 4 5
75
Reverse Polish Calculator 1 2 - 4 5 + * 4 5 45 + 9
76
Reverse Polish Calculator 1 2 - 4 5 + * 9 45 + 9
77
Reverse Polish Calculator 1 2 - 4 5 + * 9 9 * -9
78
Reverse Polish Calculator 1 2 - 4 5 + * -9 9 * -9
79
Reverse Polish Calculator 1 2 - 4 5 + * -9
80
C Program Design C Data Structures Queues
81
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 Operations – enqueue (insert) – dequeue (remove)
82
Memory Queue Implementation Array sizeQueue queueHead queue numItems...... queue = (int*) malloc(sizeof(int) * sizeQueue); queueHead = queueTail = 0; numItems = 0; queue = (int*) malloc(sizeof(int) * sizeQueue); queueHead = queueTail = 0; numItems = 0; queueTail = 0 0 1 2 sizeQueue - 1
83
Memory Queue Implementation Array sizeQueue queueHead queue...... int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } queueTail 0 1 2 sizeQueue - 1 Enqueue(35); 35 numItems = 0= 1
84
Memory Queue Implementation Array sizeQueue queueHead queue...... int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } queueTail 0 1 2 sizeQueue - 1 Enqueue(35); 35 numItems Enqueue(99); 99 = 2
85
= 1 Memory Queue Implementation Array sizeQueue queueHead queue...... int Dequeue(int* ptrData) { if(numItems==0) return 0; *ptrData = queue[queueHead++]; queueHead %= sizeQueue; numItems--; return 1; } int Dequeue(int* ptrData) { if(numItems==0) return 0; *ptrData = queue[queueHead++]; queueHead %= sizeQueue; numItems--; return 1; } queueTail 0 1 2 sizeQueue - 1 Dequeue(&data); 35 numItems 99 data 35
86
Queue Implementation Array arrayQueue.c arrayQueue.exe
87
Queue Implementation Linked List 45622715542 queueHead queueTail typedef struct _queue { Node* queueHead; Node* queueTail; } Queue; typedef struct _queue { Node* queueHead; Node* queueTail; } Queue;
88
Queue Implementation Linked List 45622715542 queueHead queueTail Enqueue(&queue, 145) 145
89
Queue Implementation Linked List 45622715542 queueHead queueTail Enqueue(&queue, 145) 145 Enqueue(&queue, 67) 67
90
Queue Implementation Linked List 45 622715542 queueHead queueTail 145 Dequeue(&queue, &data) 67 data 45
91
Queue Implementation Linked List QueueManagement.h QueueManagement.c ListQueue.c ListQueue.exe
92
C Program Design C Data Structures Trees
93
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 root
94
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 root
95
Binary Trees Root TLTL TRTR root struct treeNode { int data; struct treeNode *left; struct treeNode *right; }; Data leftright
96
Binary Search Trees root
97
Tree Traversals A B F C D H I G E Infix: Prefix: Postfix: ABCDEFGHI FBADCEGIH ACEDBHIGF
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*); // 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.