Sie sind auf Seite 1von 31

Dynamic data structures

#define NUMBEROFPOINTS 20

int xy[NUMBEROFPOINTS][2];

char string[20];

12-1

Dynamic memory allocation

malloc()

free()

12-2

syntax
Syntax #include <stdlib.h> or #include<alloc.h> void *malloc(size_t size); Description malloc allocates a block of size bytes from the memory heap. It allows a program to allocate memory explicitly as it's needed, and in the exact amounts needed. Allocates main memory. The heap is used for dynamic allocation of variable-sized blocks of memory. Many data structures, for example, trees and lists, naturally employ heap memory allocation. Return Value On success, malloc returns a pointer to the newly allocated block of memory. If not enough space exists for the new block, it returns NULL. The contents of the block are left unchanged. If the argument size == 0, malloc returns NULL.
12-3

syntax
Syntax #include <stdlib.h> void free(void *block); Description Frees allocated block. free deallocates a memory block allocated by a previous call to calloc, malloc, or realloc. Return Value

None.
12-4

malloc - example
#include <stdio.h> #include <stdlib.h>
int main() { char *ptrC; ptrC=(char *)malloc(20* sizeof(char)); if(ptrC==NULL) { printf("Out of memory!\n"); return(0); } scanf("%s", ptrC); printf("%s", ptrC); free(ptrC); return(0); } #include <stdio.h> int main() { char C[20];

scanf("%s", C); printf("%s", C);


return(0); }

12-5

syntax
The sizeof operator has two distinct uses: sizeof unary-expression sizeof (type-name) The result in both cases is an integer constant that gives the size in bytes of how much memory space is used by the operand (determined by its type, with some exceptions). The amount of space that is reserved for each type depends on the machine.

12-6

#include <stdio.h> #include <stdlib.h>

Example

struct points { int x, y, z; };


int main() { struct points *path; int numberofpoints; printf("Enter number of points:"); scanf("%d", &numberofpoints); if((path=(struct points *) malloc(sizeof(points)*numberofpoints))==NULL) return(0); ... ... free(path); return(0); }

12-7

Linked lists
ptrLinkedList xxx * list

xxx * list

xxx * list

xxx * list

xxx * list

NULL

* list

12-8

Example - definition
struct onenode { int data; struct onenode *nextNode; };

12-9

EXAMPLE
LINKED LIST

12-10

#include <stdio.h> #include <stdlib.h> struct onenode { int data; struct onenode *nextNode; }; typedef struct onenode * ptrOneNode; void remove(ptrOneNode *ptrList, int data); void insert(ptrOneNode *ptrList, int data); void printlist(ptrOneNode list); int main() { ptrOneNode linkedlist=NULL; insert(&linkedlist, 1); insert(&linkedlist, 2); insert(&linkedlist, 3); remove(&linkedlist, 2); printlist(linkedlist); return(0); }

12-11

void insert(ptrOneNode *ptrList, int data) { ptrOneNode aNewNode, currentNode=*ptrList; if((aNewNode=(ptrOneNode)malloc(sizeof(onenode)))==NULL) return; aNewNode->data=data; aNewNode->nextNode=NULL; if(*ptrList==NULL) { *ptrList=aNewNode; return; } while(currentNode->nextNode!=NULL) currentNode=currentNode->nextNode; currentNode->nextNode=aNewNode; }
12-12

void remove(ptrOneNode *ptrList, int data) { ptrOneNode currentNode=*ptrList, tmp;


if(*ptrList==NULL) return; if(currentNode->data==data) { *ptrList=currentNode->nextNode; free(currentNode); return; } while(currentNode->nextNode!=NULL) { if(currentNode->nextNode->data==data) { tmp=currentNode->nextNode; currentNode->nextNode=currentNode->nextNode->nextNode; free(tmp); return; } currentNode=currentNode->nextNode; } }

12-13

void printlist(ptrOneNode list) { while(list!=NULL) { printf("data=%d\n", list->data); list=list->nextNode; } }

12-14

NOT AGAIN!
YES THE SAME EXMAPLE AGAIN!

12-15

