简体   繁体   English

C删除函数中的循环双向链表

[英]Circular doubly linked list in C delete function

I have a circular doubly linked list.我有一个循环双向链表。

The deletefront() function is not working: the output is wrong. deletefront()函数不起作用:输出错误。 What is the mistake?什么是错误?

The other functions are working.其他功能正在运行。 But I get a wrong output after displaying after calling deletefront function.但是在调用deletefront函数后显示后我得到了错误的输出。 The 100 value which should be deleted is still appearing.应该删除的 100 值仍然出现。 Please correct it.请更正。

I have included the C source code:我已经包含了 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();
}

The problem is not in the deletefront() function, instead, you have missed updating a few links in the insertfront() and insertend() functions.问题不在于deletefront()函数,而是您错过了更新insertfront()insertend()函数中的一些链接。

I have updated the code here and also added the comment where I made the changes.我在这里更新了代码并在我进行更改的地方添加了注释。 Try to visualise it using an example.尝试使用示例对其进行可视化。

However, I suggest that you solve such issues using a debugger or go through the code with a sample test case.但是,我建议您使用调试器解决此类问题,或者使用示例测试用例查看代码。 It will improve you debugging as well as coding skills!它将提高您的调试和编码技能!

Your code have a lot of mistakes.你的代码有很多错误。

// 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);
}

Try this尝试这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM