每一个点开我的文章的小伙伴大家好!
在发这篇文章的时候,我是一名大一的学生,我将在博客记载我的学习之路!
这是我的第一篇博客文章,是关于C语言数据结构的单链表一元多项式的操作。
接下来我会把各个操作给分解 文章末尾附上源码(Visual Studio2022完美运行)!
我们直接进入正题吧!
一、首先是代码开头的各种定义:
#define _CRT_SECURE_NO_WARNINGS //加上这个,我们就不用scanf_s,可以用scanf了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int coef; //表示系数
int exp; // 表示指数
struct node* next; //指向下一个结点的指针
};
typedef struct node* Pnode;
二、输出单链表:
我写了两种写法
void Print1(Pnode L) //写法一
{
Pnode p = L->next; //创建的链表头结点是没有值的,所以从L的下一个结点开始输出内容
printf("%dx^%d", p->coef, p->exp);
p = p->next;
while(p)
{
printf("+%dx^%d", p->coef, p->exp);
p = p->next;
}
printf("\n");
}
void Print2(Pnode L) //写法二
{
Pnode p = L->next; //创建的链表头结点是没有值的,所以从L的下一个结点开始输出内容
printf("%dx^%d", p->coef, p->exp);
p = p->next;
for (; p;) // 对这一步有疑问的小伙伴可以去了解一下for循环的用法
{
printf("+%dx^%d", p->coef, p->exp);
p = p->next;
}
printf("\n");
}
三、建立多项式:
我们创建的单链表,表头是不设置值的(我的个人习惯0.0)。
这里我也有两种写法,一种是头插法,一种是尾插法。
Pnode Creat_toucha()
//多项式链表建立:以(系数,指数)方式输入项建立多项式,返回所建立的链表的头结点
{
Pnode head, tail;
//这里用的头插法 链表内的顺序与输入的顺序相同 如果按由大到小顺序输入的话,输出就是从小到大
head = (Pnode)malloc(sizeof(struct node));
tail = NULL;
int coef = 0, exp = 0;
while (1)
{
printf("输入系数:\n");
scanf("%d", &coef);
if (coef == 0)
break;
printf("输入指数:\n");
scanf("%d", &exp);
Pnode p = (Pnode)malloc(sizeof(struct node));
if (p != NULL) {
p->coef = coef; p->exp = exp;
p->next = head->next;
}
head->next = p;
p->next = tail;
tail = p;
}
return head;
}
Pnode Creat_Weicha()
//多项式链表建立:以(系数,指数)方式输入项建立多项式,返回所建立的链表的头结点
{
Pnode head, s; //这里用的尾插法 链表内的顺序与输入的顺序相同
head = (Pnode)malloc(sizeof(struct node));
s = head;
s->next = NULL;
int coef = 0, exp = 0;
while (1)
{
printf("输入系数:\n");
scanf("%d", &coef);
if (coef == 0)
break;
printf("输入指数:\n");
scanf("%d", &exp);
Pnode p = (Pnode)malloc(sizeof(struct node));
if (p != NULL) {
p->coef = coef; p->exp = exp;
p->next = head->next;
}
p->next = s->next;
s->next = p;
s = p;
}
return head;
}
四、对一元多项式进行有小到大的排列:
我用的冒泡排序法,我这里只写了一种,其实还有别的更高效的方法可以使用
Pnode Rank_mp(Pnode L) //冒泡排序法
{
Pnode head, pre1, pre2, p, q, r;
head = L;
pre1 = L;
pre2 = L->next;
p = L->next;
q = L->next->next;
for (head = L; pre1->next != NULL; pre1 = pre1->next) {
p = pre1->next;
pre2 = pre1->next;
q = p->next;
r = p->next;
while (q != NULL)
{
if (p->exp > q->exp)
{
pre1->next = q;
r = q->next;
pre2->next = r;
q->next = p;
p = pre1->next;
q = r;
}
else {
pre2 = pre2->next;
q = q->next;
r = r->next;
}
}
}
return L;
}
五、两个一元多项式的相加:
Pnode Add(Pnode L1, Pnode L2)
//③ 多项式相加:实现两个多项式相加操作。操作生成一个新的多项式,原有的两个多项式不变,返回生成的多项式的头结点;
{
Pnode L, s, r;
L = (Pnode)malloc(sizeof(struct node));
s = L;
s->next = NULL;
Pnode p, q;
p = L1->next; q = L2->next;
int temp1=0, temp2=0; //作为系数和指数的中间交换变量
while (p && q) //p q都不为空,L1 L2都没到尽头时
{
if (p->exp < q->exp)
{
r = (Pnode)malloc(sizeof(struct node));
temp1 = p->coef; r->coef = temp1;
temp2 = p->exp; r->exp = temp2;
p = p->next;
r->next = s->next;
s->next = r;
s = r;
}
else if(p->exp>q->exp) {
r = (Pnode)malloc(sizeof(struct node));
temp1 = q->coef; r->coef = temp1;
temp2 = q->exp; r->exp = temp2;
q = q->next;
r->next = s->next;
s->next = r;
s = r;
}
else if (p->exp == q->exp && p->coef + q->coef != 0)
{
r = (Pnode)malloc(sizeof(struct node));
temp1 = p->coef + q->coef; r->coef = temp1;
temp2 = p->exp; r->exp = temp2;
p = p->next; q = q->next;
r->next = s->next;
s->next = r;
s = r;
}
else if (p->exp == q->exp && p->coef + q->coef == 0)
{
p = p->next; q = q->next;
}
}
while (p)
{
r = (Pnode)malloc(sizeof(struct node));
temp1 = p->coef; r->coef = temp1;
temp2 = p->exp; r->exp = temp2;
p = p->next;
r->next = s->next;
s->next = r;
s = r;
}
while (q)
{
r = (Pnode)malloc(sizeof(struct node));
temp1 = q->coef; r->coef = temp1;
temp2 = q->exp; r->exp = temp2;
q = q->next;
r->next = s->next;
s->next = r;
s = r;
}
return L;
}
六、main函数的:
int main()
{
Pnode L1,L2,L11,L22,L;
printf("创建第一个一元多项式 L1:\n");
L1 = Creat();
printf("创建成功!\n");
printf("输出第一个一元多项式为:\n");
Print1(L1);
printf("输出成功\n");
printf("排序后的一元多项式为:\n");
L11=Rank_mp(L1);
Print1(L11);
printf("创建第二个一元多项式 L1:\n");
L2 = Creat();
printf("创建成功!\n");
printf("输出第二个一元多项式为:\n");
Print1(L2);
printf("输出成功\n");
printf("排序后的二元多项式为:\n");
L22=Rank_mp(L2);
Print1(L2);
L = Add(L11, L22);
printf("两个多项式相加的结果为:\n");
Print1(L);
printf("输出成功!\n");
return 0;
}
Visual Studio2022完美运行的源码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int coef; //表示系数
int exp; // 表示指数
struct node* next; //指向下一个结点的指针
};
typedef struct node* Pnode;
void Print1(Pnode L) //写法一
{
Pnode p = L->next;
printf("%dx^%d", p->coef, p->exp);
p = p->next;
while(p)
{
printf("+%dx^%d", p->coef, p->exp);
p = p->next;
}
printf("\n");
}
void Print2(Pnode L) //写法二
{
Pnode p = L->next;
printf("%dx^%d", p->coef, p->exp);
p = p->next;
for (; p;)
{
printf("+%dx^%d", p->coef, p->exp);
p = p->next;
}
printf("\n");
}
Pnode Creat()//多项式链表建立:以(系数,指数)方式输入项建立多项式,返回所建立的链表的头结点
{
Pnode head, tail;
head = (Pnode)malloc(sizeof(struct node));
tail = NULL;
int coef = 0, exp = 0;
while (1)
{
printf("输入系数:\n");
scanf("%d", &coef);
if (coef == 0)
break;
printf("输入指数:\n");
scanf("%d", &exp);
Pnode p = (Pnode)malloc(sizeof(struct node));
if (p != NULL) {
p->coef = coef; p->exp = exp;
p->next = head->next;
}
head->next = p;
p->next = tail;
tail = p;
}
return head;
}
Pnode Rank_mp(Pnode L) //冒泡排序法
{
Pnode head, pre1, pre2, p, q, r;
head = L;
pre1 = L;
pre2 = L->next;
p = L->next;
q = L->next->next;
for (head = L; pre1->next != NULL; pre1 = pre1->next) {
p = pre1->next;
pre2 = pre1->next;
q = p->next;
r = p->next;
while (q != NULL)
{
if (p->exp > q->exp)
{
pre1->next = q;
r = q->next;
pre2->next = r;
q->next = p;
p = pre1->next;
q = r;
}
else {
pre2 = pre2->next;
q = q->next;
r = r->next;
}
}
}
return L;
}
Pnode Add(Pnode L1, Pnode L2)//③ 多项式相加:实现两个多项式相加操作。操作生成一个新的多项式,原有的两个多项式不变,返回生成的多项式的头结点;
{
Pnode L, s, r;
L = (Pnode)malloc(sizeof(struct node));
s = L;
s->next = NULL;
Pnode p, q;
p = L1->next; q = L2->next;
int temp1=0, temp2=0; //作为系数和指数的中间交换变量
while (p && q) //p q都不为空,L1 L2都没到尽头时
{
if (p->exp < q->exp)
{
r = (Pnode)malloc(sizeof(struct node));
temp1 = p->coef; r->coef = temp1;
temp2 = p->exp; r->exp = temp2;
p = p->next;
r->next = s->next;
s->next = r;
s = r;
}
else if(p->exp>q->exp) {
r = (Pnode)malloc(sizeof(struct node));
temp1 = q->coef; r->coef = temp1;
temp2 = q->exp; r->exp = temp2;
q = q->next;
r->next = s->next;
s->next = r;
s = r;
}
else if (p->exp == q->exp && p->coef + q->coef != 0)
{
r = (Pnode)malloc(sizeof(struct node));
temp1 = p->coef + q->coef; r->coef = temp1;
temp2 = p->exp; r->exp = temp2;
p = p->next; q = q->next;
r->next = s->next;
s->next = r;
s = r;
}
else if (p->exp == q->exp && p->coef + q->coef == 0)
{
p = p->next; q = q->next;
}
}
while (p)
{
r = (Pnode)malloc(sizeof(struct node));
temp1 = p->coef; r->coef = temp1;
temp2 = p->exp; r->exp = temp2;
p = p->next;
r->next = s->next;
s->next = r;
s = r;
}
while (q)
{
r = (Pnode)malloc(sizeof(struct node));
temp1 = q->coef; r->coef = temp1;
temp2 = q->exp; r->exp = temp2;
q = q->next;
r->next = s->next;
s->next = r;
s = r;
}
return L;
}
int main()
{
Pnode L1,L2,L11,L22,L;
printf("创建第一个一元多项式 L1:\n");
L1 = Creat();
printf("创建成功!\n");
printf("输出第一个一元多项式为:\n");
Print1(L1);
printf("输出成功\n");
printf("排序后的一元多项式为:\n");
L11=Rank_mp(L1);
Print1(L11);
printf("创建第二个一元多项式 L1:\n");
L2 = Creat();
printf("创建成功!\n");
printf("输出第二个一元多项式为:\n");
Print1(L2);
printf("输出成功\n");
printf("排序后的二元多项式为:\n");
L22=Rank_mp(L2);
Print1(L2);
L = Add(L11, L22);
printf("两个多项式相加的结果为:\n");
Print1(L);
printf("输出成功!\n");
return 0;
}
如果小伙伴们对哪个函数有疑问的话可以私信我,我可以解答你,想要源码文档的也可以私信我!
在最后祝愿大家能够不断学习并且提升自己!