C语言数据结构_单链表应用(多项式相加)

单链表的应用-多项式的相加

方法一

该代码的原理:首先创建两个链表tempList1和tempList2分别存放按照指数从小到大一定长度的数据。再申请五个指针p,q,r,s,temp指针p和q分别指向tempList1(tempList2)和tempList2(tempList1),s是free指数相同的结点,r将两个链表的结点相连以及连接free(s)结点的前后两个结点,按照我的代码首先p指向tempList1的第一个结点(头结点的下一个结点)q指向tempList2的第一个结点(头结点的下一个结点),然后进行结点所存储的指数的比较,分三种情况,第一:p的指数小于q的指数,r = p,p = p->next,然后继续比较p和q的指数的大小,第二:p的指数大于q的指数,这一种情况相当重要,将p的前一个结点与q连接,这个时候一定要注意一个细节此时的tempList1已经不再与p相连,相当于tempList1已经连接了tempList2,如果要进行下一步操作一定要将p和q交换位置,第三:p的指数和q的指数相等时,情况一:当系数相加不等于0时,p的指数直接等于p,q指数相加,然后s = q,q = q->next,free(s)。情况二:当系数相加等于0时,p,q都指向下一个结点,然后free两个指数相同的结点,此时问题来了,如果free了tempList1的结点,那么整个链表就没有连起来了,此时就要r->next = p,这样就可以重新将链表连起来,如果没有连有可能在打印链表的时候就会形成死循环,因为整个链表没有指向空的结点。最后在将提前结束的结点的NULL指向没有结束的结点。这样就完成了多项式的加法。

上代码

#include <stdio.h>
#include <malloc.h>

/**
 * Linked list of integers. The key is data. The key is sorted in non-descending order.
 */
typedef struct LinkNode{
	int coefficient;
	int exponent;
	struct LinkNode *next;
} *LinkList, *NodePtr;

/**
 * Initialize the list with a header.
 * @return The pointer to the header.
 */
LinkList initLinkList(){
	LinkList tempHeader = (LinkList)malloc(sizeof(struct LinkNode));
	tempHeader->coefficient = 0;
	tempHeader->exponent = 0;
	tempHeader->next = NULL;
	return tempHeader;
}// Of initLinkList

/**
 * Print the list.
 * @param paraHeader The header of the list.
 */
void printList(LinkList paraHeader){
	NodePtr p = paraHeader->next;
	while (p->next != NULL) {
		printf("%d * 10^%d + ", p->coefficient, p->exponent);
		p = p->next;
	}// Of while
	printf("%d * 10^%d",p->coefficient,p->exponent);
	printf("\r\n");
}// Of printList

/**
 * Print one node for testing.
 * @param paraPtr The pointer to the node.
 * @param paraChar The name of the node.
 */
void printNode(NodePtr paraPtr, char paraChar){
	if (paraPtr == NULL) {
		printf("NULL\r\n");
	} else {
		printf("The element of %c is (%d * 10^%d)\r\n", paraChar, paraPtr->coefficient, paraPtr->exponent);
	}// Of while
}// Of printNode

/**
 * Add an element to the tail.
 * @param paraCoefficient The coefficient of the new element.
 * @param paraExponent The exponent of the new element.
 */
void appendElement(LinkList paraHeader, int paraCoefficient, int paraExponent){
	NodePtr p, q;

	// Step 1. Construct a new node.
	q = (NodePtr)malloc(sizeof(struct LinkNode));
	q->coefficient = paraCoefficient;
	q->exponent = paraExponent;
	q->next = NULL;

	// Step 2. Search to the tail.
	p = paraHeader;
	while (p->next != NULL) {
		p = p->next;
	}// Of while

	// Step 3. Now add/link.
	p->next = q;
}// Of appendElement

/**
 * Polynomial addition.
 * @param paraList1 The first list.
 * @param paraList2 The second list.
 */
void add(NodePtr paraList1, NodePtr paraList2){
	NodePtr p, q, r, s,temp;
    //p,q分别指向tempList1和tempList2
    //r作用一:将tempList1和tempList2的结点相连,作用二:将删除的结点的前后结点相连
    //s删除指定结点
    //temp将p,q指向交换

	// Step 1. Search to the position.
	p = paraList1->next;
	printNode(p, 'p');
	q = paraList2->next;
	printNode(q, 'q');
	r = paraList1; // Previous pointer for inserting.
	printNode(r, 'r');
	free(paraList2); // The second list is destroyed. 
	
	while ((p != NULL) && (q != NULL)) {
		if (p->exponent < q->exponent) {
			//Link the current node of the first list.
			printf("case 1\r\n");
			r = p;
			printNode(r, 'r');
			p = p->next;
			printNode(p, 'p');
		} else if ((p->exponent > q->exponent)) {
			//Link the current node of the second list.
			printf("case 2\r\n");
			r->next = q;
			r = q;
			printNode(r, 'r');
			q = q->next;
			printNode(q, 'q');
			temp = p;//交换q,p的指向
			p = q;
			q = temp;
		} else {
			printf("case 3\r\n");
			//Change the current node of the first list.
			p->coefficient = p->coefficient + q->coefficient;
			printf("The coefficient is: %d.\r\n", p->coefficient);
			if (p->coefficient == 0) {
				printf("case 3.1\r\n");
				s = p;
				p = p->next;
				printNode(p, 'p');
				free(s);
				r->next = p;//这一步非常重要
			} else {
				printf("case 3.2\r\n");
				r = p;
				printNode(r, 'r');
				p = p->next;
				printNode(p, 'p');
			}// Of if
			s = q;
			q = q->next;
			//printf("q is pointing to (%d, %d)\r\n", q->coefficient, q->exponent);
			free(s);
		}// Of if

		printf("p = %ld, q = %ld \r\n", p, q);
	} // Of while
	printf("End of while.\r\n");

	if (p == NULL) {
		r->next = q;
	} else {
		r->next = p;
	} // Of if

	printf("Addition ends.\r\n");
}// Of add

/**
 * Unit test.
 */
void additionTest(){
	// Step 1. Initialize the first polynomial.
	LinkList tempList1 = initLinkList();
	appendElement(tempList1, 7, 2);
	appendElement(tempList1, 3, 3);
	appendElement(tempList1, 9, 8);
	appendElement(tempList1, 5, 9);
    appendElement(tempList1, 4, 11);
    appendElement(tempList1, 1, 20);
	printList(tempList1);

	// Step 2. Initialize the second polynomial.
	LinkList tempList2 = initLinkList();
	appendElement(tempList2, 8, 1);
	appendElement(tempList2, 22, 7);
	appendElement(tempList2, -9, 8);
	appendElement(tempList2, 6, 11);
	printList(tempList2);

	// Step 3. Add them to the first.
	add(tempList1, tempList2);
	printList(tempList1);
}// Of additionTest

/**
 * The entrance.
 */
void main(){
	additionTest();
	printf("Finish.\r\n");
}// Of main

运行结果

7 * 10^2 + 3 * 10^3 + 9 * 10^8 + 5 * 10^9 + 4 * 10^11 + 1 * 10^20
8 * 10^1 + 22 * 10^7 + -9 * 10^8 + 6 * 10^11
The element of p is (7 * 10^2)
The element of q is (8 * 10^1)
The element of r is (0 * 10^0)
case 2
The element of r is (8 * 10^1)
The element of q is (22 * 10^7)
p = 2037088, q = 2036832
case 2
The element of r is (7 * 10^2)
The element of q is (3 * 10^3)
p = 2036864, q = 2037088
case 1
The element of r is (3 * 10^3)
The element of p is (9 * 10^8)
p = 2036896, q = 2037088
case 2
The element of r is (22 * 10^7)
The element of q is (-9 * 10^8)
p = 2037120, q = 2036896
case 3
The coefficient is: 0.
case 3.1
The element of p is (6 * 10^11)
p = 2037152, q = 2036928
case 2
The element of r is (5 * 10^9)
The element of q is (4 * 10^11)
p = 2036960, q = 2037152
case 3
The coefficient is: 10.
case 3.2
The element of r is (10 * 10^11)
The element of p is (1 * 10^20)
p = 2036992, q = 0
End of while.
Addition ends.
8 * 10^1 + 7 * 10^2 + 3 * 10^3 + 22 * 10^7 + 5 * 10^9 + 10 * 10^11 + 1 * 10^20
Finish.

方法二

该方法是将tempList1的每一个结点的指数都与tempList2里面的小于该指数的结点进行比较,如果在tempList2里面找到了相同指数的结点,那么就直接将其free掉,如果系数相加等于零也将tempList1里面的结点也free掉,如果相加系数不等于零则将tempList1里面的结点保留,系数赋值为相加的系数,然后将tempList1的尾结点与tempList2的第一个结点(头结点的下一个结点)相连,最后将tempList1按照指数从小到大的顺序排列。

上代码

#include<stdio.h>
#include<stdlib.h>

struct Node
{
    int coefficient;
	int exponent;
    struct Node* next;
};

struct Node* createList()
{
    struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
    headNode->coefficient = 0;
    headNode->exponent = 0;
    headNode->next = NULL;
    
    return headNode;
}

struct Node* createNode(int coefficient,int exponent)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->coefficient = coefficient;
    newNode->exponent = exponent;
    newNode->next = NULL;

    return newNode;
}

