1 / 100

C Program Design C Data Structures

C Program Design C Data Structures. 主講人:虞台文. Content. Introduction Self-Referential Structures Dynamic Memory Allocation Linked Lists Stacks Queues Trees. C Program Design C Data Structures. Introduction. Introduction. Dynamic data structures

mance
Download Presentation

C Program Design C Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Program DesignC Data Structures 主講人:虞台文

  2. Content • Introduction • Self-Referential Structures • Dynamic Memory Allocation • Linked Lists • Stacks • Queues • Trees

  3. C Program DesignC Data Structures Introduction

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

  5. C Program DesignC Data Structures Self-Referential Structures

  6. List 100 102 235 440 888 List Linked Lists List List List

  7. 100 102 235 440 888 Linked Lists struct node { int data;struct node *nextNode;}; Structure that contains a pointer to a structure of the same type.

  8. 300 140 750 119 282 405 809 230 290 510 760 900 struct treeNode { int data;struct treeNode *left;struct treeNode *right;}; Tree

  9. C Program DesignC 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. #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. #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() % 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"); } } Example Randomly generate 50 integers for guess.

  16. #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() % 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"); } } 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() % 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); } } 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() % 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); } } Example Randomly generate the number of integers specified by the user for guess.

  19. C Program DesignC Data Structures Linked Lists

  20. 100 102 235 440 888 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

  21. 100 102 235 440 888 When use linked lists? • You have an unpredictable number of data elements • Your list needs to be sorted quickly • Data comes and goes frequently

  22. 100 102 235 440 888 Nodes struct node { int data;struct node *nextNode;}; // start pointer of a list node* rootList; rootList

  23. Example A sorted list

  24. Example A sorted list // ListManagement.h typedef struct node { int data; struct node *nextNode; } Node; Node* GetNode(); voidFreeNode(Node* ptr); Node* Insert(Node* ptrRoot, int itemData); Node* Delete(Node* ptrRoot, int itemData); void PrintList(Node* ptrRoot); void FreeList(Node* ptrRoot);

  25. // 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. // 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. // 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 voidFreeNode(Node* ptr) { free(ptr); }

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

  29. 145 // 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 rootList 100 102 235 440 888

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

  31. 95 // 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 rootList 100 102 235 440 888

  32. // 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{ pNodeInsert->nextNode = pCurNode; return pNodeInsert; } } Example A sorted list

  33. rootList 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 Delete(rootList, 235) A sorted list

  34. 235 // 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 Delete(rootList, 235) rootList A sorted list 100 102 440 888

  35. rootList 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 Delete(rootList, 100) A sorted list

  36. 100 // 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 Delete(rootList, 100) rootList A sorted list 102 235 440 888

  37. // 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; FreeNode(pCurNode); return ptrRoot; } } Example A sorted list

  38. // 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. // 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. //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. //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. //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. //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. This is a non-recursive version. Example A sorted list ListManagement.h ListManagement.c Main.c LinkList.exe

  45. Recursive version. Example A sorted list ListManagement.h RecursiveListManagement.c Main.c LinkList.exe

  46. C Program DesignC Data Structures Stacks

  47. Pop Push 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

  48. stack . . . Memory sizeStack sp Stack Implementation  Array stack = (int*) malloc(sizeof(int) * sizeStack); sp = stack + sizeStack; numItems = 0; numItems=0

  49. stack . . . Memory sizeStack sp Stack Implementation  Array Push(50); numItems=1 50

  50. stack . . . Memory sizeStack sp Stack Implementation  Array Push(50); Push(41); numItems=2 41 50

More Related