연결형 자료구조를 이용한 다항식의 계산 프로그래밍

목차

없음

본문내용

#include
#include

// 다항식 항을 표현하는 구조체
typedef struct Term {
int coef; // 계수
int exp; // 차수
struct Term* next; // 다음 항을 가리키는 포인터
} Term;

// 다항식을 입력받아 연결 리스트로 구성하는 함수
Term* inputPolynomial(int termCount) {
Term* head = NULL;
Term* tail = NULL;

for (int i = 0; i < termCount; ++i) { int coef, exp; printf("계수와 차수를 차례대로 입력하세요 (예: 2 3): "); scanf("%d %d", &coef, &exp); if (coef != 0 && exp >= 0 && exp <= 3) { Term* newTerm = (Term*)malloc(sizeof(Term)); newTerm->coef = coef;
newTerm->exp = exp;
newTerm->next = NULL;

if (tail == NULL) {
head = tail = newTerm;
} else {
tail->next = newTerm;

출처 : 해피캠퍼스

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다