#include <stdio.h> #include <stdlib.h> struct onenode { int data; struct onenode *nextNode; }; typedef struct onenode * ptrOneNode; void remove(ptrOneNode ptrList, int data); void insert(ptrOneNode ptrList, int data); void printlist(ptrOneNode list); int main() { struct onenode linkedlist; linkedlist.nextNode=NULL; insert(&linkedlist, 1); insert(&linkedlist, 2); insert(&linkedlist, 3); remove(&linkedlist, 2); printlist(&linkedlist); return(0); }

12-16

void insert(ptrOneNode ptrList, int data) { ptrOneNode aNewNode; if((aNewNode=(ptrOneNode)malloc(sizeof(onenode)))==NULL) return; aNewNode->data=data; aNewNode->nextNode=NULL; while(ptrList->nextNode!=NULL) ptrList=ptrList->nextNode; ptrList->nextNode=aNewNode; }

12-17

void remove(ptrOneNode ptrList, int data) { ptrOneNode tmp;


while(ptrList->nextNode!=NULL) { if(ptrList->nextNode->data==data) { tmp=ptrList->nextNode; ptrList->nextNode=ptrList->nextNode->nextNode; free(tmp); return; } ptrList=ptrList->nextNode; } }
12-18

void printlist(ptrOneNode list) { list=list->nextNode; while(list!=NULL) { printf("data=%d\n", list->data); list=list->nextNode; } }

12-19

Stack - LIFO
Data
Data Data Data

PUSH

POP

12-20

#include <stdio.h> #include <stdlib.h>

Example -Stack

struct tStack { int data; struct tStack * nextptr; }; typedef struct tStack stack; int pop(stack **stackptr); void push(stack **stackptr, int data); int main() { stack *stackptr=NULL;

push(&stackptr, 1); push(&stackptr, 2); push(&stackptr, 3); printf("%d %d %d", pop(&stackptr), pop(&stackptr), pop(&stackptr));
}
12-21

void push(stack **stackptr, int data) { stack *tmp;


if((tmp=(stack *)malloc(sizeof(stack)))!=NULL) { tmp->nextptr=*stackptr; tmp->data=data; *stackptr=tmp; } } int pop(stack **stackptr) { stack *tmp; int data; if((tmp=*stackptr)==NULL) return(0); data=tmp->data; *stackptr=(*stackptr)->nextptr; free(tmp); return(data); }

12-22

Queues - FIFO

In

Data

Data

Data

Data

Out

12-23

Implementation
headPtr tailPtr

xxx *

yyy *

Data

Data

Data

12-24

Trees
Data

Data

Data

Data

Data

Data

Data

12-25

Trees Search example


47

25

77

11

43

65

93

17

31

44

68

12-26

#include <stdio.h> #include <stdlib.h> #define CW 6 struct tTree { int data; struct tTree *left, *right; }; typedef struct tTree tree; tree *find(tree *treePtr, int data); void insertBranch(tree **treePtr, int data); void printTree(tree *treePtr, int col); int main() { tree *treePtr=NULL, *tmp; int i; for(i=0; i!=17; i++) insertBranch(&treePtr, rand()%100); printTree(treePtr, 0); find(treePtr, 12); if((tmp=find(treePtr, 4))!=NULL) printf("Found %d\n", tmp->data); return(0); } 12-27

void insertBranch(tree **treePtr, int data) { if(*treePtr==NULL) { if((*treePtr=(tree *)malloc(sizeof(tree)))!=NULL) { (*treePtr)->data=data; (*treePtr)->left=NULL; (*treePtr)->right=NULL; } } else if(data<(*treePtr)->data) insertBranch(&((*treePtr)->left), data); else if(data>(*treePtr)->data) insertBranch(&((*treePtr)->right), data); }

12-28

tree *find(tree *treePtr, int data) { if(data>treePtr->data && treePtr->right!=NULL) return(find(treePtr->right, data)); else if(data<treePtr->data && treePtr->left!=NULL) return(find(treePtr->left, data)); else if(data==treePtr->data) return(treePtr);
return(NULL); }

12-29

void printTree(tree *treePtr, int col) { int i; if(treePtr->right!=NULL) printTree(treePtr->right, col+CW); for(i=0; i!=col; i++) printf(" "); printf("[%2d]\n\n", treePtr->data); if(treePtr->left!=NULL) printTree(treePtr->left, col+CW); }

12-30

Exercises
A 12.8, 12.9, 12.10, 12.23

B 12.15, 12.19

12.12, 12.13
12-31

Das könnte Ihnen auch gefallen