为什么在删除元素的时候,删除别的元素都没有问题,但删除0的时候就会出现这样的输出
#include <stdio.h>
#include <stdlib.h>
struct Data
{
int data;
};
struct Linked_Node
{
struct Data data;
struct Linked_Node* next;
};
struct Linked_Node* setNode(void);//创建链表
struct Linked_Node* createNode(int);//创建节点
void insertByHead(struct Linked_Node*,int);//插入节点(头插法)
void deleteNode(struct Linked_Node*,int);//删除节点(指定数据)
void printNode(struct Linked_Node*);//打印链表
struct Linked_Node* setNode(void)//创建链表
{
struct Linked_Node* head = (struct Linked_Node*)malloc(sizeof(struct Linked_Node*));
head->data.data = 0;
head->next = NULL;
return head;
}
struct Linked_Node* createNode(int data)//创建节点
{
struct Linked_Node* node = (struct Linked_Node*)malloc(sizeof(struct Linked_Node*));
node->data.data = data;
node->next = NULL;
return node;
}
void insertByHead(struct Linked_Node* head,int data)//插入节点(头插法)
{
struct Linked_Node* node = createNode(data);
node->next = head->next;
head->next = node;
return;
}
void deleteNode(struct Linked_Node* head,int data)//删除节点(指定数据)
{
struct Linked_Node* tempnode = head;
struct Linked_Node* tempnodeformer = tempnode;
if(head->next == NULL){printf("链表为空\n"); return;}
while(tempnode->data.data != data)
{
tempnodeformer = tempnode;
tempnode = tempnodeformer->next;
if(tempnode == NULL)
{
printf("链表无目标数据\n");
return;
}
}
tempnodeformer->next = tempnode->next;
free(tempnode);
return;
}
void printNode(struct Linked_Node* head)//打印链表
{
struct Linked_Node* tempnode = head->next;
if(head->next == NULL){printf("链表为空\n"); return;}
do
{
printf("%d\t",tempnode->data.data);
tempnode = tempnode->next;
}while(tempnode->next != NULL);
printf("%d\t",tempnode->data.data);
return;
}
int main(void)
{
struct Linked_Node* head = setNode();
insertByHead(head,2);
insertByHead(head,1);
insertByHead(head,0);
deleteNode(head,0);
printNode(head);
return 0;
}