鱼C论坛

 找回密码
 立即注册
查看: 848|回复: 4

[已解决]怎么修改

[复制链接]
发表于 2024-3-16 11:17:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. typedef struct list{
  4.         struct list *prior;
  5.         int data;
  6.         int freq;
  7.         struct list *next;
  8. }List;

  9. void CreatList(List *&L,int a[],int n)
  10. {
  11.         List *s,*r;
  12.         int i;
  13.         L=(List *)malloc(sizeof(List));
  14.         L->prior=L->next=NULL;
  15.         r=L;
  16.         for(i=0;i<n;i++)
  17.         {
  18.                 s=(List *)malloc(sizeof(List));
  19.                 s->data=a[i];
  20.                 s->freq=0;
  21.                 r->next=s;s->prior=r;
  22.                 r=s;
  23.         }
  24.         r->next=NULL;
  25. }

  26. void printf(List *&L)
  27. {
  28.         List *p=L->next;
  29.         while(p!=NULL)
  30.         {
  31.                 printf("%d ",p->data);
  32.                 p=p->next;
  33.         }
  34.         printf("\n");
  35.         p=L->next;
  36.         while(p!=NULL)
  37.         {
  38.                 printf("%d ",p->freq);
  39.                 p=p->next;
  40.         }
  41.         printf("\n");
  42. }

  43. bool locatenode(List *&h,int x)
  44. {
  45.         List *t,*p;
  46.         p=h;
  47.         for(h=h->next;h;h=h->next)
  48.         {
  49.                 if(h->data==x)
  50.                 break;
  51.         }
  52.         if(h==NULL)
  53.                 return(false);
  54.         else
  55.         {
  56.                 h->freq++;
  57.                 for(t=h->prior;t!=p&&t->freq<=h->freq;t=t->prior);
  58.                 h->prior->next=h->next;
  59.                 if(h->next!=NULL)
  60.                 {
  61.                         h->next->prior=h->prior;
  62.                 }
  63.                 h->next=t->next;
  64.                 h->next->prior=h;
  65.                 t->next=h;
  66.                 h->prior=t;
  67.                 return(true);
  68.         }
  69. }

  70. int main()
  71. {
  72.         int a[5]={1,2,3,4,5};
  73.         List *L;
  74.         CreatList(L,a,5);
  75.         printf(L);
  76.         printf("\n");
  77.         locatenode(L,4);
  78.         printf(L);
  79. }
复制代码

