Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming : Elementary Data Structures 2009/04/22 Jaemin

Similar presentations


Presentation on theme: "C Programming : Elementary Data Structures 2009/04/22 Jaemin"— Presentation transcript:

1 C Programming : Elementary Data Structures 2009/04/22 Jaemin Soh(jmsoh@paradise.kaist.ac.kr)

2 Contents Data Structure Stack Queue Dynamic Memory Allocation Linked List Tree 2

3 Data Structure Data + Structure Data Handling rule Point Input Output Properties Examples Stack Queue Tree 3

4 Stack Storage layout: Last-In, First-Out (LIFO) Primitive operations: push, pop, isEmpty, isFull 4 82 Stack: isEmpty() == true 17 8 821 push(8) push(2); push(1); push(7); isFull() == true Stack grows pop() == 7 Stack grows

5 Stack Implementation(1/3) A simple implementation in C Use an array to hold elements stored in a stack. “top” points the most recently pushed element. Primitive functions int isEmpty(); int isFull(); void push(int x); int pop(); 5 Stack: 8 top

6 Stack Implementation(2/3) 6 int isEmpty() { if (top<0) return 1; /* true */ else return 0; /* false */ } int isFull() { return ( (top==7) ? 1 : 0 ); } /* Stack */ int stack[8]; /* top refers the array index of the most recently pushed item */ int top = -1;

7 Stack Implementation(3/3) 7 void push(int x) { if (top>=7) { /* STACK OVERFLOW */ return; } stack[++top] = x; /* top refers the most recently pushed item */ return; } int pop() { int err = -2147483647; if (top<0) { /* STACK IS EMPTY */ return err; } return stack[top--]; }

8 Queue Storage layout: First-Come, First-Served (FCFS) Primitive operations: put, get, isEmpty, isFull 8 82 Queue: isEmpty() == true 17 8 721 put(8) put(2); put(1); put(7); isFull() == true Queue grows get() == 8

9 Queue Implementation(1/4) A simple implementation in C Use an array to hold elements stored in a queue. “head” points the next empty slot. “tail” points the oldest element. Primitive functions int isEmpty(); int isFull(); void put(int x); int get(); 9 Queue: 8217 tailhead

10 Queue Implementation(2/4) Circular Queue 10 8217123 get() == 8; get() == 2; get() == 1; 7123821 put(14); put(11); 7123 1411 head=1tail=3 7123 1411 head=1tail=4 0 1 2 3 4 5 6 7 get() == 7; tail=3head=7 tail=0 head=7

11 Queue Implementation(3/4) 11 int isEmpty() { if (head==tail) return 1; else return 0; } int isFull() { if (((head+1) % 8) == tail) return 1; else return 0; } /* Circular queue */ int queue[8]; /* head refers the next empty slot */ int head = 0; /* tail refers the oldest element */ int tail = 0;

12 Queue Implementation(4/4) 12 void put(int x) { if (is_full()) { /* QUEUE OVERFLOW */ return; } queue[head] = x; head = (head+1) % 8; /* if (head == 7) head = 0; else head++; */ } int get() { int err = -2147483647; int ret; if (is_empty()) { /* STACK IS EMPTY */ return err; } ret = queue[tail]; tail = (tail+1) % 8; return ret; }

13 C Array The allocated space is not flexible Data insertion/deletion is difficult 13 1234567 index: 01 2 3 4 5 6 7 8 910 ?

14 Dynamic Memory Allocation Static Allocation Determined when program starts. Dynamic Memory Allocation Determined during the running time. 14

15 Dynamic Memory Allocation Synopsis Allocates size bytes and returns a pointer to the allocated memory. 15 int *pi; int size, i; scanf(“%d”, &size); pi = (int *)malloc(sizeof(int) * size); // (type): static type casting for (i = 0; i < size; i++)// Initialization pi[i] = -1; void* malloc(size_t size) /* : malloc/calloc/realloc */

16 Dynamic Memory Deallocation Synopsis Frees the memory space pointed by ptr. Frees(Deallocates) the dynamically allocated memory space 16 void free(void *ptr) /* : free */ int *pi; int size, i; scanf(“%d”, &size); pi = (int *)malloc(sizeof(int) * size); if (pi != NULL) { free(pi); pi = NULL; }

