c언어 다항식 오류 질문합니다

조회수 615회
처리되지 않은 예외가 throw됨: 쓰기 액세스 위반입니다.
item이(가) nullptr였습니다.

디버깅시 item이 왜 쓰레기값인지 모르겠습니다.

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct _list_node
{
  struct _list_node *link;
  int coefficient;
  int order;
} NODE;

static NODE *polyAdd(NODE *head1, NODE *head2)
{
  NODE *item = NULL;
  NODE *p1 = head1;
  NODE *p2 = head2;
  NODE *head, *tail = NULL;

  while (1)
  {
    if (p1 != NULL && p2 != NULL)
    {
      if (p1->order == p2->order)
      {
        item->coefficient = p1->coefficient + p2->coefficient; /* 여기서 오류*/
        item->order = p1->order;
        p1 = p1->link;
        p2 = p2->link;
      }

      else
      {
        if (p1->order > p2->order)
        {
          item->coefficient = p1->coefficient;
          item->order = p1->order;
          p1 = p1->link;
        }

        if (p1->order < p2->order)
        {
          item->coefficient = p2->coefficient;
          item->order = p2->order;
          p2 = p2->link;
        }
      }
    }

    else
    {
      if (p1 == NULL)
      {
        item->coefficient = p2->coefficient;
        item->order = p2->order;
        p1 = p1->link;
      }

      if (p2 == NULL)
      {
        item->coefficient = p1->coefficient;
        item->order = p1->order;
        p2 = p2->link;
      }
    }

    if (p1 == NULL && p2 == NULL)
    {
      tail->link = NULL;
      break;
    }

    if (tail = NULL)
    {
      head = item;
    }
    else
    {
      tail->link = item;
    }
    tail = item;
  }

  return head;
}

static NODE *create(int *list, int size)
{
  int  i;
  NODE *item, *head = (NODE *)0;

  for (i = 0; i < size; i++) {
    if (list[i]) {
      item = (NODE *)malloc(sizeof(NODE));
      if (!item) {
        fprintf(stderr, "\nOut of memory.\n");
        exit(1);
      }
      item->coefficient = list[i];
      item->order = i;
      item->link = head;
      head = item;
    }
  }
  return(head);
}

static int getSize(int *list)
{
  int size = 0;
  if (!list)
    return(0);
  while (list[size] >= 0)
    size++;
  return(size);
}

static void printList(NODE *head)
{
  NODE *item = head;
  while (item) {
    if (item != head)
      printf(" +");
    if (item->order)
      printf(" %d e%d", item->coefficient, item->order);
    else
      printf(" %d", item->coefficient);
    item = item->link;
  }
  printf("\n");
}

static void deleteList(NODE **head)
{
  NODE *del, *item;
  if (!head)
  {
    fprintf(stderr, "[deleteList] usage of function is wrong.\n");
    exit(1);
  }
  item = *head;
  while (item)
  {
    del = item;
    item = item->link;
    free(del);
  }
  *head = (NODE *)0;
}


void main(void)
{
  int list1[] = { 1, 2, 3, 4, 5, 6, -1 };
  int list2[] = { 6, 5, 4, 3, 2, 1, -1 };
  int list3[] = { 2, 7, 0, 3, 0, 8, -1 };
  int list4[] = { 0, 0, 8, 0, 2, 9, 8, -1 };
  int n1, n2;
  NODE *root, *head1, *head2;

  /*
   * Easy
   */
  n1 = getSize(list1);
  n2 = getSize(list2);
  head1 = create(list1, n1);
  head2 = create(list2, n2);
  root = polyAdd(head1, head2);

  printf("\nPolynomial 1 :"); printList(head1);
  printf("Polynomial 2 :");   printList(head2);
  printf("Added result :");   printList(root);

  deleteList(&head1);
  deleteList(&head2);
  deleteList(&root);

  /*
   * Hard case 1
   */
  n1 = getSize(list3);
  n2 = getSize(list4);
  head1 = create(list3, n1);
  head2 = create(list4, n2);
  root = polyAdd(head1, head2);

  printf("\nPolynomial 1 :"); printList(head1);
  printf("Polynomial 2 :");   printList(head2);
  printf("Added result :");   printList(root);

  deleteList(&root);

  /*
   * Hard case 2
   */
  root = polyAdd(head2, head1);

  printf("\nPolynomial 1 :"); printList(head2);
  printf("Polynomial 2 :");   printList(head1);
  printf("Added result :");   printList(root);

  deleteList(&head1);
  deleteList(&head2);
  deleteList(&root);
}

  • 으으 가독성;; ohsangyun 2019.6.2 21:41

1 답변

  • item 은 NULL 로 초기화된 상태(NODE * item = NULL;)에서 item - > coefficient 을 했기 때문에 오류가 나는 겁니다.

    static NODE * polyAdd(NODE * head1, NODE * head2) {
        NODE * item = NULL;
        NODE * p1 = head1;
        NODE * p2 = head2;
        NODE * head, * tail = NULL;
    
        while (1) {
            if (p1 != NULL && p2 != NULL) {
                if (p1 - > order == p2 - > order) {
                    item - > coefficient = p1 - > coefficient + p2 - > coefficient; /* 여기서 오류*/
                    item - > order = p1 - > order;
                    p1 = p1 - > link;
                    p2 = p2 - > link;
                } else {
                    if (p1 - > order > p2 - > order) {
                        item - > coefficient = p1 - > coefficient;
                        item - > order = p1 - > order;
                        p1 = p1 - > link;
                    }
    
                    if (p1 - > order < p2 - > order) {
                        item - > coefficient = p2 - > coefficient;
                        item - > order = p2 - > order;
                        p2 = p2 - > link;
                    }
                }
            } else {
                if (p1 == NULL) {
                    item - > coefficient = p2 - > coefficient;
                    item - > order = p2 - > order;
                    p1 = p1 - > link;
                }
    
                if (p2 == NULL) {
                    item - > coefficient = p1 - > coefficient;
                    item - > order = p1 - > order;
                    p2 = p2 - > link;
                }
            }
    
            if (p1 == NULL && p2 == NULL) {
                tail - > link = NULL;
                break;
            }
    
            if (tail = NULL) {
                head = item;
            } else {
                tail - > link = item;
            }
            tail = item;
        }
    
        return head;
    }
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)