연결리스트 다항식 뺼셈
내꼬야
질문 제목 : 연결리스트 뺄셈 하는건데용 쉽지 않네요;;head를 정의한거 같은데 뭐가 문제죠? ㅜ질문 내용 : 문제 찾아주시고 그 문제에 대해서 자세히 설명좀 부탁드릴게요 아리까리 해서 자세히 부탁드려요 ㅎ#includestdio.h
#includestdlib.h
#includewindows.h
typedef struct listnode {
int coef;
int expo;
int length;
struct listnode *link;
} listnode;
listnode* getnode(listnode *p, int coef, int expo, int *cnt)
{
p = (listnode*)malloc(sizeof(listnode));
p-coef = coef;
p-expo = expo;
p-link = null;
p-length = *cnt;
return p;
}
listnode* getpoly()
{
listnode *p = null;
listnode *head = p;
int a,b,cnt=0,i=0;
while(i3) {
printf(계수 입력 : );
scanf(%d,&a);
printf(차수 입력 : );
scanf(%d,&b);
cnt++;
p = getnode(p,a,b,&cnt);
i++;
}
return head;
}
listnode* subtractpoly(listnode *a, listnode *b)
{
listnode *c = (listnode*)malloc(sizeof(listnode));
listnode *head = c;
while(a != null && b != null) {
if(a-expo == b-expo) {// a의 차수 == b의 차수
c-coef = a-coef - b-coef;
a = a-link;
b = b-link;
c = c-link;
}
else if(a-expo b-expo) { // a의 차수 b의 차수
c-coef = a-coef;
c-expo = a-expo;
c = c-link;
a = a-link;
}
else {// a의 차수 b의 차수
c-coef = b-coef;
c-expo = b-expo;
c = c-link;
b = b-link;
}
}
for( ;a != null;a = a-link) { // a 데이터가 남아있으면
c-coef = a-coef;
c-expo = a-expo;
c = c-link;
a = a-link;
}
for( ;b != null;b = b-link) { // b 데이터가 남아있으면
c-coef = b-coef;
c-expo = b-expo;
c = c-link;
b = b-link;
}
return head;
}
void displaypoly(listnode *a)
{
while(a != null) {
if(a != null)
printf(+);
printf(%dx(%d) ,a-coef,a-expo);
a = a-link;
}
}
void print(listnode *a,listnode *b, listnode *c)
{
while(a != null) {
if(a != null)
printf(+);
printf(%dx(%d) ,a-coef,a-expo);
a = a-link;
}
while(b != null) {
if(b != null)
printf(+);
printf(%dx(%d) ,b-coef,b-expo);
b = b-link;
}
while(c != null) {
if(c != null)
printf(+);
printf(%dx(%d) ,c-coef,c-expo);
c = c-link;
}
}
int main()
{
listnode *a,*b,*c;
a = getpoly(); // 첫 번째 다항식 출력
b = getpoly(); // 두 번째 다항식 출력
c = subtractpoly(a,b); // 다항식 덧셈 수행
displaypoly(a); // 한 개의 다항식 출력
//print(a,b,c); // 입력 다항식 출력
return 0;
}