怎么修改
最佳答案
2024-3-16 13:35:13
按C语言给你改了

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>

  4. typedef struct list {
  5.     struct list *prior, *next;
  6.     int data, freq;
  7. } List;

  8. void CreatList(List **L, int a[const], int n) {
  9.     struct list *h = NULL, **pn = L;
  10.     for(size_t i = 0; i < n; ++i) {
  11.         *pn = malloc(sizeof(**pn));
  12.         (*pn)->prior = h;
  13.         (*pn)->next = NULL;
  14.         (*pn)->data = a[i];
  15.         (*pn)->freq = 0;
  16.         h = *pn;
  17.         pn = &h->next;
  18.     }
  19. }

  20. void print(const List *L) {
  21.     for(const List *i = L; i; i = i->next) {
  22.         printf("%d ", i->data);
  23.     }
  24.     puts("");
  25.     for(const List *i = L; i; i = i->next) {
  26.         printf("%d ", i->freq);
  27.     }
  28.     puts("");
  29. }

  30. bool locatenode(List *L, int x) {
  31.     for(List *i = L; i; i = i->next) {
  32.         if(i->data != x) continue;
  33.         ++i->freq; return true;
  34.     }
  35.     return false;
  36.     /*
  37.     这是在做什么?
  38.     for(t = h->prior; t != p && t->freq <= h->freq; t = t->prior)
  39.     ;
  40.     h->prior->next = h->next;
  41.     if(h->next != NULL) {
  42.     h->next->prior = h->prior;
  43.     }
  44.     h->next = t->next;
  45.     h->next->prior = h;
  46.     t->next = h;
  47.     h->prior = t;
  48.     */
  49. }

  50. // 有始有终,有借有还
  51. // 好不好?
  52. void free_list(List *L) {
  53.     if(!L) return;
  54.     free_list(L->next);
  55.     free(L);
  56. }

  57. int main(void) {
  58.     int a[5] = {1, 2, 3, 4, 5};
  59.     List *L;
  60.     CreatList(&L, a, 5);
  61.     print(L);
  62.     printf("%d\n", locatenode(L, 4));
  63.     print(L);
  64.     free_list(L);
  65.     return 0;
  66. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 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[i];
                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 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2024-3-16 13:00:13 | 显示全部楼层
我想问一下,你这是C语言还是C++ ?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-16 13:35:13 | 显示全部楼层    本楼为最佳答案   
按C语言给你改了

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>

  4. typedef struct list {
  5.     struct list *prior, *next;
  6.     int data, freq;
  7. } List;

  8. void CreatList(List **L, int a[const], int n) {
  9.     struct list *h = NULL, **pn = L;
  10.     for(size_t i = 0; i < n; ++i) {
  11.         *pn = malloc(sizeof(**pn));
  12.         (*pn)->prior = h;
  13.         (*pn)->next = NULL;
  14.         (*pn)->data = a[i];
  15.         (*pn)->freq = 0;
  16.         h = *pn;
  17.         pn = &h->next;
  18.     }
  19. }

  20. void print(const List *L) {
  21.     for(const List *i = L; i; i = i->next) {
  22.         printf("%d ", i->data);
  23.     }
  24.     puts("");
  25.     for(const List *i = L; i; i = i->next) {
  26.         printf("%d ", i->freq);
  27.     }
  28.     puts("");
  29. }

  30. bool locatenode(List *L, int x) {
  31.     for(List *i = L; i; i = i->next) {
  32.         if(i->data != x) continue;
  33.         ++i->freq; return true;
  34.     }
  35.     return false;
  36.     /*
  37.     这是在做什么?
  38.     for(t = h->prior; t != p && t->freq <= h->freq; t = t->prior)
  39.     ;
  40.     h->prior->next = h->next;
  41.     if(h->next != NULL) {
  42.     h->next->prior = h->prior;
  43.     }
  44.     h->next = t->next;
  45.     h->next->prior = h;
  46.     t->next = h;
  47.     h->prior = t;
  48.     */
  49. }

  50. // 有始有终,有借有还
  51. // 好不好?
  52. void free_list(List *L) {
  53.     if(!L) return;
  54.     free_list(L->next);
  55.     free(L);
  56. }

  57. int main(void) {
  58.     int a[5] = {1, 2, 3, 4, 5};
  59.     List *L;
  60.     CreatList(&L, a, 5);
  61.     print(L);
  62.     printf("%d\n", locatenode(L, 4));
  63.     print(L);
  64.     free_list(L);
  65.     return 0;
  66. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-3-16 14:19:32 | 显示全部楼层
再来个C++版本的
  1. #include <iostream>

  2. using std::cout, std::endl;
  3. using std::ostream;

  4. class list_t {
  5.     struct node_t {
  6.         node_t *prev, *next;
  7.         int data, freq;
  8.         node_t(node_t *prev = nullptr, node_t *next = nullptr, int data = 0, int freq = 0):
  9.             prev(prev), next(next), data(data), freq(freq) {}
  10.     };
  11.     node_t *node = nullptr;
  12.     static void free_list(node_t *node) {
  13.         if(!node) return;
  14.         free_list(node->next);
  15.         delete node;
  16.     }
  17.     list_t(const list_t &list) = delete;
  18.     list_t &operator=(const list_t &list) = delete;
  19. public:
  20.     list_t(const int data[], size_t size) {
  21.         node_t *h = NULL, **pn = &this->node;
  22.         for(size_t i = 0; i < size; ++i) {
  23.             *pn = new node_t(h, nullptr, data[i], 0);
  24.             h = *pn;
  25.             pn = &h->next;
  26.         }
  27.     }
  28.     ~list_t() {free_list(this->node);}
  29.     bool locate_node(int x) {
  30.         for(node_t *i = this->node; i; i = i->next) {
  31.             if(i->data != x) continue;
  32.             ++i->freq; return true;
  33.         }
  34.         return false;
  35.     }
  36.     friend ostream &operator<<(ostream &os, const list_t &list) {
  37.         for(const node_t *i = list.node; i; i = i->next) {
  38.             os << i->data << " ";
  39.         }
  40.         os << endl;
  41.         for(const node_t *i = list.node; i; i = i->next) {
  42.             os << i->freq << " ";
  43.         }
  44.         return os;
  45.     }
  46. };

  47. int main(void) {
  48.     int a[5] = {1, 2, 3, 4, 5};
  49.     list_t list(a, 5);
  50.     cout << list << endl;
  51.     cout << list.locate_node(4) << endl;
  52.     cout << list << endl;
  53.     return 0;
  54. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 14:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表