我在双向链表中的交换代码有问题

如何解决我在双向链表中的交换代码有问题

我用 C 编写了交换代码 但我在学校编译器中没有得到完美的成绩。 我认为以下代码有问题。 我用结构体和指针制作了双向链表。 节点定义为:

struct node* newnode(char x)
{
    struct node* new = (struct node*)malloc(sizeof(struct node));
    new->next = NULL;
    new->prev = NULL;
    new->c = x;

    return new;
}

下面的代码是交换代码,我让每个函数因子表示“struct node*p”是从头到尾的完整列表,“int a and b”是列表的等级 “count1(p)”是计算完整列表中的节点并返回节点数(不包括头和尾)的函数。

void swap(struct node* p,int a,int b)
{
    int x,y; 
    struct node* x1,* x2,emp = { 0 };
    int i = 0;
    x1 = p;
    x2 = p;

    if (a >= b) {
        x = b;
        y = a;
    }
    else {
        x = a;
        y = b;
    }
if (b <= 0 || a <= 0 || a > N || a > count1(p) || b > N || b > count1(p)) {
        printf("invalid position\n");
        return;
    }while (i < x && x1 != tail) {
        x1 = x1->next;
        i++;
    }
    i = 0;
    while (i < y && p != tail) {
        x2 = x2->next;
        i++;
    }
    if (x1->next != x2)
    {
        x1->prev->next = x2;
        x1->next->prev = x2;
        x2->prev->next = x1;
        x2->next->prev = x1;
        emp.next = x1->next;
        emp.prev = x1->prev;
        x1->next = x2->next;
        x1->prev = x2->prev;
        x2->next = emp.next;
        x2->prev = emp.prev;
    }
    if (x1->next == x2)
    {
        emp.prev = x1->prev;
        emp.next = x1->next;
        x1->prev->next = x2;
        x2->next->prev = x1;
        x1->prev = x2;
        x1->next = x2->next;
        x2->prev = emp.prev;
        x2->next = x1;


    }

}

以下代码是我的完整代码:

#include<stdio.h>
#include<stdlib.h>
int N;
struct node {
    struct node* next;
    struct node* prev;
    char c;
};
struct node* head,* tail;

void reset() {

    head = (struct node*)malloc(sizeof(struct node));
    tail = (struct node*)malloc(sizeof(struct node));
    head->prev = NULL;
    head->next = tail;
    head->c = NULL;
    tail->prev = head;
    tail->next = NULL;
    tail->c = NULL;
}
struct node* newnode(char x)
{
    struct node* new = (struct node*)malloc(sizeof(struct node));
    new->next = NULL;
    new->prev = NULL;
    new->c = x;

