1808035990 发表于 2024-3-16 11:17:27

怎么修改

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

typedef struct list{
        struct list *prior;
        int data;
        int freq;
        struct list *next;
}List;

void CreatList(List *&L,int a[],int n)
{
        List *s,*r;
        int i;
        L=(List *)malloc(sizeof(List));
        L->prior=L->next=NULL;
        r=L;
        for(i=0;i<n;i++)
        {
                s=(List *)malloc(sizeof(List));
                s->data=a;
                s->freq=0;
                r->next=s;s->prior=r;
                r=s;
        }
        r->next=NULL;
}

void printf(List *&L)
{
        List *p=L->next;
        while(p!=NULL)
        {
                printf("%d ",p->data);
                p=p->next;
        }
        printf("\n");
        p=L->next;
        while(p!=NULL)
        {
                printf("%d ",p->freq);
                p=p->next;
        }
        printf("\n");
}

bool locatenode(List *&h,int x)
{
        List *t,*p;
        p=h;
        for(h=h->next;h;h=h->next)
        {
                if(h->data==x)
                break;
        }
        if(h==NULL)
                return(false);
        else
        {
                h->freq++;
                for(t=h->prior;t!=p&&t->freq<=h->freq;t=t->prior);
                h->prior->next=h->next;
                if(h->next!=NULL)
                {
                        h->next->prior=h->prior;
                }
                h->next=t->next;
                h->next->prior=h;
                t->next=h;
                h->prior=t;
                return(true);
        }
}

int main()
{
        int a={1,2,3,4,5};
        List *L;
        CreatList(L,a,5);
        printf(L);
        printf("\n");
        locatenode(L,4);
        printf(L);
}
怎么修改

FishC 发表于 2024-3-16 11:17:55

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

typedef struct list {
        struct list* prior;
        int data;
        int freq;
        struct list* next;
} List;

void CreatList(List*& L, int a[], int n) {
        List* s, * r;
        int i;
        L = (List*)malloc(sizeof(List));
        L->prior = L->next = NULL;
        r = L;
        for (i = 0; i < n; i++) {
                s = (List*)malloc(sizeof(List));
                s->data = a;
                s->freq = 0;
                r->next = s;
                s->prior = r;
                r = s;
        }
        r->next = NULL;
}

void print(List* L) {
        List* p = L->next;
        while (p != NULL) {
                printf("%d ", p->data);
                p = p->next;
        }
        p = L->next;
        printf("\n");
        while (p != NULL) {
                printf("%d ", p->freq);
                p = p->next;
        }
        printf("\n");
}

bool locatenode(List*& h, int x) {
        List* t, * p;
        p = h;
        for (h = h->next; h; h = h->next) {
                if (h->data == x)
                        break;
        }
        if (h == NULL)
                return (false);
        else {
                h->freq++;
                for (t = h->prior; t != p && t->freq < h->freq; t = t->prior);
                h->prior->next = h->next;
                if (h->next != NULL) {
                        h->next->prior = h->prior;
                }
                h->next = t->next;
                h->next->prior = h;
                t->next = h;
                h->prior = t;
                return (true);
        }
}

int main() {
        int a[] = { 1,2,3,4,5 };
        List* L;
        CreatList(L, a, 5);
        print(L);
        printf("\n");
        locatenode(L, 4);
        print(L);

        return 0;
}

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

人造人 发表于 2024-3-16 13:00:13

我想问一下,你这是C语言还是C++ ?

人造人 发表于 2024-3-16 13:35:13

按C语言给你改了

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

typedef struct list {
    struct list *prior, *next;
    int data, freq;
} List;

void CreatList(List **L, int a, int n) {
    struct list *h = NULL, **pn = L;
    for(size_t i = 0; i < n; ++i) {
      *pn = malloc(sizeof(**pn));
      (*pn)->prior = h;
      (*pn)->next = NULL;
      (*pn)->data = a;
      (*pn)->freq = 0;
      h = *pn;
      pn = &h->next;
    }
}

void print(const List *L) {
    for(const List *i = L; i; i = i->next) {
      printf("%d ", i->data);
    }
    puts("");
    for(const List *i = L; i; i = i->next) {
      printf("%d ", i->freq);
    }
    puts("");
}

bool locatenode(List *L, int x) {
    for(List *i = L; i; i = i->next) {
      if(i->data != x) continue;
      ++i->freq; return true;
    }
    return false;
    /*
    这是在做什么?
    for(t = h->prior; t != p && t->freq <= h->freq; t = t->prior)
    ;
    h->prior->next = h->next;
    if(h->next != NULL) {
    h->next->prior = h->prior;
    }
    h->next = t->next;
    h->next->prior = h;
    t->next = h;
    h->prior = t;
    */
}

// 有始有终,有借有还
// 好不好?
void free_list(List *L) {
    if(!L) return;
    free_list(L->next);
    free(L);
}

int main(void) {
    int a = {1, 2, 3, 4, 5};
    List *L;
    CreatList(&L, a, 5);
    print(L);
    printf("%d\n", locatenode(L, 4));
    print(L);
    free_list(L);
    return 0;
}

人造人 发表于 2024-3-16 14:19:32

再来个C++版本的
#include <iostream>

using std::cout, std::endl;
using std::ostream;

class list_t {
    struct node_t {
      node_t *prev, *next;
      int data, freq;
      node_t(node_t *prev = nullptr, node_t *next = nullptr, int data = 0, int freq = 0):
            prev(prev), next(next), data(data), freq(freq) {}
    };
    node_t *node = nullptr;
    static void free_list(node_t *node) {
      if(!node) return;
      free_list(node->next);
      delete node;
    }
    list_t(const list_t &list) = delete;
    list_t &operator=(const list_t &list) = delete;
public:
    list_t(const int data[], size_t size) {
      node_t *h = NULL, **pn = &this->node;
      for(size_t i = 0; i < size; ++i) {
            *pn = new node_t(h, nullptr, data, 0);
            h = *pn;
            pn = &h->next;
      }
    }
    ~list_t() {free_list(this->node);}
    bool locate_node(int x) {
      for(node_t *i = this->node; i; i = i->next) {
            if(i->data != x) continue;
            ++i->freq; return true;
      }
      return false;
    }
    friend ostream &operator<<(ostream &os, const list_t &list) {
      for(const node_t *i = list.node; i; i = i->next) {
            os << i->data << " ";
      }
      os << endl;
      for(const node_t *i = list.node; i; i = i->next) {
            os << i->freq << " ";
      }
      return os;
    }
};

int main(void) {
    int a = {1, 2, 3, 4, 5};
    list_t list(a, 5);
    cout << list << endl;
    cout << list.locate_node(4) << endl;
    cout << list << endl;
    return 0;
}
页: [1]
查看完整版本: 怎么修改