簡體   English   中英

C刪除函數中的循環雙向鏈表

[英]Circular doubly linked list in C delete function

我有一個循環雙向鏈表。

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

問題不在於deletefront()函數,而是您錯過了更新insertfront()insertend()函數中的一些鏈接。

我在這里更新了代碼並在我進行更改的地方添加了注釋。 嘗試使用示例對其進行可視化。

但是,我建議您使用調試器解決此類問題,或者使用示例測試用例查看代碼。 它將提高您的調試和編碼技能!

你的代碼有很多錯誤。

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

嘗試這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM