【问题标题】:Circular doubly linked list in C delete functionC删除函数中的循环双向链表
【发布时间】:2020-12-18 15:28:41
【问题描述】:

我有一个循环双向链表。

deletefront() 函数不起作用:输出错误。什么错误?

其他功能正常。但是在调用deletefront 函数后显示后我得到了错误的输出。应该删除的 100 值仍然出现。请改正。

我已经包含了 C 源代码:

// circular doubly linked list
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node *rlink;
    struct node *llink;
} node;

node *head = NULL;

node *getnode(int ele) {
    node *ptr;
    ptr = (node *)malloc(sizeof(node));
    if (ptr == NULL) {
        printf("memory not alloc");
        exit(0);
    }
    if (ptr != NULL) {
        ptr->data = ele;
        ptr->rlink = NULL;
        ptr->llink = NULL;
    }
    return ptr;
}

void insertfront(int ele) {
    node *newnode;
    newnode = getnode(ele);
    if (head == NULL) {
        head = newnode;
        head->rlink = head;
        head->llink = head;
    } else {
        head->llink = newnode;
        newnode->rlink = head;
        head = newnode;
    }
}

void insertend(int ele) {
    node *newnode;
    newnode = getnode(ele);
    if (head == NULL) {
        head = newnode;
        head->rlink = head;
        head->llink = head;
    } else {
        node *temp = head;
        do {
            temp = temp->rlink;
        } while (temp != head->llink);

        newnode->rlink = temp->rlink;
        temp->rlink = newnode;
        newnode->llink = temp;
    }
}

int lenlist() {
    node *temp;
    int count = 0;
    temp = head;
    do {
        temp = temp->rlink;
        count++;
    } while (temp != head);

    return count;
}

void insertatpos(int ele,int pos) {
    if (pos == 1) {
        insertfront(ele);
    } else
    if (pos == (lenlist() + 1)) {
        insertend(ele);
    } else
    if (pos > 1 && pos <= (lenlist() + 1)) {
        node *prev, *curr;
        node *newnode = getnode(ele);
        int count = 1;
        curr = head;//curr points to 1st node
        do {
            prev = curr;
            count++;
            curr = curr->rlink;
            if (count == pos) {
                prev->rlink = newnode;
                newnode->llink = prev;
                newnode->rlink = curr;
                curr->llink = newnode;
            }
        } while (curr != head);
    } else {
        printf("invalid position");
    }
}

void delfront() {
    if (head == NULL)
        printf("empty list");

    node *aux;
    node *lastnode, *secondnode;
    aux = head;
    lastnode = head->llink;
    secondnode = head->rlink;

    secondnode->llink = lastnode;
    lastnode->rlink = secondnode;

    free(aux);

    head = secondnode;
}

void display() {
    node *aux = head;
    do {
        printf("%d->", aux->data);
        aux = aux->rlink;
    } while (aux != head);
    printf("\n");
}

int main() {
    insertfront(100);
    insertend(20);
    printf("\n%d\n", lenlist());
    insertatpos(45, 2);
    display();
    delfront();
    display();
}

【问题讨论】:

  • 请以tour 开头并阅读How to Ask。关于您的代码,它甚至不能远程编译,请考虑提供minimal reproducible example。此外,您同时使用 C 和 C++ 标记了它,请参阅这两个标记的描述。
  • 使用调试器怎么样?
  • 当您的代码被编译时,它会生成一个包含 24 个错误的列表 (see it at onlinegdb here)。请更正。

标签: c list doubly-linked-list


【解决方案1】:

你的代码有很多错误。

// circular doubly linked list
#include <stdio.h>
#include <stdlib.h>
/*i changed the names of your pointers here*/
typedef struct node {
    int data;
    struct node *prev, *next;
} node;

/*
node *head = NULL;
This will be removed.
Avoid using globals as much as you can.
*/

/*
This function is unecessary.

node *createNode(int ele) {
    node *ptr;
    ptr = (node *)malloc(sizeof(node));
    if (ptr == NULL) {
        printf("memory not alloc");
        exit(0);
    }
    if (ptr != NULL) {
        ptr->data = ele;
        ptr->rlink = NULL;
        ptr->llink = NULL;
    }
    return ptr;
}
*/

char insertFront(node **head, int ele) {
    node *newNode=malloc(sizeof(node));

    If (newNode==NULL) return 0;

    newNode->data=ele;

    if (*head){
        newNode->next=*head;
        newNode->prev=(*head)->prev;
        (*head)->prev->next=newNode;
        (*head)->prev=newNode;
    } else {
        newNode->next=newNode;
        newNode->prev=newNode;
    }

    *head=newNode;

    return 1;
}

char insertEnd(node **head, int ele) {
    node *newNode=malloc(sizeof(node));

    If (newNode==NULL) return 0;

    newNode->data=ele;

    if (*head){
        newNode->next=*head;
        newNode->prev=(*head)->prev;
        (*head)->prev->next=newNode;
        (*head)->prev=newNode;
    } else {
        newNode->next=newNode;
        newNode->prev=newNode;
        *head=newNode;
    }

    return 1;
}

/*You could simple create a struct list that would have as members
the head of your list and its height to avoid calculating it each time
you want it but anyway. I will fix that.

int lenList(node *head) {
    if (*head==NULL) return 0;

    node *temp=head;
    int count = 0;

    do {
        temp = temp->next;
        count++;
    } while (temp != head);

    return count;
}
*/

char insertNatP(node **head, int ele, int pos) {
    If (pos<1 || pos>lenList(head)){
         printf("Invalid Position\n");
         return 0;
    }

    int i;

    for(i=0; i<pos-1; head=&head->next, i++);

    node *newNode=malloc(sizeof(node));

    If (newNode==NULL){
         printf("Memory could not be allocated\n");
         return 0;
    }

    newNode->data=ele;

    If (*head!=NULL){
        newNode->prev=(*head)->prev;
        (*head)->prev->next=newNode;
        (*head)->prev=newNode
        newNode->next=*head;
    } else {
        newNode->prev=newNode;
        newNode->next=newNode;
    }

    *head=newNode;

    return 1;
}

char delFront(node **head) {
    if (*head == NULL) return 0;

    node garbage=*head;
    *head=(*head)->next;

    if (*head==garbage) *head=NULL; else{
        (*head)->prev=garbage->prev;
        garbage->prev->next=*head;
    }

    free(garbage);

    return 1;
}

void printList(node *list) {
    if (list==NULL) return;

    node *sentinel=list->prev;

    while (list!=sentinel) {
        printf("%d->", list->data);
        list=list->next;
    } 
    printf("%d\n", list->data);
}

int main() {
    node *l1=NULL;

    insertFront(&l1, 100);
    insertEnd(&l1, 20);

    printf("\n%d\n", lenList(l1));
    insertNatP(&l1, 45, 2);
    printList(l1);

    delFront(&l1);
    printList(l1);
}

试试这个

【讨论】:

    【解决方案2】:

    问题不在于deletefront() 函数,而是您错过了更新insertfront()insertend() 函数中的一些链接。

    我更新了代码here 并添加了我进行更改的注释。尝试使用示例对其进行可视化。

    但是,我建议您使用调试器解决此类问题,或者通过示例测试用例检查代码。它将提高您的调试和编码技能!

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 2016-06-11
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 2014-03-04
      相关资源
      最近更新 更多