1.双向链表按任意位置插入
DoubleLink change_pos(DoubleLink head, datatype element, int pos)
{
if (pos < 1 || pos > length(head) + 1)
{
puts("修改:位置不合法");
return head;
}
if (1 == pos)
{
printf("修改: 1 位的 %d 修改为 %d\n", head->data, element);
head->data = element;
return head;
}
DoubleLink p = head;
for (int i = 1; i < pos; i++)
{
p = p->next;
}
printf("修改:%d 位的 %d 修改为 %d\n", pos, p->data, element);
p->data = element;
return head;
}
2.双向链表按任意位置删除
DoubleLink delete_pos(DoubleLink head, int pos)
{
if (pos < 1 || pos > length(head))
{
puts("删除:位置不合法");
return head;
}
if (1 == pos)
{
printf("删除:删除 1 处元素 %d\n", head->data);
free(head);
head = NULL;
return head;
}
DoubleLink p = head;
for (int i = 1; i < pos - 1; i++)
{
p = p->next;
}
DoubleLink s = p->next;
p->next = s->next;
if (p->next != NULL)
{
p->next->front = p;
}
printf("删除:删除 %d 处元素 %d\n", pos, s->data);
free(s);
s = NULL;
return head;
}
3.思维导图
源代码
myfunc.h
#ifndef __MYFUNC_H__
#define __MYFUNC_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum num
{
FALSE = -1,
SUCCESS
};
typedef int datatype;
typedef struct Node
{
datatype data;
struct Node* front;
struct Node* next;
} *DoubleLink;
DoubleLink create();
DoubleLink insert_head(DoubleLink head, datatype element);
DoubleLink insert_rear(DoubleLink head, datatype element);
DoubleLink delete_head(DoubleLink head);
DoubleLink delete_rear(DoubleLink head);
DoubleLink insert_pos(DoubleLink head, datatype element, int pos);
DoubleLink delete_pos(DoubleLink head, int pos);
DoubleLink change_pos(DoubleLink head, datatype element, int pos);
DoubleLink search_element(DoubleLink head, datatype element);
DoubleLink change_element(DoubleLink head, datatype old_element, datatype element);
DoubleLink delete_element(DoubleLink head, datatype element);
DoubleLink sort(DoubleLink head);
DoubleLink free_space(DoubleLink head);
void show(DoubleLink head);
int length(DoubleLink head);
#endif
myfunc.c
#include "myfunc.h"
DoubleLink create()
{
DoubleLink s = (DoubleLink)malloc(sizeof(struct Node));
if (NULL == s)
{
return NULL;
}
s->data = 0;
s->front = NULL;
s->next = NULL;
return s;
}
DoubleLink insert_head(DoubleLink head, datatype element)
{
DoubleLink s = create();
if (NULL == s)
{
return NULL;
}
s->data = element;
if (NULL == head)
{
head = s;
}
else
{
s->next = head;
head->front = s;
head = s;
}
printf("头插元素 %d\n", element);
return head;
}
void show(DoubleLink head)
{
if (NULL == head)
{
puts("展示:链表为空");
return;
}
printf("展示:链表中元素:");
DoubleLink p = head;
while (p)
{
printf("%2d", p->data);
if (NULL == p->next)
{
break;;
}
p = p->next;
}
printf("\n");
printf("展示:逆序:");
while (p)
{
printf("%2d", p->data);
p = p->front;
}
printf("\n");
}
DoubleLink insert_rear(DoubleLink head, datatype element)
{
DoubleLink s = create();
if (NULL == s)
{
return NULL;
}
s->data = element;
if (NULL == head)
{
head = s;
}
else
{
DoubleLink p = head;
while (p->next)
{
p = p->next;
}
p->next = s;
s->front = p;
}
printf("尾插:元素 %d\n", s->data);
return head;
}
DoubleLink delete_head(DoubleLink head)
{
if (NULL == head)
{
puts("头删:链表为空");
return NULL;
}
DoubleLink s = head;
head = s->next;
head->front = NULL;
s->next = NULL;
printf("头删:元素 %d\n", s->data);
free(s);
s = NULL;
return head;
}
DoubleLink delete_rear(DoubleLink head)
{
if (NULL == head)
{
puts("尾删:链表为空");
return NULL;
}
if (NULL == head->next)
{
printf("尾删:元素 %d\n", head->data);
free(head);
head = NULL;
return head;
}
DoubleLink p = head;
while (p->next->next)
{
p = p->next;
}
DoubleLink s = p->next;
p->next = NULL;
printf("尾删:元素 %d\n", s->data);
free(s);
s = NULL;
return head;
}
int length(DoubleLink head)
{
int len = 0;
DoubleLink p = head;
while (p)
{
p = p->next;
len++;
}
return len;
}
DoubleLink insert_pos(DoubleLink head, datatype element, int pos)
{
if (pos < 1 || pos > length(head) + 1)
{
puts("插入:位置不合法");
return head;
}
if (1 == pos)
{
printf("插入:");
head = insert_head(head, element);
return head;
}
DoubleLink p = head;
for (int i = 1; i < pos - 1; i++)
{
p = p->next;
}
DoubleLink s = create();
s->data = element;
s->next = p->next;
s->next->front = s;
p->next = s;
s->front = p;
printf("插入:在 %d 处插入元素 %d\n", pos, s->data);
return head;
}
DoubleLink delete_pos(DoubleLink head, int pos)
{
if (pos < 1 || pos > length(head))
{
puts("删除:位置不合法");
return head;
}
if (1 == pos)
{
printf("删除:删除 1 处元素 %d\n", head->data);
free(head);
head = NULL;
return head;
}
DoubleLink p = head;
for (int i = 1; i < pos - 1; i++)
{
p = p->next;
}
DoubleLink s = p->next;
p->next = s->next;
if (p->next != NULL)
{
p->next->front = p;
}
printf("删除:删除 %d 处元素 %d\n", pos, s->data);
free(s);
s = NULL;
return head;
}
DoubleLink change_pos(DoubleLink head, datatype element, int pos)
{
if (pos < 1 || pos > length(head) + 1)
{
puts("修改:位置不合法");
return head;
}
if (1 == pos)
{
printf("修改: 1 位的 %d 修改为 %d\n", head->data, element);
head->data = element;
return head;
}
DoubleLink p = head;
for (int i = 1; i < pos; i++)
{
p = p->next;
}
printf("修改:%d 位的 %d 修改为 %d\n", pos, p->data, element);
p->data = element;
return head;
}
DoubleLink search_element(DoubleLink head, datatype element)
{
if (NULL == head)
{
puts("按元素查找:链表为空");
return head;
}
int i = 1;
DoubleLink q = head;
for (i = 1; q != NULL; i++)
{
if (q->data == element)
{
break;
}
q = q->next;
}
if (NULL == q)
{
printf("按元素查找:%d 不存在\n", element);
return head;
}
printf("按元素查找:%d 在 %d 位\n", element, i);
return head;
}
DoubleLink change_element(DoubleLink head, datatype old_element, datatype element)
{
if (NULL == head)
{
puts("按元素修改:链表为空");
return head;
}
int i = 1;
DoubleLink q = head;
for (i = 1; q != NULL; i++)
{
if (q->data == old_element)
{
break;
}
q = q->next;
}
if (NULL == q)
{
printf("按元素修改:%d 不存在\n", element);
return head;
}
q->data = element;
printf("按元素修改:%d 位的 %d 修改为 %d\n", i, old_element, element);
return head;
}
DoubleLink delete_element(DoubleLink head, datatype element)
{
if (NULL == head)
{
puts("按元素删除:链表为空");
return head;
}
int i = 1;
DoubleLink q = head;
for (i = 1; q != NULL; i++)
{
if (q->data == element)
{
break;
}
q = q->next;
}
if (NULL == q)
{
printf("按元素删除:%d 不存在\n", element);
return head;
}
if (head->data == element)
{
q = head;
head = head->next;
free(q);
q = NULL;
printf("按元素删除:删除在 %d 位的 %d\n", i, element);
return head;
}
DoubleLink p = head;
while (p->next != q)
{
p = p->next;
}
p->next = q->next;
free(q);
q = NULL;
printf("按元素删除:删除在 %d 位的 %d\n", i, element);
return head;
}
DoubleLink sort(DoubleLink head)
{
if (NULL == head)
{
puts("排序:链表为空");
return head;
}
if (NULL == head->next)
{
puts("排序:链表只有一个元素");
return head;
}
DoubleLink p, q;
p = head;
while (p->next != NULL)
{
q = p->next;
while (q != NULL)
{
if (p->data > q ->data)
{
datatype t = p->data;
p->data = q->data;
q->data = t;
}
q = q->next;
}
p = p->next;
}
puts("排序:完成");
return head;
}
DoubleLink free_space(DoubleLink head)
{
puts("清空链表");
if (NULL == head)
{
return NULL;
}
DoubleLink p;
while (head != NULL)
{
p = head;
head = head->next;
free(p);
p = NULL;
}
head = NULL;
return head;
}
test.c
#include "myfunc.h"
int main()
{
DoubleLink head = NULL;
int n;
scanf("%d", &n);
datatype element;
for (int i = 0; i < n; i++)
{
scanf("%d", &element);
head = insert_rear(head, element);
if (NULL == head)
{
printf("插入元素 %d 失败\n", element);
}
}
printf("----输入完毕----\n\n");
show(head);
int pos;
printf("输入位置:");
scanf("%d", &pos);
// printf("输入元素:");
// scanf("%d", &element);
head = delete_pos(head, pos);
show(head);
// printf("输入新元素:");
// scanf("%d", &element);
// head = change_pos(head, old_element, element);
// show(head);
// printf("输入元素:");
// scanf("%d", &element);
// head = delete_element(head, element);
// show(head);
// head = sort(head);
// show(head);
// head = free_space(head);
// show(head);
return 0;
}