Data structure and algorithm basis (Wang Zhuo) (10): case analysis and implementation (polynomial; sparse polynomial; library management system (omitted);)

Table of contents

Creation of (polynomial) linear tables:

(Polynomial) linked list creation:

Referring to the PPT ideas, the program design process is as follows:

Addition, subtraction, and multiplication of (polynomial) linear tables:


Question stem:

Realize (sparse) polynomials in the form of linear lists and linked lists, respectively

  • Definition (Constructive Framework) Creation
  • Addition, subtraction, multiplication

Creation of (polynomial) linear tables:

(Slightly, it is not difficult compared to other operations, and I have time to write in the future)


(Polynomial) linked list creation:

If we write it, it is estimated that there is a high probability that we just enter a few items in the polynomial and we are done:

project 1:

#include<iostream>
using namespace std;
#include<stdlib.h>//存放exit

#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE  -1
#define OVERFLOW   -2   

#define MAXlength 100  //初始大小为100,可按需修改

typedef int Status;         //函数调用状态

struct Pnode//node:结; 结点;
{
    float coef;//coefficient:系数
    int expn;//指数
    Pnode* next;
};

typedef Pnode* Polynomial;


int CreatePoly(Polynomial& P, int n)
{
    //输入n项的系数和指数,建立:表示多项式的有序链表:P
    P = new Pnode;
    P->next = NULL;
    for (int i = 1; i <= n; ++i)
    {
        static Pnode* s = new Pnode;
        cin >> s->coef >> s->expn;
    }
    return true;
}
    
int main()
{

}

However, there are more perfect and suitable methods in PPT:

(Master tmd is a master, and his programming ideas are more perfect and systematic than mine, nb)

In short, on the basis of the previous steps, the following steps are added:


pre:

Find the first item whose exponent power is greater than the previous item, and the initial value points to the head node

Points to the predecessor of the first node to
be found that is greater than the index of the entry



Pointer q:

Initialize pointing to head node

Go down the chain, compare the
current node in the linked list with
the index of the input item one by one,
find the first node *q that is greater than the index of the input item,
insert the input item node *s before the node *q


Referring to the PPT ideas, the program design process is as follows:

Initialize pointers pre, q

Compare the size of the index item of the current input item with the next item one by one

If the current, input item(s) index item is greater:

Continue to look backwards, in the process:

After each item is compared, the pointers pre and q point to the next item (pointing to the next item once)

until the first term (q) that is greater than the exponential term of s is found

Then insert the input item (s) before the larger item (q)

enter next item

That is, each time a new item is entered, a comparison operation is performed from scratch:

project 2:

#include<iostream>
using namespace std;
#include<stdlib.h>//存放exit

#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE  -1
#define OVERFLOW   -2   

#define MAXlength 100  //初始大小为100,可按需修改

typedef int Status;         //函数调用状态

struct Pnode//node:结; 结点;
{
    float coef;//coefficient:系数
    int expn;//指数
    Pnode* next;
};

typedef Pnode* Polynomial;

int CreatePoly(Polynomial& P, int n)
{
    //输入n项的系数和指数,建立:表示多项式的有序链表:P
    P = new Pnode;
    P->next = NULL;
    for (int i = 1; i <= n; ++i)
    {
        static Pnode* s = new Pnode;
        cin >> s->coef >> s->expn;
        Pnode* pre = P;        //pre初值为头结点
        Pnode* q = P->next;        //q初始化指向首元结点
        while (s->expn > q->expn)
            //每比较完一项,指针pre和q就往后指一项(进行一次指向下一项操作)
        {
            pre = pre->next;
            q = q->next;
        }
        pre->next = s;
        s->next = q;
    }
    return true;
}

int main()
{

}

 OK, now the program is almost complete

But (however), we still have a few procedural details that we haven't considered here:

(1):

The judgment (whether) of the program loop is to execute the statement, which should be changed to:

        while (q &&s->expn > q->expn)

(2):

In addition, although here let pre point to the input node s, it doesn't matter

(Although pre->next is lost, we still know his next address q, it doesn't matter)

But in order to develop the habit of avoiding losing node information, we still let the new input node point to the back first:

project 3:

#include<iostream>
using namespace std;
#include<stdlib.h>//存放exit

#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE  -1
#define OVERFLOW   -2   

#define MAXlength 100  //初始大小为100,可按需修改

typedef int Status;         //函数调用状态

struct Pnode//node:结; 结点;
{
    float coef;//coefficient:系数
    int expn;//指数
    Pnode* next;
};

typedef Pnode* Polynomial;

int CreatePoly(Polynomial& P, int n)
{
    //输入n项的系数和指数,建立:表示多项式的有序链表:P
    P = new Pnode;
    P->next = NULL;
    for (int i = 1; i <= n; ++i)
    {
        static Pnode* s = new Pnode;
        cin >> s->coef >> s->expn;
        Pnode* pre = P;        //pre初值为头结点
        Pnode* q = P->next;        //q初始化指向首元结点
        while (q &&s->expn > q->expn)
            //每比较完一项,指针pre和q就往后指一项(进行一次指向下一项操作)
        {
            pre = pre->next;
            q = q->next;
        }
        s->next = q;
        pre->next = s;      
     //将输入项s插入到q和其前驱结点pre之间
    }
    return true;
}

int main()
{

}

Addition, subtraction, and multiplication of (polynomial) linear tables:


Subsequent second rounds make up,

Swallow dates wholeheartedly without understanding

Guess you like

Origin blog.csdn.net/Zz_zzzzzzz__/article/details/128741528