C语言下实现链表的新建,追加,插入,删除,打印,释放操作
<链表是数据结构的基础知识,这次我利用了函数和指针实现了链表节点的几个基本操作>
展示效果在文章末尾
代码如下:
/************************************************
@2020/04/03 21:03
@yudashuai
链表的新建, 追加, 删除, 插入, 打印, 释放
************************************************/
struct Node
{
int data;
struct Node *next;
};
void clean(struct Node *head);
void display(struct Node *head);
struct Node *new(struct Node *head);
struct Node *delete(struct Node *head, int target);
struct Node *insert(struct Node *head, int target);
int main(void)
{
char choose;
struct Node *head = NULL;
printf("想要增加节点吗?\n");
scanf(" %c", &choose);
while(getchar() != '\n')
{
}
while(choose == 'y' || choose == 'Y')
{
head = new(head);
printf("还想要增加节点吗?\n");
scanf(" %c", &choose);
while(getchar() != '\n')
{
}
}
// display
display(head);
// delete
printf("想要删除节点吗?\n");
scanf(" %c", &choose);
while(getchar() != '\n')
{
}
while(choose == 'y' || choose == 'Y')
{
int x;
printf("请输入想要删除的节点值:\n");
scanf("%d", &x);
head = delete(head, x);
printf("还想要删除节点吗?\n");
scanf(" %c", &choose);
while(getchar() != '\n')
{
}
}
display(head);
// insert
printf("想要插入节点吗?\n");
scanf(" %c", &choose);
while(getchar() != '\n')
{
}
while(choose == 'y' || choose == 'Y')
{
int x;
printf("请输入想要插入的节点值:\n");
scanf("%d", &x);
head = insert(head, x);
printf("还想要插入节点吗?\n");
scanf(" %c", &choose);
while(getchar() != '\n')
{
}
}
display(head);
// clean
clean(head);
return 0;
}
struct Node *new(struct Node *head)
{
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
struct Node *pr = head;
if(p == NULL)
{
printf("已经没有足够的内存空间了\n");
exit(0);
}
if(head == NULL)
{
head = p;
p -> next = NULL;
printf("请输入第一个节点值:\n");
scanf("%d", &p->data);
}
else
{
while(pr -> next != NULL)
{
pr = pr -> next;
}
printf("请再输入节点值:\n");
scanf("%d", &p->data);
pr -> next = p;
p -> next = NULL;
}
return head;
}
void display(struct Node *head)
{
struct Node *p = head;
int count = 1;
while(p != NULL)
{
printf("第%d个节点值为:%d\n", count, p -> data);
++count;
p = p -> next;
}
}
struct Node *insert(struct Node *head, int target)
{
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
struct Node *q = head;
struct Node *pr = head;
p -> data = target;
if(head == NULL) // 若链表为空
{
head = p;
p -> next = NULL;
return head;
}
else if(head -> data > target) // 若插到第一个节点
{
p -> next = head;
head = p;
return head;
}
else
{
while(q -> data < target && q -> next != NULL)
{
q = q -> next;
printf("%d\n", q->data);
}
printf("不插到第一个节点\n");
if(q -> data >= target) // 插入中间节点
{
while(pr -> next != q)
{
pr = pr -> next;
}
p -> next = pr -> next;
pr -> next = p;
return head;
}
else
{
printf("应该插入最后一个节点\n");
pr = q;
pr -> next = p;
p -> next = NULL;
return head;
}
}
}
struct Node *delete(struct Node *head, int target)
{
struct Node *p = head;
struct Node *pr = head;
if(head == NULL)
{
printf("没有节点可供删除!\n");
return head;
}
else if(head != NULL)
{
while(p -> data != target && p -> next != NULL) // 搜索待删除节点
{
p = p -> next;
}
if(p -> next == NULL && p -> data != target)
{
printf("未找到节点!\n");
return head;
}
else
{
if(head -> data == target) // 若头节点正是要删除的节点
{
head = head -> next;
free(p);
}
else
{
while(pr -> next != p) // 待删除节点为中间节点
{
pr = pr -> next;
}
pr -> next = p -> next;
free(p);
}
return head;
}
}
}
void clean(struct Node *head)
{
struct Node *p = head;
struct Node *pr = p;
while(p != NULL)
{
pr = p;
p = p -> next;
free(pr);
}
}
展示效果: