The first week of learning data structure (simple addition of univariate nth degree polynomials with linked list)

        The addition of univariate polynomials, Xiaobai's work, simply realizes the addition of univariate N-degree polynomials (multiplication is the same principle, so there is no desire to write, let's stand up). (it's just a record, without any teaching significance)

        In fact, at the beginning, it was implemented using arrays, but I felt that the smell of arrays was still too strong and did not match with the linked list, so I began to practice with a real linked list. Because I had to go to class, I continued to write for three days. At the beginning, I didn't want to write so much. As a result, it was realized that one function had to get another data first, which was realized by writing a function, So it's outrageous to write the last subfunction, which can make the results output in the order of exponents from large to small, and the congeners are not merged, and the most fatal thing is that the minimum exponents of the two polynomials must be the same, which is too lazy to write this function. Explain it in the article.

        

struct List
{
	int factor;
	int exponent;		
	struct List* next;
};

        This is the linked List definition section. I found that if typedef is * List and then use * List to define the subsequent function, there will be many inexplicable errors in the function, but it is taught in the book, so it is unknown, so I dare not use it.

void intialization(struct List** polynomial)//Initialize a polynomial;
{
	int count, f, e;
	printf("Please enter a polynomial number of items:");//You need to input the total number of polynomial items;
	scanf_s("%d", &count);
	for (int i = 1; i <= count; i++)
	{
		printf("Please enter factor and exponent for %dth\n", i);
		scanf_s("%d%d", &f, &e);
		insertNode(polynomial, f, e);//f is the coefficient, e is the exponent, and insertNode is an insertion function;
	}
}

        New linked list is used.

	struct List* Apoly = NULL;
	intialization(&Apoly);

         The definition is generally like this (mainly because the automatic creation of linked list is too wrong for me, so I give up directly):         

void insertNode(struct List** last, int ratio, int chara)//Insert the coefficient and index at the end of the linked list;
{
	struct List* substitute = (*last);
	struct List* new_Node = (struct List*)malloc(sizeof(struct List));
	new_Node->factor = ratio;
	new_Node->exponent = chara;
	new_Node->next = NULL;
	if ((*last) == NULL)
	{
		(*last) = new_Node;
		return;
	}
	while (substitute->next != NULL)
		substitute = substitute->next;
	substitute->next = new_Node;
}

        This is an insertion function, in which the pointer of the pointer is used. At that time, there was no pointer concept. The head of the breakpoint insertion was numb and I didn't know why it couldn't get in. Later, I found a group and asked the boss. The boss recommended a website to me and said that I should follow it, so I could figure it out a little bit. (huge dish)

         

void collate(struct List** head_ref)//Let the polynomials be arranged from large to small according to the exponent;
{
	struct List* stroke = (*head_ref);
	struct List* context = (*head_ref);
	struct List* coincidence = NULL;
	int count = Listlength(stroke);//Determine the number of cycles through the length of the linked list;
	int bigflag, flagbig;
	while (count--)
	{	
		int position;
		bigflag = context->exponent;//Index;
		flagbig = context->factor;//Coefficient;
		for (stroke = context; stroke != NULL; stroke = stroke->next)
		{

			if (stroke->exponent > bigflag)//If the coefficient is found to be large, replace it;
			{
				bigflag = stroke->exponent;
				flagbig = stroke->factor;
			}
		}
		position=searchNode(context, bigflag);//Find the node position of the maximum index;
		insertNode(&coincidence, flagbig, bigflag);//Insert the maximum index and its coefficient;
		deletNode(&context, position);//Delete that node and continue the loop;
	}
	(*head_ref) = coincidence;//Assign to the header node;
}

        I always think my method is extremely stupid. I use a lot of functions, which is equivalent to creating a new node to put one item into the maximum index, but I don't see what others do. I can think of this.

void deletNode(struct List** head_ref, int position)//Delete the node on position;
{
	if ((*head_ref) == NULL)
		return;
	struct List* temp = (*head_ref);
	struct List* ereas = (*head_ref);
	if (position == 0)
	{
		(*head_ref) = (*head_ref)->next;
		free(ereas);
		return;
	}
	else
		while (position--)
		{
			temp = ereas;
			ereas=ereas->next;
		}
	temp->next = ereas->next;
	free(ereas);
}

        In fact, as long as the head node may be involved, it is OK to use the pointer of the pointer (lazy man).

int Listlength(struct List* head)//Calculate the length of the linked list;
{
	int count = 0;
	while (head!=NULL)
	{
		count++;
		head = head->next;
	}
	return count;
}
int searchNode(struct List* head, int expoly)//Find the node location of a specific index;
{
	int last = 0;
	while (head->exponent != expoly)
	{
		head = head->next;
		last++;
	}
	return last;
}
void printList(struct List* pList)//Print a polynomial;
{
	printf("The polynomial is :");
	while (pList != NULL)
	{
		if (pList->factor > 0)
			printf("+");
		printf("%dX^%d  ", pList->factor, pList->exponent);
		pList = pList->next;
	}
}

        These are small programs. They all write that they need some parameters, and then write a function. It's annoying.

void addPolynomial(struct List** summation, struct List* addend, struct List* augend)//Addition of polynomials;
{
	while (addend != NULL && augend != NULL)
	{
		if (addend->exponent == augend->exponent)
		{
			insertNode(summation, ((addend->factor) + (augend->factor)), addend->exponent);
			addend = addend->next; augend = augend->next; continue;
		}
		if (addend->exponent > augend->exponent)
		{	
			insertNode(summation, addend->factor, addend->exponent);
			addend = addend->next; continue;
		}
		if (addend->exponent < augend->exponent)
		{
			insertNode(summation, augend->factor, augend->exponent);
			augend = augend->next; continue;
		}
	}

}

        Unparalleled garbage addition. In fact, if you want to add any two polynomials, you can add them later

if(A polynomial==NULL)
    copy B All values after the polynomial are followed.
Similarly B

        It is suggested to sort the polynomials first and then add them. Of course, you can also write a function to complete them at the same time. This addition is equivalent to creating a new C formula and adding the sum of them (suddenly it seems that there is no free, numb).

        In fact, the ambition at that time was to write the addition and multiplication of N-ary and N-degree polynomials, and finally wrote a unary one. However, I personally feel that this unary is still complete. Of course, as a linked list, it still lacks many operations, but it is almost alive. As for the merging and multiplication of the same exponential terms, it is just a matter of time, Personally, I feel that after understanding the double pointer, the writing behind it is extremely smooth. At most, it is a logical problem. The code segment is no longer a problem. (I suddenly think of the double pointer set of double pointers in the collate function. I was very flustered when I wrote it, but nothing happened at all)

This is a learning note or something similar. It doesn't mean to be a teacher.

Tags: C data structure linked list

Posted on Mon, 13 Sep 2021 14:05:57 -0400 by Zephyr_Pure