您的位置:首页 > 理论基础 > 数据结构算法

考研数据结构与算法之单链表多项式的计算(一)

2014-04-22 20:51 330 查看
销毁链表的时候出了一点问题,现在被卡住了,明天继续。

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct
{
int coef;		// 系数
int expn;		// 指数
}term,ElemType;
typedef struct Polyn
{
ElemType data;
Polyn* next;
}Polyn;
typedef struct Polyn *PolynList;
// 创建一段多项式
Status CreatePolyn(PolynList *P, int n);
// 销毁多项式
Status DestroyPolyn(PolynList *P);
// 显示多项式
Status PrintPolyn(PolynList P);
// 获取链表长度
int PolynLength(PolynList P);
// 多项式相加
Status AddPolyn(PolynList *PA, PolynList *PB);
// 多项式相剪
Status SubPolyn(PolynList *PA, PolynList *PB);
// 多项式相乘
Status MutPolyn(PolynList *PA, PolynList *PB);
int num1[10][2] ={1, 23,
2, 1,
4, 6,
5, 1,
9, 100,
11, 1,
12, 1,
13, 2,
18, 1,
45, 2};
int num2[10][2] ={0, 23,
1, 1,
4, 6,
11, 1,
28, 100,
31, 1,
32, 1,
33, 2,
38, 1,
55, 2};
int main(void)
{
PolynList list1;
CreatePolyn(&list1,7);
PrintPolyn(list1);
printf("%d",PolynLength(list1));
DestroyPolyn(&list1);
system("pause");
}
Status CreatePolyn(PolynList *P, int n)
{
PolynList q,s;
int i = 0, j = -1;
*P = (PolynList)malloc(sizeof(PolynList));
s = *P;
while(i<n)
{
q = (PolynList)malloc(sizeof(PolynList));
q->data.coef = i;
q->data.expn = i;
s->next = q;
s = q;
i++;
}
s->next = NULL;
return OK;
}
Status PrintPolyn(PolynList P)
{
PolynList q;
q = P->next;
printf("================================================================================\n");
printf("原式 = ");
do
{
printf(" %dA%d +", q->data.coef, q->data.expn);
q = q->next;
}while(q->next);
printf(" %dA%d;\n", q->data.coef, q->data.expn);
printf("================================================================================\n");
return OK;
}
int PolynLength(PolynList P)
{
PolynList q = P->next;
int i = 1;
while(q->next)
{
q = q->next;
i++;
}
return i;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: