yancaidong 发表于 2017-6-16 21:03:39

二级指针删除最后一个结点出错,平台VS2017

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

struct test
{
        char book;
        char title;
        struct test *next;
};
void initialize(struct test *node);
struct test *list_node(struct test *head);
void addbook(struct test **head);
void release(struct test **head);
void printdata(struct test *head);
void getInput(struct test *node);
void node_delete(struct test **node);
void insert(struct test **head, struct test *node);

int main()
{
        struct test *head = NULL;
        struct test *pnode = NULL;
        head = (struct test *)malloc(sizeof(struct test));
        head->next = NULL;
        pnode = head;
        char ch;
        do
        {
                addbook(&pnode);
                getchar();
                printf("是否继续:");
                ch=getchar();
                getchar();
        }while (ch == 'Y' || ch == 'y');
        printdata(head);
        printf("=-----------------------------\n");
        node_delete(&pnode);//pnode指向尾节点,删除尾结点出错//
        printdata(head);
        printf("=-----------------------------\n");
        release(&head);
        printdata(head);
        printf("=-----------------------------\n");
}

void getInput(struct test *node)
{
        if (node == NULL)
        {
                printf("NULL");
                exit(-1);
        }
        else
        {
                printf("请输入书名:");
                scanf("%s", node->book);
                printf("请输入作者:");
                scanf("%s", node->title);
        }
}

void printdata(struct test *head)
{
        struct test *node = NULL;
        if (head == NULL)
        {
                printf("链表为空");
                exit(-1);
        }
        else
        {
                node = head->next;
                while (node != NULL)
                {
                        printf("书名:");
                        printf("%s\n", node->book);
                        printf("作者:");
                        printf("%s\n", node->title);
            node = node->next;
                }
        }
}

void release(struct test **head)
{
        struct test *temp = NULL;
        while (*head!=NULL)
        {
                temp = *head;
                *head = temp->next;
                free(temp);
                temp = NULL;
        }
}

void node_delete(struct test **node)
{
        struct test *temp = NULL;
       
        if (&node == NULL)
        {
                printf("结点为空");
                exit(-1);
        }
        else
        {
                temp = *node;
                *node = temp->next;
                free(temp);
                temp = NULL;
        }
}

void addbook(struct test **pnode)
{
        struct test *node;
        node = (struct test *)malloc(sizeof(struct test));
        if (node == NULL)
        {
                printf("failed");
                exit(-1);
        }
        else
        {
                initialize(node);
                getInput(node);
                insert(pnode, node);
        }
}

void insert(struct test **pnode,struct test *node)
{
        struct test *temp = NULL;
        if (*pnode != NULL)
        {
                temp = *pnode;
                while (temp->next != NULL)
                {
                        temp = temp->next;
                }
                temp->next = node;
                node->next = NULL;
                *pnode = node;
        }
        else
        {
                *pnode= node;
                node->next = NULL;
        }
}

void initialize(struct test *node)
{
        int i = 0;
        for (i; i < 128; i++)
        {
                node->book = NULL;
                node->title = NULL;
        }
}

struct test *list_node(struct test *head)
{
        struct test *temp = NULL;
        temp = head;
        while (temp->next != NULL)
        {
                temp = temp->next;
        }
        return temp;
}

yancaidong 发表于 2017-6-16 21:09:37

错误演示
页: [1]
查看完整版本: 二级指针删除最后一个结点出错,平台VS2017