    return new;
}
void prcount(struct node* p)
{

    printf("%d\n",count1(p));
}
int count1(struct node* p)
{
    int sum = 0;

    while (p != tail) {
        p = p->next;

        sum++;

    }
    return sum + -1;
}
void add1(struct node* p,int n,char x)
{
    struct node* new1 = newnode(x);
    int i = 1;
    if (n <= 0 || n > N || n > count1(p) + 1) {
        printf("invalid position\n");
        return;
    }
    while (i < n && p != tail) {
        p = p->next;

        i++;

    }
    p->next->prev = new1;
    new1->prev = p;
    new1->next = p->next;
    p->next = new1;

}
void delete1(struct node* p,int n)
{
    int i = 0;
    if (n <= 0 || n > N || n > count1(p)) {
        printf("invalid position\n");
        return;
    }
    while (i < n && p != tail)
    {
        p = p->next;
        i++;
    }
    p->prev->next = p->next;
    p->next->prev = p->prev;
    free(p);


}
void get1(struct node* p,int n) {
    int i = 0;
    if (n <= 0 || n > N || n > count1(p)) {
        printf("invalid position\n");
        return;
    }while (i < n && p != tail) {
        p = p->next;
        i++;
    }
    printf("%c\n",p->c);
}
void print1(struct node* p)
{
    int i = 0;
    if (count1(p) == 0) {
        printf("invalid position\n");
        return;
    }
    while (i < N && p->next != tail)
    {
        p = p->next;
        printf("%c",p->c);
        i++;
    }
    printf("\n");

}void removeall(struct node* p)
{

    struct node* emp;
    p = p->next;


    while (p != tail)

    {
        emp = p->next;
        free(p);
        p = emp;
    }
    head->prev = NULL;
    head->next = tail;
    head->c = NULL;
    tail->prev = head;
    tail->next = NULL;
    tail->c = NULL;
}
void swap(struct node* p,y;
    struct node* x1,emp = { 0 };
    int i = 0;
    x1 = p;
    x2 = p;

    if (a >= b) {
        x = b;
        y = a;
    }
    else {
        x = a;
        y = b;
    }

    if (b <= 0 || a <= 0 || a > N || a > count1(p) || b > N || b > count1(p)) {
        printf("invalid position\n");
        return;
    }while (i < x && x1 != tail) {
        x1 = x1->next;
        i++;
    }
    i = 0;
    while (i < y && x2 != tail) {
        x2 = x2->next;
        i++;
    }
    if (x1->next != x2)
    {
        x1->prev->next = x2;
        x1->next->prev = x2;
        x2->prev->next = x1;
        x2->next->prev = x1;
        emp.next = x1->next;
        emp.prev = x1->prev;
        x1->next = x2->next;
        x1->prev = x2->prev;
        x2->next = emp.next;
        x2->prev = emp.prev;
    }
    if (x1->next == x2)
    {
        emp.prev = x1->prev;
        emp.next = x1->next;
        x1->prev->next = x2;
        x2->next->prev = x1;
        x1->prev = x2;
        x1->next = x2->next;
        x2->prev = emp.prev;
        x2->next = x1;


    }

}
int main()
{
    int i;
    int r,n;
    char x,y;

    reset();

    scanf("%d",&N);
    for (i = 0; i < N; i++)
    {
        scanf(" %c",&x);
        if (x == 'A') {//A mean add(head,char x) -> add 'x' on a rank in list
            scanf("%d",&r);

            scanf(" %c",&y);

            add1(head,r,y);
        }
        else if (x == 'D') {// D mean delete(struct node *head,int a) -> delete element on int a rank
            scanf("%d",&r);

            delete1(head,r);
        }
        else if (x == 'G') {//G mean Get(head,int a)-> print element on int a rank
            scanf("%d",&r);
            get1(head,r);

        }
        else if (x == 'P') {//P mean print(head) -> print all element in list
            print1(head);
        }
        else if (x == 'R') {//R mean Removeall(head) -> remove all node (exclude head and tail0
            removeall(head);
        }
        else if (x == 'C')//C Mean prcount(head) -> count and print number of node in list (exclude head and tail)
        {
            prcount(head);
        }
        else if (x == 'S') {//S mean Swap(head,int b) -> swap element of int a rank with int b rank 
            scanf("%d",&r);
            scanf("%d",&n);
            swap(head,n);
        }
    }
    removeall(head);
}

这是我的完整代码。 我想知道有什么问题或者更有效的方法来制作交换码?

解决方法

#include<stdio.h>
#include<stdlib.h>
int N;
struct node {
    struct node* next;
    struct node* prev;
    char c;
};
struct node* head,* tail;

void reset() {

    head = (struct node*)malloc(sizeof(struct node));
    tail = (struct node*)malloc(sizeof(struct node));
    head->prev = NULL;
    head->next = tail;
    head->c = NULL;
    tail->prev = head;
    tail->next = NULL;
    tail->c = NULL;
}
struct node* newnode(char x)
{
    struct node* new = (struct node*)malloc(sizeof(struct node));
    new->next = NULL;
    new->prev = NULL;
    new->c = x;

