#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *create()
{
NODE *head, *end_node, *new_node;
head = malloc(sizeof(NODE));
end_node = head;
new_node = malloc(sizeof(NODE));
new_node->data = 10;
end_node->next = new_node;
end_node = new_node;
new_node = malloc(sizeof(NODE));
new_node->data = 20;
end_node->next = new_node;
end_node = new_node;
new_node = malloc(sizeof(NODE));
new_node->data = 30;
end_node->next = new_node;
end_node = new_node;
end_node->next = NULL;
return head;
}
void print_node(NODE *head)
{
NODE *current_node;
current_node = head->next;
if(current_node == NULL)
{
printf("null \n");
}else
{
while (current_node != NULL)
{
printf("%d \n", current_node->data);
current_node = current_node->next;
}
}
}
void delete_node(NODE *head, int t)
{
NODE *current_node, *front_node;
front_node = head;
current_node = head->next;
while (current_node != NULL)
{
if (current_node->data == t)
{
front_node->next = current_node->next;
free(current_node);
printf("Found!\n");
return;
}else
{
front_node = current_node;
current_node = current_node->next;
}
}
printf("Not found\n");
}
void insert_node(NODE *head, int in_data)
{
NODE *current_node, *front_node, *add_node;
add_node = malloc(sizeof(NODE));
add_node->data = in_data;
front_node = head;
current_node = head->next;
while (current_node != NULL)
{
if (current_node->data < in_data)
{
front_node = current_node;
current_node = current_node->next;
}else
{
break;
}
}
front_node->next = add_node;
add_node->next = current_node;
}
int main(void)
{
NODE *h;
h = create();
// printf("%p\t %d\n", h,h->data);
print_node(h);
delete_node(h, 10);
print_node(h);
insert_node(h, 10);
print_node(h);
return 0;
}
单向带头链表
最新推荐文章于 2024-08-10 18:31:58 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)