data_CHAP04 -  c  / / 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:  c  / / Data Structures By Hyun-Ju Park / Hanbat National University / / / 55 55       / / 555 55 / 66 Z  / / / / / 55 55 55 5     `û  / / / / 555 555 555 55 / 66 / / 6 ú   6 / / 6   6 / / 6  6 /  / / Data Structures By Hyun-Ju Park / Hanbat National University / / 66   6 / / 66 66 / / / 55 5 à é / / 555 55 / 66 6 /  (sequential representation)    −  ø   ` û é ó › › ú (bat, cat, eat, fat, hat, jat, lat, mat, oat, pat, rat, sat, tat, vat, wat) add ‘gat’ & delete ‘lat’ à  ø (linked representation)  › ô §  ` \ \  ô §  \ `  §     o (pointer)§ ñ  (link) ⇒ % (linked list) / / Data Structures By Hyun-Ju Park / Hanbat National University /   C  −   : : ó û›     | ø ø   |\  NULL›  \ ” T T û  ó    2  − −  − − & * / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    _  $  ª  int i, *pi; int float *pf; pi = &i; /* pi\ i§  */ i pi i pi i = 10; *pi = 10; /* i 10  */ i 10 pi if (pi = NULL) if (!pi) /* pi  a */ pi = malloc(sizeof(int)); pf = (float *)pi; i 10 ? pi / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   › C \ − −   › û ó     §  ª \ ]   \ §   \  (heap) malloc() & free() malloc (  ) ó char* ‚\ void* ›   K int i, *pi; float f, *pf; pi = (int *) malloc(sizeof(int)); pf = (float *) malloc(sizeof(float)); *pi = 1024; *pf = 3.14; printf(“an integer = %d, a float = %f\n”, *pi, *pf); free(pi); free(pf); / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   §  o     \   o  \  l ª  L ó L ptr bat bat cat sat vat null      DBU NBU — ª  ptr bat cat sat vat null add mat ptr bat cat sat vat null mat mat / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       NBU – ª  ptr bat bat cat mat sat vat null delete mat ptr bat cat mat sat vat null / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   L − %  _  o  ª ]   ø §  typedef struct list_node *list_pointer; typedef struct list_node { char data[4]; list_pointer link; }; list_pointer ptr = NULL; À − − − L − − list_pointer ptr = NULL; À L ptr = (list_pointer) malloc(sizeof(list_node));   (structure member)  ø : '->' e->name D (*e).name; strcpy(ptr->data,"bat"); a #define IS_EMPTY(ptr) (!(ptr)) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6            ] ª ™ ]  first 10 10 20 null list_pointer create2() { /* r [ L §    */ list_pointer first, second; first = (list_pointer)malloc(sizeof(list_node)); second = (list_pointer)malloc(sizeof(list_node)); second->link = NULL; second->data = 20; first->data = 10; first->link = second; return first; } / / Data Structures By Hyun-Ju Park / Hanbat National University /             JOTFSU §— QUS QUS  ª ]  void insert(list_pointer *ptr, list_pointer node) { /* data = 50 › L §  ptr node  list_pointer temp; temp = (list_pointer)malloc(sizeof(list_node)); if (IS_FULL(temp)) { fprintf(stderr, “The memory is full\n”); exit(1); } temp->data = 50; ptr if (*ptr) { node temp->link = node->link; node->link = temp; temp } else { temp->link = NULL; *ptr = temp; } } */ 10 20 null 50 / / Data Structures By Hyun-Ju Park / Hanbat National University /                 – QUS /6-- QUS QUS QUS QUS MJOL ª ]   EFMFUF  EFMFUF void delete(list_pointer *ptr, list_pointer trail, list_pointer node ) { /*  › L § û, trailó ûo node L ° ptró  ø */ if (trail) trail->link = node->link; else *ptr = (*ptr)->link; ptr free(node); 10 50 } 20 null node trail = null node trail ptr 10 20 null ptr 50 20 null / / Data Structures By Hyun-Ju Park / Hanbat National University /      V ª  void print_list(list_pointer ptr) { printf(“The list contains: ”); for ( ; ptr; ptr = ptr->link) printf(“%4d”, ptr->data); printf(“\n”); } ptr 10 50 20 null / / Data Structures By Hyun-Ju Park / Hanbat National University /   S   o  § ý      oW ý  ª  ` 2À§  \  top ...  null null front rear ...  null ó À  Data Structures  ù  \j  [   §   \j   By Hyun-Ju Park / Hanbat National University § / 6 / /  ns    c  Õª ]  #define MAX_STACKS 10 /* typedef struct { int key; /* */ } element; typedef struct stack *stack_pointer; typedef struct stack { element item; stack_pointer link; }; stack_pointer top[MAX_STACKS]; */ top[i] = NULL, 0 D i < MAX_STACKS i  À , top[i]=NULL , IS_FULL(temp) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6      oW ý — ª  void add(stack_pointer *top, element item) { /*  § */ stack_pointer temp = (stack_pointer) malloc(sizeof (stack)); if (IS_FULL(temp)) { fprintf(stderr,"The memory is full\n"); exit(1); } temp->item = item; temp->link = *top; *top = temp; } *top ... null *top item ... null / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    _    oW ý – ª  element delete(stack_pointer *top) { /* ó›  § û */ stack_pointer temp = *top; element item; if (IS_EMPTY(temp)) { fprintf(stderr,"The stack is empty\n"); exit(1); } item = temp->item; *top = temp->link; free(temp); return item; } *top item ... null *top ... null / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  ns   c  Õª ]  #define MAX_QUEUE 10 /*  */ typedef struct queue *queue_pointer; typedef struct queue { element item; queue_pointer link; }; queue_pointer front[MAX_QUEUES], rear[MAX_QUEUES]; front[i]=NULL, 0 D i <MAX_QUEUES i  À , front[i]=NULL  , IS-FULL(temp) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    _    oW  SFBS — ª  void addq(queue_pointer *front, queue_pointer *rear, element item) { /* rear  § */ queue_pointer temp = (queue_pointer) malloc(sizeof (queue)); if (IS_FULL(temp)) { fprintf(stderr,"The memory is full\n"); exit(1); } temp->item = item; temp->link = NULL; if (*front) (*rear)->link = temp; else *front = temp; *rear = temp; } *front ... *rear null *rear *front ... item null / / Data Structures By Hyun-Ju Park / Hanbat National University / 6      oW  GSPOU – ª  element deleteq(queue_pointer *front) { /*   § û */ queue_pointer temp = *front; element item; if (IS_EMPTY(*front)) { fprintf(stderr,"The queue is empty\n"); exit(1); } item = temp->item; *front = temp->link; free(temp); return item; } *rear *front ... null *rear *front ... null / / Data Structures By Hyun-Ju Park / Hanbat National University /   4.4.1 ­ ­ ß  § à  o  ª ]  %   typedef struct poly_node *poly_pointer; typedef struct poly_node { int coef; int expon; poly_pointer link; }; poly_pointer a, b, d;      ­  ª ]  a = 3x14 + 2x8 + 1 a 3 14 2 8 3 14 null b = 8x14 + 3x10 + 10x6 b 8 14 -3 10 10 6 null / / Data Structures By Hyun-Ju Park / Hanbat National University /  4.4.2  ­      E  B C   ™ ª ]  a b 3 8 14 14 2 -3 8 10 1 10 0 6 null null d 11 11 14 null d 11 14 -3 10 null d 11 14 -3 10 2 8 null / / Data Structures By Hyun-Ju Park / Hanbat National University /     _         ­  ] aª ]    poly_pointer padd(poly_pointer a, poly_pointer b) { /* aé b o */ poly_pointer front, rear, temp; int sum; rear = (poly_pointer)malloc(sizeof(poly_node)); if (IF_FULL(rear)) { fprintf(stderr, "The memory is full\n"); exit(1); } front = rear; while(a && b) switch (COMPARE(a->expon, b->expon)) { case -1: /* a->expon < b->expon */ attach(b->coef, b->expon, &rear); b = b->link; break; case 0: /* a->expon = b->expon */ sum = a->coef + b->coef; if (sum) attach(sum, a->expon, &rear); a = a->link; b->link; break; case: 1 /* a->expon > a->expon */ attach(a->coef, a->expon, &rear); a = a->link; } /*  aé  b ` §  */ for (; a; a = a->link) attach(a->coef, a->expon, &rear); for (; b; b = b->link) attach(b->coef, b->expon, &rear); rear->link = NULL; /* \ L § û */ temp = front; front = front->link; free(temp); return front; } Data Structures By Hyun-Ju Park / Hanbat National University / / /  void attach(float coefficient, int exponent, poly_pointer *ptr) { /* coef = coefficient expon = exponent › L § ptr \ L   . ptr \  › . */ poly_pointer temp; temp = (poly_pointer)malloc(sizeof(poly_node)); if (IS_FULL(temp)) { fprintf(stderr, "The memory is full\n"); exit(1); } temp->coef = coefficient; temp->expon = exponent; (*ptr)->link = temp; *ptr = temp; } , › L § / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  4.4.3 ­ Ñ  à ­ – ª ]  !      void erase(poly_pointer *ptr) { /* ptr \ poly_pointer temp; while (*ptr) { temp = *ptr; *ptr = (*ptr)->link; free(temp); } } û` */ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  4.4.4 ­ o  (chain)  ›  ª ]  %(circular list) ↔   L         L § a = 3x14 + 2x8 + 1 ptr ptr  − ( ) ) 3 14 2 8 1 0  L § ! ó› û` (á ) / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   −   §    (available space list) ñ  ]  malloc, free§   get_node, ret_node     HFU@OPEF ª poly_pointer get_node(void) /*  L § û */ { poly_pointer node; if (avail) { node = avail; avail = avail->link; } else { node = (poly_pointer) malloc(sizeof(poly_node)); if (IS_FULL(node)) { fprintf(stderr, "The memory is full\n"); exit(1); } } return node; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6        SFU@OPEF ª  void ret_node(poly_pointer ptr) /*    L § */ { ptr->link = avail; avail = ptr; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       a ª  avail ptr ptr temp avail   _     ... ... Ѫ −  ]    § null void cerase(poly_pointer *ptr) ptr û` */ /*   { poly_pointer temp; if (*ptr) { temp = (*ptr)->link; (*ptr)->link = avail; avail = temp; *ptr = NULL; } }   !Þ ! ó› / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   û›         o ­  ª ]  û› a - a 3 14 2 8 1 0 a - - 3 14 2 8 1 0 / / Data Structures By Hyun-Ju Park / Hanbat National University /       W ­ ª   poly_pointer cpadd(poly_pointer a, poly_pointer b) /* aé b\ L §   , aé b o { poly_pointer starta, d, lastd; int sum, done = FALSE; starta = a; /* a ø › */ a = a->link; b = b->link; /* aé b L §  */ d = get_node(); /*  L § */ d->expon = -1; lastd = d; do { switch (COMPARE(a->expon, b->expon)) { case -1: /* a->expon < b->expon */ attach(b->coef, b->expon, &lastd); b = b->link; break; case 0: /* a->expon = b->expon */ if (starta == a) done = TRUE; else { sum = a->coef + b->coef; if (sum) attach(sum, a->expon, &lastd); a = a->link; b->link; } break; case: 1 /* a->expon > a->expon */ attach(a->coef, a->expon, &lastd); a = a->link; } } while (!done); lastd->link =d; return d; } . */ / / Data Structures By Hyun-Ju Park / Hanbat National University /    = 4.5.1   _     § o § ª ]  list_pointer invert(list_pointer lead) { /* lead  \ §  ó›  . */ list_pointer middle, trail; middle = NULL; while (lead) { trail = middle; middle = lead; lead = lead->link; lead middle->link = trail; middle = null } trail = null return middle; } null / / Data Structures By Hyun-Ju Park / Hanbat National University /       § o oª  list_pointer concatenate(list_pointer ptr1, list_pointer ptr2) /*  ptr1   ptr2 ú o ›  § . ptr1  \ \ .*/ { list_pointer temp; if (IS_EMPTY(ptr1)) return ptr2; else { if (!IS_EMPTY(ptr2)) { for (temp = ptr1; temp->link; temp = temp->link) ; temp->link = ptr2; } return ptr1; } } / / Data Structures By Hyun-Ju Park / Hanbat National University /  4.5.2 o % › L §  L § z“  ã á   L  §  ã ›  L § | ⇒   %          #  ]  ­ª ]  a x1 x2 x3 x1 x2 x3 a / / Data Structures By Hyun-Ju Park / Hanbat National University / 6        — ª  void insert_front(list_pointer *ptr, list_pointer node) /* ptr   L §  \   ptr á . */ L § { if (IS_EMPTY(*ptr)) { /*  À÷ , ptr ›   ›  *ptr = node; node->link = node; } else { /*  À ,  á ›  node->link = (*ptr)->link; (*ptr)->link = node; } } */ */ node x1 x2 x3 *ptr / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       } ª  int length(list_pointer ptr) { /*   ptr $ § list_pointer temp; int count = 0; if (ptr) { temp = ptr; do { count++; temp = temp->link; } while (temp != ptr); } return count; } . */ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    S ¡} 4.6.1 S ¡} S %  (equivalence relation) (reflexive) −  x x≡x (symmetric) − r x, y x≡y y≡x (transitive) − x, y, z x≡y y≡z x≡z S  ≡ , ,  )Ô S  (equivalence relation)   S (equivalence class) r xé y  x≡y  xé y\ ó  °   0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, 2 ≡ 11, 11 ≡ 0 − { 0, 2, 4, 7, 11 } ; { 1, 3, 5 } ; { 6, 8, 9, 10 } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  4.6.2 S o       } S  ª ]  void equivalence() { initialize; while (there are more pairs) { read the next pair <i, j>; process this pair; } initialize the output; do output a new equivalence class; while (not done); } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  pairs[n][m]    ›  \j  › È <i, k>§ i   i ⇒ % L § §  é link seq[n] out[n] ›   L \ data n › ñ / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    _     S  ª  void equivalence() { initialize seq to NULL and out to TRUE; while (there are more pairs) { read the next pair <i, j>; put j on the seq[i] list; put i on the seq[j] list; } for (i = 0; i < n; i++) if (out[i]) { out[i] = FALSE; output this equivalence class; } } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6       ° VW ª  0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, 2 ≡ 11, 11 ≡ 0 [0] seq [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] data link 11 3 null 11 null 5 7 3 null 8 4 null 6 8 null 6 null 0 data 4 1 null 0 null 10 null 9 null 2 null link null / / Data Structures By Hyun-Ju Park / Hanbat National University / 6    _     S  ª  _  #include <stdio.h> #inlcude <alloc.h> #define MAX_SIZE 24 #define IS_FULL(ptr) (!(ptr)) #define FALSE 0 #define TRUE 1 typedef struct node *node_pointer; typedef struct node { int data; node_pointer link; }; void main(void) { short int out[MAX_SIZE]; node_pointer seq[MAX_SIZE]; node_pointer x, y, top; int i, j, n; printf("Enter the size (<= %d) ", MAX_SIZE); scanf("%d", &n); */ for (i = 0; i < n; i++) { /* seqé out out[i] = TRUE; seq[i] = NULL; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  /* 1 : È */ printf("Enter a pair of numbers (-1 -1 to quit): "); scanf("%d%d", &i, &j); while (i >= 0) { x = (node_pointer)malloc(sizeof(node)); if (IS_FULL(x)) { fprintf(stderr, "The memory is full\n"); exit(1); } x->data = j; x->link = seq[i]; seq[i] = x; x = (node_pointer)malloc(sizeof(node)); if (IS_FULL(x)) { fprintf(stderr, "The memory is full\n"); exit(1); } x->data = i; x->link = seq[j]; seq[j] = x; printf("Enter a pair of numbers (-1 -1 to quit): "); scanf("%d%d", &i, &j); } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6  /* 2 : */ for (i = 0; i < n; i++) if (out[i]) { printf("\nNew class: %5d", i); FALSE› */ out[i] = FALSE; /* x = seq[i]; top = NULL; /* */ for (;;) {  */ while (x) { /*  j = x->data; if (out[j]) { printf(%5d", j); out[j] = FALSE; y = x->link; x->link = top; top = x; x = y; } else x = x->link; } if (!top) break;  û` */ x = seq[top->data]; top = top->link; /* } } } / / Data Structures By Hyun-Ju Park / Hanbat National University / 66     % L  \         ] ª ]  L down down head next right   L down head row value col right / / Data Structures By Hyun-Ju Park / Hanbat National University / 66          ×      o ª ]    0 11 0 0 12 0 0 0 0 −4 0 0 0 0 − 15 0 H0 4 4 H1 H2 H3 H0 0 11 10 12 2 -4 1 2 H1 H2 H3 33 -15 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66    cª  #define MAX_SIZE 50 /* ˜ */ typedef enum {head,entry} tagfield; typedef struct matrix_node *matrix_pointer; typedef struct entry_node { int row; int col; int value; }; typedef struct matrix_node { matrix_pointer down; matrix_pointer right; tagfield tag; union { matrix_pointer next; entry_node entry; } u; }; matrix_pointer hdnode[MAX_SIZE]; / / Data Structures By Hyun-Ju Park / Hanbat National University / 66                V    ] ª ]     _   _  matrix_pointer mread(void) /* ˜ ÷  ó› .   À hdnode§ { int num_rows, num_cols, num_terms, num_heads, i; int row, col, value, current_row; matrix_pointer temp, last, node;  . */ < 4, 4, 4 > < 0, 2, 11 > < 1, 0, 12 > < 2, 1, -4 > < 3, 3, -15 > printf("Enter the number of rows, columns, and number of nonzero terms: " scanf("%d%d%d", &num_rows, &num_cols, &num_terms); num_heads = (num_cols > num_rows) ? num_cols : num_rows; /* L   L § */ node = new_node(); node->tag = entry; node->u.entry.row = num_rows; node->u.entry.col = num_cols; if (!num_heads) node->right = node; else { /* L . */ for (i = 0; i < num_heads; i++) { temp = new_node; hdnode[i] = temp; hdnode[i]->tag = head; hdnode[i]->right = temp; hdnode[i]->u.next = temp; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  current_row = 0; last = hdnode[0]; /*   L */ for (i = 0; i < num_terms; i++) { printf("Enter row, column and value: "); scanf("%d%d%d", &row, &col, &value); if (row > current_row) { /*   */ last->right = hdnode[current_row]; current_row = row; last = hdnode[row]; } temp = new_node(); temp->tag = entry; temp->u.entry.row = row; temp->u.entry.col = col; temp->u.entry.value = value; last->right = temp; last = temp; /*    */ hdnode[col]->u.next->down = temp; /*     */ hdnode[col]->u.next = temp; } last->right = hdnode[current_row]; /*   */ for (i = 0; i < num_cols; i++) /*    §  */ hdnode[i]->u.next->down = hdnode[i]; for (i = 0; i < num_heads - 1; i++) /*  L  */ hdnode[i]->u.next = hdnode[i + 1]; hdnode[num_heads - 1]->u.next = node; node->right = hdnode[0]; } return node; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 66        V ª  void mwrite(matrix_pointer *node) /* ˜  ó› . */ { int i; matrix_pointer temp, head = node->right; /* ˜  */ printf("\n num_rows = %d, num_cols = %d \n", node->u.entry.row, node->u.entry.col); printf(" The matrix by row, column, and value: \n\n"); for (i = 0; i < node->u.entry.row; i++) { /*  \   */ for (temp = head->right; temp != head; temp = temp->right) printf("%5d%5d%5d \n", temp->u.entry.row, temp->u.entry.col, temp->u.entry.value); head = head->u.next; /* ô */ } } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6        – ª  void merase(matrix_pointer *node) û , L › . */ /* ˜ { matrix_pointer x, y, head = (*node)->right; int i, num_heads;  ó› . */ /*   L é L for (i = 0; i < (*node)->u.entry.row; i++) { y = head->right; while (y != head) { x = y; y = y->right; free(x); } x = head; head = head->u.next; free(x); } . */ /* `  L y = head; while (y != *node) { x = y; y = y->u.next; free(x); } free(*node); *node = NULL; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6   o %(singly linked linear list) L P  ã L û û  ó›\ b `P L (predecessor)§  ô L \ r[ −  ! %(doubly linked list) › (RLINK),  ] ª (LLINK) ]  typedef struct node *node_pointer; typedef struct node { node_pointer llink; element item; node_pointer rlink; } / / Data Structures By Hyun-Ju Park / Hanbat National University / 6            ] ] a a œ o o ª ]  llink item rlink ----- L p == p->llink->rlink == p->rlink->llink first À   --- / / Data Structures By Hyun-Ju Park / Hanbat National University / 6            œ o o — — ª ]  void dinsert(node_pointer node, node_pointer newnode) { /* newnode§ node  */ newnode->llink = node; newnode->rlink = node->rlink; node->rlink->llink = newnode; node->rlink = newnode; node } --- llink item rlink --newnode node1 L node2 node3 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66            o o   – – ª ]  void ddelete(node_pointer node, node_pointer deleted) { /*   û */ if (node == deleted) printf("Deletion of head node not permitted.\n"); else { deleted->llink->rlink = deleted->rlink; deleted->rlink->llink = deleted->link; free(deleted); } } llink item rlink --- L node1 node2 node3 / / Data Structures By Hyun-Ju Park / Hanbat National University / 66  ...
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