연결리스트로 다항식 덧셈 구하는 프로그램 퍼왔는데 빨간색 부분 이해가안가는데 설명좀 부탁드립니다. ㅠㅠ
뱐헀어
질문 제목 : 연결리스트로 다항식 덧셈 구하는 프로그램 퍼왔는데 빨간색 부분 이해가안가는데 설명좀 부탁드립니다. ㅠㅠ연결리스트 다항식 덧셈 프로그램 이해 안가는 부분질문 내용 :
#include stdio.h
#include stdlib.h
typedef struct listnode { //리스트 노드 구조
int coef; //계수
int exp; //지수
struct listnode *link;
} listnode;
typedef struct listheader { //리스트 헤더 노드 구조
int length;
listnode *head; //*head 이게 포인터 함수로 알고있는데 포인터함수를 잘모르겠거든요... 설명이나 포인터함수에대해 잘 나와있는곳 링크좀 부탁드립니다. ㅠ
listnode *tail;
} listheader;
void init(listheader *plist) // 초기화, 공백 헤더 노드
{
plist-length = 0; // -연산이 뭐죠? 처음봐서요.. ㅠㅠ
plist-head = plist-tail = null; // head와 tail을 null으로
}
// 연결 리스트에 삽입
// plist는 헤더를 가리키는 포인터, coef는 계수, exp는 지수
void insert_node_last(listheader *plist, int coef, int exp)
{
//공백 연결리스트 생성
listnode *temp = (listnode *)malloc(sizeof(listnode)); // (listnode *)malloc(sizeof(listnode)) 이부분에서 괄호안에 왜 *딸랑 하나 넣었고 malloc가 뭐죠...? 이게 전체적으로 뭘 나타내는거죠?
if(temp == null){
exit(1); // exit가 프로그램을종료하는건가요? 아니면 함수를 종료하는건가요?
}
temp-coef=coef;
temp-exp=exp;
temp-link=null;
if(plist-tail == null){
plist-head = plist-tail = temp;
}
else {
plist-tail-link = temp;
plist-tail = temp;
}
plist-length++; //length 증가
}
// 다항식 덧셈
// 다항식3 = 다항식1 + 다항식2
void poly_add(listheader *plist1, listheader *plist2, listheader *plist3)
{
listnode *a = plist1-head;
listnode *b = plist2-head;
int sum; //계수를 담을 변수
while(a && b){
if(a-exp == b-exp){ //지수가 같을경우
sum = a-coef + b- coef;
if( sum != 0 ) insert_node_last(plist3, sum, a-exp);
a=a-link; b=b-link;
}
else if(a-exp b-exp){ //다항식1의 지수가 클경우
insert_node_last(plist3, a-coef, a-exp);
a=a-link;
}
else {
insert_node_last(plist3, b-coef, b-exp);
b=b-link;
}
}
//남아있는 항들을 모두 다항식3으로 복사
for(; a != null; a=a-link)
insert_node_last(plist3, a-coef, a-exp);
for(; b != null; b=b-link)
insert_node_last(plist3, b-coef, b-exp);
}
//다항식 출력
void poly_print(listheader *plist)
{
listnode *p=plist-head;
for(;p;p=p-link){
if (p-coef == 0 || p-exp == 0){
printf(); //계수 or 지수가 0이면 표시하지 않음
}else if (p-exp == 1){
printf(%d,p-coef); //지수가 1이면 계수만 표시
}else{
printf(%dx^%d, p-coef, p-exp);
//계수 or 지수가 0이 아니면 계수x^지수 형태로 표시
if (p-link == null)
{
printf();
}else{
printf( + );
}
}
}
printf(\n);
}
void main()
{
listheader list1, list2, list3; //다항식 입력받을 변수 선언
init(&list1);//init 함수 호출로 공백 리스트
init(&list2);
init(&p;init(&list3);
int a,b; //항의 계수와 지수를 입력받기 위한 변수
//다항식1을 입력받는 부분
printf(다항식1의 항(계수,지수)을 입력하세요. (0 0 이면 입력종료)\n);
while (1)
{
scanf(%d %d,&a,&b);
if (a==0 && b==0) // 제가 해깔려서 그러는데 && 이랑 || 이거 있자나요 뭔차이죠? 그리고 여기서 a값이 아니라 b값만 0들어와도 while문을 탈출하는걸 만들어야하는데요 왜 a==0이부분 지우면 이상한 값 출력되고 여전히 왜 둘다 0 0 이어야지 종료되는지 모르겠네요ㅠ
{
break;
}
insert_node_last(&list1, a, b);
}
printf(다항식1 : );
poly_print(&list1); //다항식1 출력
printf(\n);
//다항식2을 입력받는 부분
printf(다항식2의 항(계수,지수)을 입력하세요. (0 0 이면 입력종료)\n);
while (1)
{
scanf(%d %d,&a,&b);
if (a==0 && b==0)
{
break;
}
insert_node_last(&list2, a, b);
}
printf(다항식2 : );
poly_print(&list2); //다항식2 출력
printf(\n);
// 다항식3 = 다항식1 + 다항식2
poly_add(&list1, &list2, &list3);
printf(결과 : );
poly_print(&list3); //다항식3 출력
}