1 / 22

제 4 장 리스트

제 4 장 리스트. 4.4 다항식. 리스트를 이용한 다항식 표현 A(x) = 3x 14 +2x 8 +1 B(x) = 8x 14 -3x 10 + 10x 6. a. 3. 14. 2. 8. 1. 0. <br>. b. 8. 14. -3. 10. 10. 6. <br>. a. 3. 14. 2. 8. 1. 0. <br>. typedef struct poly_node *poly_pointer; typedef struct poly_node{ int coef; int exp;

leoma
Download Presentation

제 4 장 리스트

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제4장 리스트

  2. 4.4 다항식 • 리스트를 이용한 다항식 표현 • A(x) = 3x14+2x8+1 • B(x) = 8x14-3x10+ 10x6 a 3 14 2 8 1 0 \n b 8 14 -3 10 10 6 \n

  3. a 3 14 2 8 1 0 \n typedef struct poly_node *poly_pointer; typedef struct poly_node{ int coef; int exp; poly_pointer link; }

  4. a 3 14 2 8 1 0 \n 1) a->exp == b->exp b 8 14 -3 10 10 6 \n d 11 14 \n

  5. a 3 14 2 8 1 0 \n 2) a->exp < b->exp b 8 14 -3 10 10 6 \n d 11 14 -3 10 \n

  6. a 3 14 2 8 1 0 \n 3) a->exp > b->exp b 8 14 -3 10 10 6 \n d 11 14 -3 10 2 8 \n

  7. a 3 14 2 8 1 0 \n 4) a->exp < b->exp b 8 14 -3 10 10 6 \n d 11 14 -3 10 2 8 10 6 \n

  8. a 3 14 2 8 1 0 \n 4) b == NULL b 8 14 -3 10 10 6 \n d 11 14 -3 10 2 8 10 6 1 0 \n

  9. poly_pointer padd(poly_pointer a, poly_pointer b) { /* a와 b가 합산된 다항식을 반환 */ poly_pointer front, rear, temp; int sum; rear = (poly_pointer) malloc(sizeof(poly_node)); if(IS_FULL(rear)){ fprintf(stderr,”The memory is full\n”); exit(1); } front = rear; while(a && b) switch(compare(a->exp,b->exp)){ case –1: /* a > b */ attach(b->coef, b->exp, &rear); b=b->link; break; case 0: /* a = b */ sum = a->coef + b->coef; if (sum) attach(sum, a->exp, &rear); a=a->link; b=b->link; break; case 1: /* a < b */ attach(a->coef, a->exp, &rear); a=a->link; break; }

  10. /* 리스트 a와 리스트 b의 나머지를 복사 */ for(; a; a=a->link) attach(a->coef, a->exp, &rear); for(; b; b=b->link) attach(b->coef, b->exp, &rear); rear->link = NULL; /* 필요없는 초기 노드 삭제 */ temp = front; front = front->link; free(temp); return front; }

  11. void attach(int coef, int exp, poly_pointer *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 = coef; temp->exp = exp; (*ptr)->link = temp; *ptr = temp; }

  12. d 11 14 -3 10 2 8 \n temp 3 5 \n 3 5 void attach(int coef, int exp, poly_pointer *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 = coef; temp->exp = exp; (*ptr)->link = temp; *ptr = temp; }

  13. 4.4 다항식 ptr 11 14 -3 10 2 8 \n • 다항식 제거 void erase(poly_pointer *ptr) { poly_pointer temp; while (*ptr){ temp = *ptr; *ptr = (*ptr)->link; free(temp); } }

  14. avail \n • Available space list의 관리 poly_pointer get_node(void) { 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; }

  15. 4.8 이중연결리스트 • 단순연결리스트 • 링크의 방향으로만 이동 가능 head \n

  16. 4.8 이중연결리스트 head \n • 노드 구조 typedef struct node *node_pointer; typedef struct node { node_pointer llink; element item; node_pointer rlink; }

  17. 4.8 이중연결리스트 • 이중연결리스트의 특징 • ptr = ptr->llink->rlink = ptr->rlink->llink • 공백 리스트 ptr ptr \n - \n

  18. node newnode void dinsert(node_pointer node, node_pointer newnode) { /* newnode를 node의 오른쪽에 삽입 */ newnode->llink = node; newnode->rlink = node->rlink; node->rlink->llink = newnode; node->rlink = newnode; }

  19. node 1 newnode 2 node node 4 3 3 newnode newnode

  20. node deleted 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); } }

  21. 4.8 이중연결리스트 node deleted

  22. 과제물 • 이중 연결 리스트를 이용해서 스택을 작성하시오.

More Related