zl程序教程

您现在的位置是:首页 >  后端

当前栏目

链表指针相关

链表 相关 指针
2023-09-14 09:11:42 时间

二叉树

typedef struct Node
{
    int data;
    struct Node *lchild, *rchild;
} Node, *Tree;
int f, a[10005];
Tree build(int cur)
{
    Tree T;
    if (f)
    {
        T = NULL;
        return T;
    }
    int p = a[cur];
    if (p == -1)
        f = 1, T = NULL;
    else if (p == 0)
        T = NULL;
    else
    {
        T = (Node *)malloc(sizeof(Node));
        T->data = p;
        T->lchild = build(cur * 2 + 1);
        T->rchild = build(cur * 2 + 2);
    }
    return T;
}
void destory(Tree t)
{
    Tree cur = t;
    if (cur != NULL)
    {
        destory(cur->lchild);
        destory(cur->rchild);
        free(cur);
        cur=NULL;
    }
}

链表

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *inset(struct node *head, int n)
{
    int i;
    struct node *p, *q, *tail;
    q = head;
    for (i = 0; i < n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node));
        scanf("%d", &p->data);
        p->next = q->next;
        q->next = p;
        q = p;
    }
    return (head);
}
int main()
{
    int i, j, n, m, a, b, c, k;
    struct node *head, *p, *q, *tail;
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        scanf("%d%d%d", &m, &a, &b);
        head = (struct node *)malloc(sizeof(struct node));
        head->next = NULL;
        head = inset(head, m);
        q = head;
        while (q->next != NULL)
        {
            k = 1;
            if ((q->next->data >= a) && (q->next->data <= b))
            {
                k = 0;
                p = q->next;
                q->next = p->next;
                free(p);
            }
            if (k != 0)
                q = q->next;
        }
        p = head;
        p = p->next;
        int f = 0;
        if (p == NULL)
        {
            free(head);
            printf("-1");
        }
        else
            while (p != NULL)
            {
                q = p;
                if (f)
                    printf(" ");
                printf("%d", p->data);
                f = 1;
                p = p->next;
                free(q);
            }
        printf("\n");
    }
    return 0;
}

链表逆序

void reverse_linklist(struct node*& head)
{
    node *p = head;
    if(p->next == NULL){
        return;
    }
    head = p->next;
    reverse_linklist(head);
    p->next->next = p;
    p->next = NULL;
}

TZOJ1216: 一元多项式求和 

#include <bits/stdc++.h>
using namespace std;

struct Node
{
    double coe;
    int exp;
    struct Node *next;
};

Node *add(Node *a, Node *b)
{
    Node *p = a;
    while (p->next)
    {
        if (p->next->exp > b->exp)
        {
            b->next = p->next;
            p->next = b;
            return a;
        }
        else if (p->next->exp == b->exp)
        {
            p->next->coe += b->coe;
            if (p->next->coe == 0)
            {
                Node *q = p->next;
                p->next = p->next->next;
                delete q;
                return a;
            }
            return a;
        }
        p = p->next;
    }
    b->next = p->next;
    p->next = b;
    return a;
}

void Print(Node *head)
{
    Node *t = head;
    head = head->next;
    delete t;
    while (head)
    {
        if (fabs(head->coe) >= 1e-6)
        {
            printf("%.2f %d\n",head->coe,head->exp);
        }
        Node *p = head;
        head = head->next;
        delete p;
    }
}

int main()
{
    int T;
    cin>>T;
    while (T--)
    {
        Node *head = new Node;
        head->next = NULL;
        int n;
        cin>>n;
        while (n--)
        {
            Node *t = new Node;
            cin>>t->coe>>t->exp;
            head = add(head, t);
        }
        cin>>n;
        while (n--)
        {
            Node *t = new Node;
            cin>>t->coe>>t->exp;
            head = add(head, t);
        }
        Print(head);
        if(T)cout<<"\n";
    }
    return 0;
}

 

CSS控制单词换行 word-break:break-all;

const double X::PI=acos(-1.0);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<X::PI<<endl;