data_CHAP03 -   ý / / Data Structures By Hyun-Ju Park /...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview:   ý / / Data Structures By Hyun-Ju Park / Hanbat National University / / 6  6 / / / / 55 55 5  ›  !  › ! / / / 555 555 55 / 6 6 / 6 / k 6 / k /  / / / / / Data Structures By Hyun-Ju Park / Hanbat National University /   ý  {     (stack) (top) “\ û÷ `   !     û÷ ­ û ûo ÷`\  (Last-In-First-Out, LIFO)  / / Data Structures By Hyun-Ju Park / Hanbat National University /      ý — – ª  A top B A top C B A top D C B A top E D C B A top D C B A top / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   −  (system stack) o |  › ó !! ( “ #2) ‚\ “\ § top  104,  3.2 : fp a1 / / Data Structures   fp By Hyun-Ju Park / Hanbat National University / 6                    ý ª  structure Stack objects : 0[  § ñ  functions :  stack ∈ Stack, item ∈ element, max_stack_size ∈ positive integer Stack CreateS(max_stack_size) ::= max_stack_size À Boolean IsFull(stack, max_stack_size) ::= if (stack  == max_stack_size) return TRUE else return FALSE Boolean IsEmpty(stack) ::= if (stack == CreateS(max_stack_size)) return TRUE else return FALSE Stack Add(stack, item) ::= if (IsFull(stack)) stack_full else stack  item return Element Delete(stack) ::= if (IsEmpty(stack)) stack_empty else item û` / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   − ÷  À stack[MAX_STACK_SIZE]  ( … i   § stack[i – 1]  − topó : − À §  )  § stack[0]  r   § stack[1]   top = -1 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6      _    _  ý   ý   — ý – ª ]   Stack CreateS(max_stack_size) ::= #define MAX_STACK_SIZE 100 /* typedef struct { int key; /* */ } element; element stack[MAX_STACK_SIZE]; int top = -1; Boolean IsEmpty(stack) ::= top < 0; Boolean IsFull(stack) ::= top >= MAX_STACK_SIZE – 1; */ void add(int *top, element item) { /*   item */ if (*top >= MAX_STACK_SIZE – 1) { stack_full(); return; IsFull(stack) } stack[++*top] = item; } void delete(int *top) { /* stack § */ IsEmpty(stack) if (*top == -1) return stack_empty(); return stack[(*top)--]; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6     (queue)  (rear) o ÷` û÷ ­ ûo  (front) û ÷`\  û÷ ­ (First-In-First-Out, FIFO)        — – ª ]  A rear front B A rear front C B A rear front D C B A rear D C B rear front front / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   û     (job scheduling) ø (job queue) ›   — – ª ]    § front -1 -1 -1 -1 0 1 rear -1 0 1 2 2 2 Q[0] Q[1] Q[2] Q[3] œ  — — — – – J1 J1 J1 J2 J2 J2 J3 J3 J3 Job 1 Job 2 Job 3 Job 1 Job Job 1 / / Data Structures By Hyun-Ju Park / Hanbat National University /    _      ª  structure Queue objects : 0[  § ñ  functions :  queue ∈ Queue, item ∈ element, max_queue_size ∈ positive integer Stack CreateQ(max_queue_size) ::= max_queue_size À§ Boolean IsFull(queue, max_queue_size) ::= if (queue  == max_queue_size) return TRUE else return FALSE Boolean IsEmpty(queue) ::= if (queue == CreateQ(max_queue_size)) return TRUE else return FALSE Stack AddQ(queue, item) ::= if (IsFull(queue)) queue_full else queue  item queue§ Element DeleteQ(queue) ::= if (IsEmpty(queue)) return else queue á \ item û` / / Data Structures By Hyun-Ju Park / Hanbat National University /  ÷  À queue[MAX_QUEUE_SIZE]› r front, rear            – ª    ]   — Queue CreateQ(max_queue_size) ::= #define MAX_QUEUE_SIZE 100 /* typedef struct { int key; /* */ } element; element queue[MAX_QUEUE_SIZE]; int rear = -1; int front = -1; Boolean IsEmpty(queue) ::= front == rear; Boolean IsFull(queue) ::= rear == MAX_STACK_SIZE – 1; */ void addq(int *rear, element item) { /* queue item */ if (*rear >= MAX_QUEUE_SIZE – 1) { queue_full(); return; } queue[++*rear] = item; } void deleteq(int *front, int rear) { /* queue á  § û */ if (*front == rear) return queue_empty(); return queue[++*front]; } / / Data Structures By Hyun-Ju Park / Hanbat National University /  Ò   ø  `  z“ ú ó› queue_full 0 1 2 3 4 … n-3 n-2 n-1 n − −   :   n[  › \ ` \ ó Q[0] § íó›  ã − À : ý ó ! / / Data Structures By Hyun-Ju Park / Hanbat National University /  ⇒ (circular queue) more efficient queue representation À queue[MAX_QUEUE_SIZE]§  ó› front\ − −      › | front = rear \ front = rear = 0   œ  œ H ª ]  ó› `á § b . [3] [2] [4] [2] [3] J3 J2 J1 [4] [1] [0] [0] front = 0 rear = 0 [5] [1] [5] [0] front = 0 rear = 3 / / Data Structures By Hyun-Ju Park / Hanbat National University / 6      ª  [3] [2] J3 J2 J1 [4] front = 0 rear = 3 [1] [3] [5] [3] [0] [2] J3 J2 J1 J4 J5 [4] [2] J8 J7 J6 J5 J9 [4] [1] [5] [1] [5] [0] front = 0 rear = 5 ô ó› [0] [0] front = 4 rear = 3 − −  û›\ é  À§  ñ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6          —  – ª ]   _  void addq(int front, int *rear, element item) { /* queue item */ *rear = (*rear + 1) % MAX_QUEUE_SIZE; if (front == *rear) { queue_full(rear); return; } queue[*rear] = item; } void deleteq(int *front, int rear) { element item; /* queue front  § û  item [ô */ if (*front == rear) return queue_empty(); *front = (*front + 1) % MAX_QUEUE_SIZE; return queue[*front]; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    c    ª ]  N 010001100011111 100011011100111 011000011110011 110111101101100 110100101111111 001101110100101 011110011111111 001101101111101 110001101100000 001111100011110 010011111011110 N / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   À, maze[i][j], 1 ≤ i ≤ m, 1 ≤ j ≤ p − − − −  − − !  i [  a \  \ ó ›  § 1›  1: 0: ›  ô - maze[1][1] - maze[m][p] / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  move    \  À move   a  Sª ]  NW (row – 1, col – 1) N (row – 1, col) NE (row – 1, col + 1) W (row, col – 1) X (row, col) (row, col + 1) E (row + 1, col – 1) SW (row + 1, col) S (i, j) (row + 1, col + 1) SE / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       S   S ª   typedef struct { short int vert; short int horiz; } offsets; offsets move[8]; // i Name N NE E SE S SW W NW − : : ô  À move[dir].horiz 0 1 1 1 0 –1 –1 –1 dir 0 1 2 3 4 5 6 7 move[dir].vert –1 –1 0 1 1 1 0 –1 , maze[next_row][next_col] next_row = row + move[dir].vert; next_col = col + move[dir].horiz; / / Data Structures By Hyun-Ju Park / Hanbat National University /  ›§     ›   |   ó› ø ó› | ` $  é  , ó − ›§ r mark[m + 2][n + 2] 0ó›  maze[m][n] = 0 / / Data Structures By Hyun-Ju Park / Hanbat National University /    _     ª  initialize a stack to the maze’s entrance coordinates and direction to north; while (stack is not empty) { <row, col, dir> = delete from top of stack; /* \ › */ while (there are more moves from current position) { <next_row, next_col> = coordinate of next move; dir = direction of move; if ((next_row == EXIT_ROW) && (next_col == EXIT_COL)) success; if (maze[next_row][next_col] == 0 && mark[next_row][next_col] == 0) { /* ó›   ó */ mark[next_row][next_col] = 1; /*  é  */ add <row, col, dir> to the top of the stack; row = next_row; col = next_col; dir = north; } } } / / Data Structures By Hyun-Ju Park / Hanbat National University /   < row, col, direction > \ `   ”  û÷ ­ û` ›  − − ›  = m × p( › $ ) $ < m / 2 (n + 1)    1E í 1 / / Data Structures By Hyun-Ju Park / Hanbat National University /    _    ˜  ª   void path(void) { int i, row, col, next_row, next_col, dir, found = FALSE; element position; mark[1][1] = 1; top = 0; stack[0].row = 1; stack[0].col = 1; stack[0].dir = 1; while (top > -1 && !found) { position = delete(&top); row = position.row; col = position.col; dir = position.dir; */ while (dir < 8 && !found) { /* dir next_row = row + move[dir].vert; next_col = col + move[dir].horiz; if (next_row == EXIT_ROW & next_col == EXIT_COL) found = TRUE; else if (!maze[next_row][next_col] && !mark[next_row][next_col]) { mark[next_row][next_col] = 1; position.row = row; position.col = col; position.dir = ++dir; add(&top, position); row = next.row; col = next.col; dir = 0; } else ++ dir; } } if (found) { printf(“The path is :\n”); printf(“row col\n”); for (i = 0; i <= top; i++) printf(“%2d %5d”, stack[i].row, stack[i].col); printf(“%2d %5d”,row, col); printf(“%2d %5d”,EXIT_ROW, EXIT_COL); } else printf(“The maze does not have a path\n”); }  Data Structures : O(mp) By Hyun-Ju Park / Hanbat National University / 6 ›ó / /   ­ } 3.4.1   ø(operand),  ø(operator), x=a/b–c+d*e–a*c ø(delimeter)›  ø  § (  121,  3.13 : C   ) x = (((a / b) – c) + (d * e)) – (a * c)  ! − ø (postfix notation)  ø  (infix notation) − ø  ø infix : postfix : a/b–c+d*e–a*c ab/c–de*+ac*– / / Data Structures By Hyun-Ju Park / Hanbat National University / 6         ª   2+3*4 a*b+5 (1 + 2) * 7 a*b/c ((a / (b – c + d) * (e – a) * c a/b–c+d*e–a*c  234*+ ab*5+ 12+7* ab*c/ abc–d+/ea–*c* ab/c–de*ac*– / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  3.4.2  ­     !! `   ÷(compiler) \ ! ⇒   (parentheses) ø    / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       ­ ª  62/3-42*+  6 2 / 3 4 2 * +  − −  ø§   ø§ ` |  ø§  [0] 6 6 6/2 6/2 6/2–3 6/2–3 6/2–3 6/2–3 6/2–3+4*2 ý [1] 2 3 4 4 4*2 [2] Top 0 1 0 1 0 1 2 2 1 0    ø§  é  §   / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    ª  _   _  ]        ­ Vc    ™  void eval(void) { /*   ›  \ ! expr  */ precedence token; char symbol; int op1, op2; int n = 0; /* ø  */ int top = -1; token = get_token(&symbol, &n); while (token != eos) { if (token == operand) add(&top, symbol – ‘0’) /* else { /* r  ø§ û   !, §  op2 = delete(&top); op1 = delete(&top); /* û */ switch (token) { case plus: add(&top, op1 + op2); break; case minus: add(&top, op1 - op2); break; case times: add(&top, op1 * op2); break; case divide: add(&top, op1 / op2); break; case mod: add(&top, op1 % op2); break; } token = get_token(&symbol, &n); } return delete(&top); } */ */ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  precedence get_token (char *symbol, int * n) { /* ô . symboló ø °, tokenó ó› o . */ *symbol = expr[(*n)++]; switch (*symbol) { case '(' : return lparen; case ')' : return rparen; case '+' : return plus; case '-' : return minus; case '/' : return divide; case '*' : return times; case '%' : return mod; case ' ' : return eos; default : return operand; } } `o ó› , / / Data Structures By Hyun-Ju Park / Hanbat National University /  3.4.3    ›  ø § û \ r ø  › a/b–c+d*e–a*c ((((a/b)–c)+(d*e))–(a*c) ab/c–de*+ac*– / / Data Structures By Hyun-Ju Park / Hanbat National University /  {  −  a+b*c    ­ ­ ª ]   a + b * c eos  − − : −  ø ó  Zó   \ ø  [0] ý [1] [2] Top -1 Output a a ab ab abc abc*+ abc*+ + + + + * * 0 0 1 1 -1  è  ø ó  ø ­ ø |\  ø§ ã ›  ø\   ø§ / / Data Structures By Hyun-Ju Park / Hanbat National University /    JTQ JO TUBDL QSFDFEFODF JDQ JODPNJOH QSFDFEFODF ª  precedence stack[MAX_STACK_SIZE]; /* ispé icp À -j \  ø lparen, rparen,  */ plus, minus, times, divide, mod, eos static int isp = { 0, 19, 12, 12, 13, 13, 13, 0 }; static int icp = { 20, 19, 12, 12, 13, 13, 13, 0 };   _         ª ]  void postfix(void) { char symbol; precedence token; int n = 0; int top = 0; stack[0] = eos; /* eos§  [\ */ for (token = get_token(&symbol, &n); token != eos; token = get_token(&symbol, &n)) { if (token == operand) printf(“%c”, symbol); else if (token == rparen) { /* í  `è |  û` while (stack[top] != lparen) print_token(delete(&top)); delete(&top); /*  §  */ else /* symbol isp token icp `` ó symbol û` while (isp[stack[top]] >= icp[token]) print_token(delete(&top)); add(&top, token); } } while ((token = delete(&top)) != eos) print_token(token); printf(“\n”); } Data Structures */ */ By Hyun-Ju Park / Hanbat National University / / /   ý   i i  V(1 : m)  B(i)\ ó B(i) = T(i) n  o  ns  §` , T(i)\  §` ` øó − B (i ) = T (i ) = m / n (i − 1),1 ≤ i ≤ n i\ B(i) + 1› B(i + 1)   B(n + 1) = mó› / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    0 1    O} ý  ª  m–1 m / n b[1] t[1] 2 m / n b[0] t[0] b[2] t[2] b[n] b[i] = t[i] = m / n i - 1, 0 ≤ i < n … b[0] t[0] b[1] t[1] b[i] t[i] b[i+1] … t[i+1] b[i+2] t[j] b[j+1] b[n] / / Data Structures By Hyun-Ju Park / Hanbat National University / 6     _         ý [email protected] JUFN — ª ]  ý [email protected]  JUFN – void add(int i, element item) { // item i   if (top[i] == boundary[i + 1]) stack_full(i); memory[++top[i]] = item; } void delete(int i) { // i   top  § û` if (top[i] == boundary[i]) return stack_empty(i); return memory[top[i]--]; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  stack_full(i) v  \§ i \  í \ ié i + 1  ó°,  ó   û › j(i < j ≤ n)§ ø j è jé \ i, i + 1    \, ã  , v(m)ó ó \ ó  \j i j§ t(j) < b(j + 1) \ ó› 1≤j<i jé j + 1 − é −  (v(1)ó ój j j i + 1, i + 2, …, j§ ) j j+1 t(j) < b(j + 1) j + 1, j + 2, …, i§ íó› è ó v m[ ó r / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  ...
View Full Document

This note was uploaded on 05/10/2011 for the course CS 101 taught by Professor Kks during the Spring '11 term at Seoul National.

Ask a homework question - tutors are online