    return new;
}
void prcount(struct node* p)
{
    
    printf("%d\n",count1(p));
}
int count1(struct node* p)
{
    int sum = 0;

    while (p != tail) {
        p = p->next;

        sum++;

    }
    return sum + -1;
}
void add1(struct node* p,int n,char x)
{
    struct node * new1 = newnode(x);
    int i = 1;
    if (n<=0||n > N || n > count1(p) + 1) {
        printf("invalid position\n");
        return;
    }
    while (i < n && p != tail) {
        p = p->next;

        i++;

    }
    p->next->prev = new1;
    new1->prev = p;
    new1->next = p->next;
    p->next = new1;

}
void delete1(struct node* p,int n)
{
    int i = 0;
    if (n <= 0 || n > N || n > count1(p)) {
        printf("invalid position\n");
        return;
    }
    while (i < n && p != tail)
    {
        p = p->next;
        i++;
    }
    p->prev->next = p->next;
    p->next->prev = p->prev;
    free(p);


}
void get1(struct node* p,int n) {
    int i = 0;
    if (n <= 0 || n > N || n > count1(p)) {
        printf("invalid position\n");
        return;
    }while (i < n && p != tail) {
        p = p->next;
        i++;
    }
    printf("%c\n",p->c);
}
void print1(struct node* p)
{
    int i = 0;
    if (count1(p) == 0) {
        printf("invalid position\n");
        return;
    }
    while (i < N && p->next != tail)
    {
        p = p->next;
        printf("%c",p->c);
        i++;
    }
    printf("\n");

}void removeall(struct node* p)
{

    struct node* emp;
    p = p->next;

    
    while (p != tail)

    {
        emp = p->next;
        free(p);
        p = emp;
    }
    head->prev = NULL;
    head->next = tail;
    head->c = NULL;
    tail->prev = head;
    tail->next = NULL;
    tail->c = NULL;
}
void swap(struct node* p,int a,int b)
{
    int x,y; 
    struct node* x1,* x2,emp = { 0 };
    int i = 0;
    x1 = p;
    x2 = p;

    if (a >= b) {
        x = b;
        y = a;
    }
    else {
        x = a;
        y = b;
    }
    
    if (b <= 0 || a <= 0 || a > N || a > count1(p) || b > N || b > count1(p)) {
        printf("invalid position\n");
        return;
    }while (i < x && x1 != tail) {
        x1 = x1->next;
        i++;
    }
    i = 0;
    while (i < y && p != tail) {
        x2 = x2->next;
        i++;
    }
    if (x1->next != x2)
    {
        x1->prev->next = x2;
        x1->next->prev = x2;
        x2->prev->next = x1;
        x2->next->prev = x1;
        emp.next = x1->next;
        emp.prev = x1->prev;
        x1->next = x2->next;
        x1->prev = x2->prev;
        x2->next = emp.next;
        x2->prev = emp.prev;
    }
    if (x1->next == x2)
    {
        emp.prev = x1->prev;
        emp.next = x1->next;
        x1->prev->next = x2;
        x2->next->prev = x1;
        x1->prev = x2;
        x1->next = x2->next;
        x2->prev = emp.prev;
        x2->next = x1;


    }

}
int main()
{
    int i;
    int r,n;
    char x,y;

    reset();

    scanf("%d",&N);
    for (i = 0; i < N; i++)
    {
        scanf(" %c",&x);
        if (x == 'A') {
            scanf("%d",&r);

            scanf(" %c",&y);

            add1(head,r,y);
        }
        else if (x == 'D') {
            scanf("%d",&r);

            delete1(head,r);
        }
        else if (x == 'G') {
            scanf("%d",&r);
            get1(head,r);

        }
        else if (x == 'P') {
            print1(head);
        }
        else if (x == 'R') {
            removeall(head);
        }
        else if (x == 'C')
        {
            prcount(head);
        }
        else if (x == 'S') {
            scanf("%d",&r);
            scanf("%d",&n);
            swap(head,n);
        }
    }
    removeall(head);
}

这是我的完整代码

,

int N 用作 main 中输入命令的数量。在您的列表函数中与 N 进行比较(好像 N 表示节点数)没有意义 - 您可以删除它们。

Besides that,there are some places where a compiler with appropriate options would issue warnings - perhaps that's why you didnt get perfect grade in school compiler.

     head->c = NULL;

由于 cchar 类型并且 NULL 是一个 空指针常量,所以这不合适;改为

     head->c = '\0';    // or head->c = 0;

     printf("%d\n",count1(p));

在声明之前调用 count1。使用前声明它

int count1(struct node* p);

或将函数定义移动到第一个使用它的函数之前。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-