当前位置: 首页 > 知识库问答 >
问题:

我对双重链表中的交换代码有问题

贲绪
2023-03-14

我用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和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, 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 && 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,int a, 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 a, int b) -> swap element of int a rank with int b rank 
            scanf("%d", &r);
            scanf("%d", &n);
            swap(head, r, n);
        }
    }
    removeall(head);
}

这是我的全部密码。我想知道是否有任何问题或更有效的方法来制作交换代码?

共有2个答案

上官斌
2023-03-14

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

除此之外,有一些地方带有适当选项的编译器会发出警告——也许这就是你在学校编译器中没有获得完美成绩的原因。

     head->c = NULL;

由于c的类型为char,并且NULL空指针常量,因此不适合;更改为

     head->c = '\0';    // or head->c = 0;
     printf("%d\n", count1(p));

在声明之前,您将调用Count1。使用前声明

int count1(struct node* p);

或者在使用第一个函数之前移动函数定义。

上官鸿祯
2023-03-14
#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, r, n);
        }
    }
    removeall(head);
}

这是我的全部代码

 类似资料:
  • 我有一个用于交换2个节点的函数: 然而,当与我的递归选择排序一起使用时: 在排序进行到一半时,它会将我节点的下一个指针设置为NULL,然后当我打印列表时,它会正确地排序到该值,但由于指针被错误地设置为NULL,其余的指针将丢失。 我查了一下然后: 我的初始列表是正确的,没有错误连接的节点。 我的swap函数只在非null有效节点上调用。 所以我责怪交换功能。它有什么问题吗?

  • 我正在尝试交换双链接列表中的两个节点。下面是具有交换功能的程序部分。 当我运行这个程序时,在第一个和第二个节点的情况下,它崩溃了。而在任何其他节点的情况下,它提供无限循环输出。(例如:-2- 我知道还有一些关于节点交换的问题,但是我没有找到任何与我的问题相似的问题。请帮帮我...!! 提前谢谢。

  • 我试图交换链表中节点的位置,然后使用排序函数进行排序。这两个函数中的任何一个都有逻辑错误。当我运行这个程序时,它会无限循环。 更新代码

  • 我在Java中实现一个双链接列表时遇到了一个问题。特别是要交换2个以下节点(在我的例子中,一个节点包含一个政治候选人)。 假设下面的DLL: head- 作为输出,我没有从头到尾的正确DLL,但从头到尾都很好: 我已经写了几个版本的这个方法reverseTwoNode。我甚至尝试在节点内部交换数据,而不是交换节点,我也有同样的问题。你能帮我真是太好了,我花了这么多时间在这个简单的功能上,我看不出有

  • 我遇到了一个问题,在这个问题中,您应该交换双链接列表中的一组节点。例如:对于列表

  • 我在实现哈夫曼算法,为此我使用了一个双链表。实现需要对列表进行排序,但仅仅交换数据是不够的——我需要交换整个节点。然而,这比我预想的要复杂一些。 我使用了这个选择排序的变体,但它会导致访问冲突错误。我假设这是因为我试图访问某个空指针,这两个条件本应阻止它。 任何帮助或建议都将不胜感激。