这个主要是为大学生同胞自己抄作业用的,哈哈哈哈。感觉着玩意写下来对变量,指针,结构体指针,传参的领悟会更加的明了。同学们可以自己复制粘贴自己调试看看,整个经过。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
} node;
int findelnm(node head_node, int data);
node *add_head_node(node *head_node, int data);
node *abb_tail_node(node *head_node, int data);
int print_node(node head_node);
node *inint_head_node();
void link_list();
int rmnode(node *head_node, int data);
int add_locat_node(node *head_node, int locat, int data);
// 头插法
node *add_head_node(node *head_node, int data)
{
node *node_new = malloc(sizeof(node));
memset(node_new, 0, sizeof(node));
node_new->next = head_node->next;
head_node->next = node_new;
node_new->data = data;
return node_new;
}
// 尾插法
node *abb_tail_node(node *head_node, int data)
{
node *tail_node = head_node;
node *new_node = malloc(sizeof(node));
while (tail_node->next != NULL)
{
tail_node = tail_node->next;
}
new_node->next = tail_node->next;
tail_node->next = new_node;
new_node->data = data;
return new_node;
}
// 从头部开始遍历
int print_node(node head_node)
{
if (head_node.next == NULL)
{
printf("遍历完成\n");
return 0;
}
printf("%d\n", head_node.next->data);
head_node = *head_node.next;
print_node(head_node);
return 0;
}
// 检索
int findelnm(node head_node, int data)
{
if (head_node.next == NULL)
{
printf("此为空链表\n");
return -1;
}
int i = 1;
while (head_node.next != NULL && head_node.next->data != data)
{
i++;
head_node = *head_node.next;
}
if (head_node.next == NULL)
{
printf("链表中没有这个元素\n");
return -1;
}
else
{
printf("%d在链表中第%d个节点\n", data, i);
}
return i;
}
// 删除
int rmnode(node *head_node, int data)
{
if (head_node->next == NULL)
{
printf("此链表为空\n");
return 0;
}
node *tepm = head_node;
node *del_node;
while (tepm->next != NULL && tepm->next->data != data)
{
tepm = tepm->next;
}
if (tepm->next == NULL)
{
printf("链表中没有要删除的节点\n");
return -1;
}
else
{
del_node = tepm->next;
tepm->next = del_node->next;
free(del_node);
printf("删除%c成功\n", data);
}
return 0;
}
// 指定位添加
int add_locat_node(node *head_node, int locat, int data)
{
if (head_node->next == NULL)
{
printf("此链表为空\n");
return 0;
}
node *temp;
node *new_node = malloc(sizeof(node));
temp = head_node->next;
int i = 1;
while (temp->next != NULL && i != locat)
{
i++;
temp = temp->next;
}
if (temp->next == NULL)
{
printf("插入位置超出链表的长度\n");
return -1;
}
else
{
new_node->next = temp->next;
temp->next = new_node;
new_node->data = data;
printf("在第%d个位置,即%d后插入数据%d成功\n", locat, temp->data, data);
return 0;
}
return 0;
}
// 初始化化头结点
node *inint_head_node()
{
node *head_node = malloc(sizeof(node));
head_node->data = -1;
head_node->next = NULL;
return head_node;
}
// 移动指定节点
// 摧毁链表
int destroy_list(node *head_node)
{
if (head_node->next == NULL)
{
free(head_node);
return 0;
}
node *node_destroy = head_node;
node *fin_node = head_node;
while (fin_node->next != NULL)
{
node_destroy = fin_node;
fin_node = fin_node->next;
free(node_destroy);
}
free(fin_node);
return 0;
}
void link_list()
{
node *haed_node = inint_head_node();
if (haed_node->data != -1)
{
printf("头结点初始化出错了\n");
return;
}
node *new_node;
for (int i = 0; i < 10; i++)
{
// new_node = add_head_node(&haed_node, i + 1);
new_node = abb_tail_node(haed_node, i);
if (new_node->data != i)
{
printf("出错了\n");
break;
return;
}
}
// printf("%d\n", haed_node.next->next->next->next->data);
print_node(*haed_node);
printf("---------------------\n");
// findelnm(*haed_node, 15);
// rmnode(haed_node, 5);
// add_locat_node(haed_node, 4, 12);
// print_node(*haed_node);
printf("%p\n", haed_node);
printf("%p\n", &haed_node->next->data);
printf("%p\n", &haed_node->next->next->data);
printf("---------------------\n");
destroy_list(haed_node);
// print_node(*haed_node);
printf("%p\n", haed_node);
printf("%p\n", &haed_node->next->data);
printf("%p\n", &haed_node->next->next->data);
}
int main(int argc, char const *argv[])
{
link_list();
return 0;
}