링크드 리스트 질문입니다=ㅂ=;
소스를 구현하다 보니 파일을 입력 받는곳에서 에러가 나는거 같습니다.
링크드 리스트는 이번에 수업을 들어서 그런지 약간 알꺼는 같은데 fopen하고 fscanf쓰는게 문제네요..
제일 마지막 메인쪽이 문제인거 같습니다..
그리고 값을 fprintf로 printList에 파일을 쓸려고 했는데요.
안돼네요=ㅂ=;; 그래서 일단 지워버렸는데, 구현을 어찌 해야할까요?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct polyNode { /*<--poly node 구조체-->*/
int coefficient;
int exponent;
struct polyNode *link; /*<--링크 필드 설정-->*/
} polyNode;
typedef struct { /*<--헤더 구성 구조체-->*/
polyNode *head;
} polyList_h;
void deleteLastNode(polyList_h *L) {
polyNode *previous;
polyNode *current;
if(L->head == NULL) return;
if(L->head->link == NULL) {
free(L->head);
L->head = NULL;
return;
} else {
previous = L->head;
current = L->head->link;
while(current->link != NULL) {
previous = current;
current = current->link;
}
free(current);
previous->link = NULL;
}
}
void addFirstNode(polyList_h *P, int c, int e) {
polyNode *newNode;
newNode = (polyNode *)malloc(sizeof(polyNode));
newNode->coefficient = c;
newNode->exponent = e;
if(P->head == NULL) {
newNode->link = NULL;
P->head = newNode;
} else {
newNode->link = P->head;
P->head = newNode;
}
}
void freepolyList_h(polyList_h *L) {
polyNode *p;
while(L->head != NULL) {
p = L->head;
L->head = L->head->link;
free(p);
p = NULL;
}
}
/*<-- poly list 생성 -->*/
polyList_h *zeroP() {
polyList_h *P;
P = (polyList_h *)malloc(sizeof(polyList_h));
P->head = NULL;
return P;
}
/*<-- null 체크-->*/
bool isZeroP(polyList_h *P) {
if(P->head == NULL) return true;
else return false;
}
/*<--지수값가져오기-->*/
int coef(polyList_h *P, int e) {
int exponent;
if(P->head == NULL) {
exponent = 0;
} else {
polyNode *temp;
temp = P->head;
while(temp != NULL) {
if(e == temp->exponent) {
exponent = temp->coefficient;
break;
} else {
temp = temp->link;
exponent = 0;
}
}
}
return exponent;
}
/*<--첫번째 노드의 지수 가져오기-->*/
int maxExp(polyList_h *P) {
if(P->head == NULL) {
return 0;
} else {
return P->head->exponent;
}
}
/*<--마지막 노드 집어넣기-->*/
void addTerm(polyList_h *P, int c, int e) {
polyNode *newNode;
polyNode *temp;
newNode = (polyNode *)malloc(sizeof(polyNode));
newNode->coefficient = c;
newNode->exponent = e;
newNode->link = NULL;
if(P->head == NULL) {
P->head = newNode;
return;
}
temp = P->head;
while(temp->link != NULL) {
temp = temp->link;
}
temp->link = newNode;
}
/*<--첫번째 다항식 삭제-->*/
void delTerm(polyList_h *P) {
if(P->head == NULL) return;
if(P->head->link == NULL) {
free(P->head);
P->head = NULL;
return;
} else {
polyNode *temp;
temp = P->head;
P->head = temp->link;
free(temp);
}
}
/*<--P1*P2 (곱셈연산)-->*/
polyList_h *polyMult(polyList_h *P1, polyList_h *P2) {
polyList_h *P;
P = zeroP();
while(!isZeroP(P2)) {
int coff = coef(P1,maxExp(P1)) * coef(P2,maxExp(P2)); /*<--계수는 서로 곱해준다-->*/
int exp = maxExp(P1) + maxExp(P2); /*<--지수는 서로 더해준다-->*/
addTerm(P,coff,exp);
delTerm(P2);
}
return P;
}
/*<--화면 출력 부분-->*/
void printList(polyList_h *L) {
polyNode *p;
p = L->head;
while(p != NULL) {
printf("%dx^%d", p->coefficient,p->exponent);
p = p->link;
if(p != NULL) {
printf("+");
}
}
printf("\n");
}
void main() {
int i = 0;
char *fname_i = "hw1.inp";
FILE *input_d;
if ((input_d = fopen(fname_i,"r"))==NULL){
printf("파일이 열리지 않았습니다!!\n");
exit(1);
}
/*<--곱셈 시작-->*/
int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
int e1 = 0, e2 = 0, e3 = 0, e4 = 0;
polyList_h *P1;
polyList_h *P2;
P1 = zeroP();
P2 = zeroP();
/*<--첫번째 다항식-->*/
fscanf(input_d,"%d %d %d %d",c1,e1,c2,e2);
addTerm(P1,c1,e1);
addTerm(P1,c2,e2);
/*<--두번째 다항식-->*/
fscanf(input_d,"\nd% d% d% d% d%",c3,e3,c4,e4);
addTerm(P2,c3,e3);
addTerm(P2,c4,e4);
fclose(input_d);
/*<--값출력-->*/
printf("----------PolyMult--------------\n");
printList(P1);
printList(P2);
printf("-------------------------------\n");
printList(polyMult(P1,P2));
}