17 Linked List An alternative to array. Data structure in which objects are arranged in a linear order Consists of nodes, each containing arbitrary data fields and link pointing to the next nodes. The size of a linked list would be changed in runtime. 17 872717 Node LinkData field HEAD X

18 Linked List Implementation An node is represented in a C structure. malloc() is used to dynamically create node structure 18 typedef struct node_t { int data; struct _node_t *next; } node_t; node_t head; head.next = NULL; /* Linked list is empty */ node_t* create_node(int d) { node_t *n = (node_t*)malloc( sizeof(node_t)); if (!n) return NULL; n->data = d; n->next = NULL; return n; }

19 Primitive Functions - Search 19 void search (int d) { node_t *p; for (p = head.next; p != NULL ; p = p->next) { if (p->data == d) break; } if (p == NULL) return NULL; else return p; }

20 Primitive Functions – Insert 20 821 prev HEAD X originally prev  next n void insert (node_t *prev, node_t *n) { node_t *next = prev->next; prev->next = n; n->next = next; }

21 Primitive Functions – Delete 21 int delete (node_t *d) { int x = d->data; node_t *prev; for (prev = &head; prev->next != d; prev = prev->next) /* DO Nothing */ ; prev->next = d->next; free(d); return x; } 821 prev HEAD X originally d  nextd

22 Doubly Linked List Has two links pointing to the next and previous nodes 22 Next Prev HEAD 8 821

23 Doubly Linked List Implementation 23 void insert (node_t *prev, node_t *n) { node_t *next = prev->next; prev->next = n; n->prev = prev; n->next = next; next->prev = n; } int delete (node_t *d) { node_t *prev = d->prev; node_t *next = d->next; int x = d->data; prev->next = next; next->prev = prev; free(d); return x; }

24 Array vs. Linked List 24 About memory savings? ArrayLinked List Indexing (random access)O(1)O(n) Inserting / Deleting at endO(1)O(1) or O(n) Inserting / Deleting at middleO(n)O(1) ResizeDifficultEasy LocalityGreatBad

25 Tree Connected, acyclic, and undirected graph. Binary tree, heap tree, B+ tree, etc… 25 5 27 146 3 Root node Leaf node Parent Child

26 Binary Tree Each node has at most two children. Implementation An node is represented as a structure. Use malloc() to dynamically create an node structure. 26 root typedef struct _node { int data ; /*struct _node *parent ; */ struct _node *left ; struct _node *right ; } node_t ; node_t *root = NULL; /* Tree is empty */ node_t* create_node (int d){ node_t *n; n = (node_t*)malloc( sizeof(node_t)); if (!n) return NULL; n->data = d; /* n->parent = NULL; */ n->left = NULL; n->right = NULL; return n; }

27 Binary Tree 27 4 26 13 57 26 1 null 3 5 4 7 right root left right root

28 Tree Primitive Functions 28 26 1 null 3 5 4 7 left right root node_t *search(int d); node_t *insert(int d); void delete(int d);

29 Tree Primitive Functions Depth-first search (DFS) One of the algorithm for traversing a tree or graph. 29 node_t *DFS(int d) { return _DFS(d, root); } node_t *_DFS(int d, node_t *cur){ node_t *n; if (cur == NULL) return NULL; else if (cur->data == d) return cur; n = _DFS(d, cur->left); if (n != NULL) return n; else return _DFS(d, cur->right); } 4 26 13 57 2 3 4 5 67 1

30 Lab #7 Implement ‘Binary tree’ With input numbers, make binary tree. In this case, left children must less than parent, and right greater. There is no same number in the input sequence. Input is entered by keyboard. (use scanf()) Implement with linked list scheme. Print the list in the increasing order Answer that given number is in which level Function declaration void insert_number(int num); void delete_number(int num); void print(); // increasing order int level(int num); 30

31 The End Any Question? 31


Download ppt "C Programming : Elementary Data Structures 2009/04/22 Jaemin"

Similar presentations


Ads by Google