void printList(struct Node* headNode)
{
    struct Node* pMove = headNode->next;
    while(pMove->next != NULL)
    {
        printf(" %d * 10^%d +",pMove->coefficient,pMove->exponent);
        pMove = pMove->next;
    }
    printf(" %d * 10^%d\r\n",pMove->coefficient,pMove->exponent);
}

void insertNodeBytail(struct Node* headNode,int coefficient,int exponent)
{
    struct Node* newNode = createNode(coefficient,exponent);
    struct Node* p = headNode;
    while(p->next != NULL)
    {
        p = p->next;
    }
    p->next = newNode;
}
//将tempList1的结点按照指数从小到大的顺序排列
void sortList(struct Node* headNode)
{
    struct Node* p = NULL;
    struct Node* q = NULL;
    struct Node* tempNode = createList();
    for(p = headNode->next; p != NULL; p = p->next)
    {
        for(q = p->next; q != NULL; q = q->next)
        {
            if(p->exponent > q->exponent)
            {
                tempNode->coefficient = p->coefficient;
                tempNode->exponent = p->exponent;
                p->coefficient = q->coefficient;
                p->exponent = q->exponent;
                q->coefficient = tempNode->coefficient;
                q->exponent = tempNode->exponent;
            }
        }
    }
}
//两个链表的多项式相加
void addList(struct Node* tempList1,struct Node* tempList2)
{
    struct Node* p = tempList1->next;
    struct Node* q = tempList2->next;
    struct Node* r = tempList2;//作用:当tempList2的结点的指数等于templist1的指数时删除tempList2结点
    struct Node* s;//充当删除结点
    struct Node* temp;//当结点相加的系数等于0时临时储存p的地址防止在删除templist1的结点时误删本来系数就等于0的结点
    int tempList1_exponent;//存储tempList1的指数
    int tempList2_exponent;//存储tempList2的指数
    while(p != NULL)
    {

        tempList1_exponent = p->exponent;
        tempList2_exponent = tempList2->next->exponent;
        while(tempList2_exponent <= tempList1_exponent) 
        {
            if(tempList1_exponent != tempList2_exponent)//不等就往tempList2的下一个结点走
            {
                r = q;
                q = q->next;
                if(q == NULL)//当q == NULL时q->exponent值没有程序就此终止所以这一步非常重要
                {
                    break;
                }
                tempList2_exponent = q->exponent;
            }else
            {
                p->coefficient = p->coefficient + q->coefficient;
                temp = p;
                s = q;
                q = q->next;
                r->next = q;
                free(s);
                if(q == NULL)
                {
                    break;
                }
                tempList2_exponent = q->exponent;
            }
        }  
        //s = p;
        p = p->next; //tempList1往下一个结点走
        if(temp->coefficient == 0)//如果相加等于零就要删除在templist1里面的指数为tempList1_exponent的结点
        {
            struct Node* m = tempList1;
            struct Node* n;
            while(m->next != NULL && m->next->exponent != tempList1_exponent)
            {
                m = m->next;
            }
            n = m->next;
            m->next = m->next->next;
            temp->coefficient = 1;//防止下一次循环误删必须赋值非零数
            free(n);
        }
        q = tempList2->next;
    }
    struct Node* pMove = tempList1;
    while(pMove->next != NULL)
    {
        pMove = pMove->next;
    }
    pMove->next = tempList2->next;
    sortList(tempList1);
    free(tempList2);
}

void test()
{
    struct Node* tempList1 = createList();
    insertNodeBytail(tempList1, -8, 1);
    insertNodeBytail(tempList1, 7 ,2);
    insertNodeBytail(tempList1, 3, 3);
    insertNodeBytail(tempList1, 9, 8);
    insertNodeBytail(tempList1, 5, 9);
    insertNodeBytail(tempList1, 0, 20);
    printList(tempList1);

    struct Node* tempList2 = createList();
    insertNodeBytail(tempList2, 8, 1);
    insertNodeBytail(tempList2, 22, 7);
    insertNodeBytail(tempList2, -9, 8);
    insertNodeBytail(tempList2, 6, 9);
    insertNodeBytail(tempList2, 0, 12);
    printList(tempList2);

    addList(tempList1,tempList2);
    printList(tempList1);

}

int main()
{
    test();
    return 0;
}

运行结果


 -8 * 10^1 + 7 * 10^2 + 3 * 10^3 + 9 * 10^8 + 5 * 10^9 + 0 * 10^20
 8 * 10^1 + 22 * 10^7 + -9 * 10^8 + 6 * 10^9 + 0 * 10^12
 7 * 10^2 + 3 * 10^3 + 22 * 10^7 + 11 * 10^9 + 0 * 10^12 + 0 * 10^20
 
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值