1k likes | 1.02k Views
Learn self-referential structures, dynamic memory allocation, linked lists, stacks, queues, & binary trees in C programming. Understand malloc, free functions, memory management, and efficient data structure handling.
E N D
Content • Introduction • Self-Referential Structures • Dynamic Memory Allocation • Linked Lists • Stacks • Queues • Trees
C Program DesignC Data Structures Introduction
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
C Program DesignC Data Structures Self-Referential Structures
List 100 102 235 440 888 List Linked Lists List List List
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.
300 140 750 119 282 405 809 230 290 510 760 900 struct treeNode { int data;struct treeNode *left;struct treeNode *right;}; Tree
C Program DesignC Data Structures Dynamic Memory Allocation
Dynamic Memory Allocation • Obtain and release memory during execution • Allocate Memory • Release Memory void *malloc(size_t size); void free(void *ptr);
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.
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.
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
#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.
#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.
#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.
#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.
#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.
C Program DesignC Data Structures Linked Lists
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
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
100 102 235 440 888 Nodes struct node { int data;struct node *nextNode;}; // start pointer of a list node* rootList; rootList
Example A sorted list
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);
// 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
// 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; }
// 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); }
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 // 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
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 // 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
// 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
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
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
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
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
// 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
// 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
// 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; } }
//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
//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;
//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;
//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;
This is a non-recursive version. Example A sorted list ListManagement.h ListManagement.c Main.c LinkList.exe
Recursive version. Example A sorted list ListManagement.h RecursiveListManagement.c Main.c LinkList.exe
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
stack . . . Memory sizeStack sp Stack Implementation Array stack = (int*) malloc(sizeof(int) * sizeStack); sp = stack + sizeStack; numItems = 0; numItems=0
stack . . . Memory sizeStack sp Stack Implementation Array Push(50); numItems=1 50
stack . . . Memory sizeStack sp Stack Implementation Array Push(50); Push(41); numItems=2 41 50