c - 如何在C中删除双向链表中的替代节点?

标签 c doubly-linked-list

我编写了以下代码,但在执行 create() 函数后它停止工作。我想删除从头节点开始的替代元素。我的delete_Alt()函数正确吗?请告诉我哪里错了。

#include <stdio.h>
#include <stdlib.h>

// using a structure
typedef struct mynode {
    int data;
    struct mynode *prev;    // to point to previous node
    struct mynode *link;    // to point to next node
} node;
node *head = NULL;

// creating the list
void create() {
    node *p, *q;
    int ch;
    do {
        p = (node *)malloc(sizeof(node));
        printf("enter data\n"); 
        scanf("%d", &p->data);
        if (head == NULL) 
        {
            p->prev = head;
            q = p;
        }
        else
        {
            p->prev = q;
            p->link = NULL;
            q->link = p;
            q = p;
        }
        printf("create another node?, press 1   ");
        scanf ("%d",&ch);
    } while(ch==1);
}

//to delete alternate elements
void delete_Alt() {
    if (head == NULL)
        printf("Empty list...ERROR");

    node *previous, *current, *next;    
    previous = head;
    current = head->link;
    while (previous !=NULL && current != NULL) {
        previous->prev = current->prev;
        previous->link = current->link; 

        next = current->link;
        previous->link = next;
        next->prev = previous;

        free(current);
    }
}

// print the list
void display() {
    node *temp;
    temp = head;
    while (temp != NULL) {
        printf("%d  ",temp->data);
        temp = temp->link;
    }
    printf("\n");
}

int main() {
    node *head = NULL;
    create();
    printf("List before deleting is:    ");
    display();
    delete_Alt();
    printf("List after deleting is:     ");
    display();
return 0;
}

最佳答案

您在创建和删除功能中犯了一些小错误...

这是更新后的代码,尝试一下...

#include <stdio.h>
#include <stdlib.h>

// using a structure
typedef struct mynode {
    int data;
    struct mynode *prev;    // to point to previous node
    struct mynode *link;    // to point to next node
} node;
node *head = NULL;

// creating the list
void create() {
    node *p, *q;
    int ch;
    do {
        p = (node *)malloc(sizeof(node));
        printf("enter data\n"); 
        scanf("%d", &p->data);
        p->link = NULL;
        if (head == NULL) 
        {
            p->prev = NULL;
            head = p;
        }
        else
        {
            q = head;
            while (q->link != NULL)
            q = q->link;
            p->prev = q;
            q->link = p;
        }
        printf("create another node?, press 1   ");
        scanf ("%d",&ch);
    } while(ch==1);
}

//to delete alternate elements
void delete_Alt() {
    if (head == NULL)
        printf("Empty list...ERROR");

    node *previous, *current, *next;    
    previous = head;
    current = head->link;
    while (previous !=NULL && current != NULL) 
    {
        previous->link = current->link; 
        next = current->link;
        free(current);
        if(next) 
        {
            next->prev = previous;
            current = next->link;
        }
        else 
        current = NULL;
        previous = next;
    }
}

// print the list
void display() {
    node *temp;
    temp = head;
    while (temp != NULL) {
        printf("%d  ",temp->data);
        temp = temp->link;
    }
    printf("\n");
}

int main() {
    node *head = NULL;
    create();
    printf("List before deleting is:    ");
    display();
    delete_Alt();
    printf("List after deleting is:     ");
    display();
return 0;
}

关于c - 如何在C中删除双向链表中的替代节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43425462/

相关文章:

java - 高效获取 LinkedHashSet 中最近插入的元素

java - Eclipse 中 C 代码的语法树

c - 什么是 'thunk' ? (在可重入排序函数的上下文中,例如 : qsort_r)

c - 如何编写信号处理程序来捕获 SIGSEGV?

特定 ISR 后上下文切换到特定进程

c - 如何在C中使用带头文件的链表?

c++ - 以升序插入双向链表的问题

c - C 中的二进制加法,来自数组

java - 我收到两个 NullPointerExceptions,我不确定如何解决

java - 在给定节点引用的情况下,删除 Java 中 LinkedList 中的节点