c programming : elementary data structures 2009/04/22 jaemin soh([email protected])

31
C Programming : Elementary Data Structures 2009/04/22 Jaemin Soh([email protected])

Upload: arlene-hopkins

Post on 16-Dec-2015

227 views

Category:

Documents


1 download

TRANSCRIPT

C Programming :Elementary Data Structures

2009/04/22

Jaemin Soh([email protected])

Contents Data Structure Stack Queue Dynamic Memory Allocation Linked List Tree

2

Data Structure Data + Structure

Data Handling rule

Point Input Output Properties

Examples Stack Queue Tree

3

Stack Storage layout: Last-In, First-Out (LIFO) Primitive operations: push, pop, isEmpty, isFull

4

8 2

Stack:

Stack:

Stack:

Stack:

isEmpty() == true

1 7

8

8 2 1

push(8)

push(2); push(1); push(7);isFull() == true

Stack grows

pop() == 7

Stack grows

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

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;

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--]; }

Queue Storage layout: First-Come, First-Served (FCFS) Primitive operations: put, get, isEmpty, isFull

8

8 2

Queue:

Queue:

Queue:

Queue:

isEmpty() == true

1 7

8

72 1

put(8)

put(2); put(1); put(7);isFull() == true

Queue grows

get() == 8

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: 8 2 1 7

tail head

Queue Implementation(2/4) Circular Queue

10

8 2 1 7 1 2 3 get() == 8; get() == 2; get() == 1;

7 1 2 38 2 1put(14); put(11);

7 1 2 3 1411head=1 tail=3

7 1 2 3 1411head=1 tail=4

0 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7

0 1 2 3 4 5 6 7

get() == 7;

tail=3 head=7

tail=0 head=7

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;

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; }

C Array The allocated space is not flexible

Data insertion/deletion is difficult

13

1 2 3 4 5 6 7

index: 0 1 2 3 4 5 6 7

8

9 10

?

Dynamic Memory Allocation Static Allocation

Determined when program starts.

Dynamic Memory Allocation Determined during the running time.

14

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 castingfor (i = 0; i < size; i++) // Initialization

pi[i] = -1;

void* malloc(size_t size) /* <stdlib.h> : malloc/calloc/realloc */

Dynamic Memory Deallocation Synopsis

Frees the memory space pointed by ptr.

Frees(Deallocates) the dynamically allocated memory space

16

void free(void *ptr) /* <stdlib.h> : free */

int *pi;int size, i;

scanf(“%d”, &size);pi = (int *)malloc(sizeof(int) * size);

if (pi != NULL) {free(pi); pi = NULL; }

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

8 7 2 7 1 7

Node

LinkData field

HEAD X

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; }

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;

elsereturn p;

}

Primitive Functions – Insert

20

8

2

1

prev

HEAD X

originally prevnext

n

void insert(node_t *prev, node_t *n){

node_t *next = prev->next;

prev->next = n;n->next = next;

}

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;

}

8 2 1

prev

HEAD X

originally dnextd

Doubly Linked List Has two links pointing to the next and previous nodes

22

Next

Prev

HEAD 8

HEAD

8 2 1

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;}

Array vs. Linked List

24

About memory savings?

Array Linked List

Indexing (random access) O(1) O(n)

Inserting / Deleting at end O(1) O(1) or O(n)

Inserting / Deleting at middle O(n) O(1)

Resize Difficult Easy

Locality Great Bad

Tree Connected, acyclic, and undirected graph.

Binary tree, heap tree, B+ tree, etc…

25

5

2 7

1 4 6

3

Root node

Leaf node

Parent

Child

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;}

Binary Tree

27

4

2 6

1 3 5 7

2 6

1null null

3null null

5null null

4

7null null

right

root

left right

root

Tree Primitive Functions

28

2 6

1null null

3null null

5null null

4

7null null

left right

rootnode_t *search(int d);node_t *insert(int d);void delete(int d);

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

2 6

1 3 5 7

2

3 4

5

6 7

1

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

The End Any Question?

31