Data Structure Learning (C ++) - Single List Application (One Dollar Polytex [1])

zhaozj2021-02-16  50

I finally gotten here, at this time, you will be very proud to say that the Single-Latisful list of Xinxing is finally known to do anything. But unfortunately, if you look like me, it is here, you may suffer when you have a two-way linked list. If you are in the introduction of the book, you can make a step in step, you can debug the multiplicated addition function on the book, I will pay attention to you.

When I said this, I remembered that I sent a single-chain list, some people gave me suggestion: It is best to separate the linked list and linked list. Yes, the C standard library is doing this, and I am not an expert, and I can't prove any advantage; however, for beginners, a class is better than two kinds of good operations. I don't know that the original book is not debugged, but I don't understand this statement:

ListNode * PA, * PB, * PC, * P;

Listiterator AITER (AH.POLY);

Listiterator Biter (ah.poly);

PA = pc = aiter.first (); PB = P = Biter.First ();

............................

PA-> Coef = PA-> Coef PB-> Coef;

P = Pb; PB = biter.next (); delete P;

If you don't have a book, let me explain. What is PA, PB, P? You said this is clear, ListNode This node is. But according to the definition of the original book, the listiterator :: first (), etc. The function returns to the pointer to the DATA domain, how can they assign a value directly? The following is more chaotic, and the PB pointing area directly disks a data member of Term, which means to the Term structure; then let ListNode type pointer P point to this Term structure, finally, actually put this structure delete , God, ListNode The DATA domain of such a node is delete!

If you start from the basic node operation, no one will get so messy. But it is because there is a few classes, many things are negligent. Therefore, I don't doubt the standard library's approach, just for beginners, it is best to operate only to one class. I have done this program based on my definition. I don't appreciate the in situ operation, the polynomial addition ( ), POLYA POLYB, and then b is gone, A will have a bunch (or less a bunch); how do you do intj intk? I have no change in J and K. In this way, heavy duty " " is not as well as Polya.Add (polyb) or PolyAdd (Polya, Polyb).

One yuan multi-model definition and implementation

#ifndef polynomial_h

#define polynomial_h

#include "list.h"

Class Term

{

PUBLIC:

Int Coef;

int Exp;

TERM (): Coef (0), Exp (0) {}

Term (int C, Int E): COEF (C), Exp (E) {}

Term (INT C): Coef (c), Exp (0) {}

}

Class Polynomial: List

{

PUBLIC:

Void input ()

{

Cout << Endl << "Input Coefficients and Index"; COUT << Endl << "Note: Please enter each item in descending order, input coefficient 0 indicate end" << endl;

INT COEF, Exp;

For (int i = 1; i )

{

Cong << "" "" << i << "is the coefficient:";

CIN >> COEF;

IF (COEF)

{

Cout << "Index:";

CIN >> Exp;

Term Term (COEF, Exp);

INSERT (TERM);

}

Else Break;

}

}

Void print ()

{

Cout << Endl;

First ();

IF (! iSempty ())

{

Term * p = next ();

COUT << p-> Coef;

IF (p-> eXP)

{

Cout << "x";

IF (p-> exp! = 1) cout << "^" << p-> exp;

}

While (NEXT ()! = NULL)

{

p = get ();

IF (P-> Coef> 0) COUT << " ";

COUT << p-> Coef;

IF (p-> eXP)

{

Cout << "x";

IF (p-> exp! = 1) cout << "^" << p-> exp;

}

}

}

Cout << Endl;

}

Friend Void Polyad (Polynomial & Polya, Polynomial & Polyb)

{

Node * PA, * Pb;

POLYA.FIRST (); polyb.first ();

PA = polya.pnext (); pb = polyb.pnext ();

While (Pa! = null && pb! = null)

{

IF (PA-> DATA.EXP == PB-> DATA.EXP)

{

PA-> data.coef = pa-> data.coef pb-> data.coef;

Polyb.remove ();

IF (! PA-> DATA.COEF) polya.remove ();

Else Polya.pnext ();

}

Else

{

IF (PA-> DATA.EXP> PB-> DATA.EXP)

{

Polyb.premove ();

POLYA.INSERTBEFORE (PB);

}

Else IF (PA-> Data.exp Data.exp) polya.pnext ();

}

PA = polya.pget (); pb = polyb.pget ();

}

IF (pa == null)

{

POLYA.PGETPRIOR () -> link = Pb;

Polyb.pgetPrior () -> link = null;

}

}

}

#ENDIF

转载请注明原文地址:https://www.9cbs.com/read-22996.html

New Post(0)