头文件
#include <stdio.h>
#include <stdlib.h>
1.定义链表结构体
struct Node
{
int value;
struct Node* next;
};
2.定义新的链表节点
int init(struct Node **head)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return -1;
}
newnode ->value = 0;
newnode ->next = NULL;
*head = newnode;
}
3.打印链表(将打印模块化,方便后面的使用)
void print(struct Node *head)
{
if (head == NULL)
{
printf("It is empty!\n");
return ;
}
while (head ->next != NULL)
{
printf("%d ", head ->next ->value);
head = head ->next;
}
4单向链表的尾插法
int insert_tail(struct Node *head, int x)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return -1;
}
newnode ->value = x;
newnode ->next = NULL;
while (head->next != NULL)
{
head = head->next;
}
head->next = newnode;
}
5.单向链表的头插法
int insert_head(struct Node *head, int x)
{
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return -1;
}
newnode ->value = x;
newnode ->next = head ->next;
head ->next = newnode;
}
6.计算链表的长度
int length (struct Node *head)
{
int count = 0;
while (head -> next !=NULL)
{
count ++;
head = head ->next;
}
return count;
}
7.单向链表的中间插入
int insert_index(struct Node *head, int x, int index)
{
if (index < 0 || index > length(head))
{
printf("index is error!\n");
return -1;
}
for (int i = 1; i < index ; i++)
{
head = head ->next;
}
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node));
if (NULL == newnode)
{
return -1;
}
newnode ->value = x;
newnode ->next = head ->next;
head ->next = newnode;
}
8.在单向链表中按照位置改变链表
int update_index(struct Node *head, int x, int index)
{
if (index < 0 || index > length(head))
{
printf("index is error!\n");
return -1;
}
for (int i = 1; i < index ; i++)
{
head = head ->next;
}
head =head -> next;
head -> value = x;
}
9.在单向链表中按照位置删除链表
int delete_index(struct Node *head, int index)
{
if (index < 0 || index > length(head))
{
printf("index is error!\n");
return -1;
}
for (int i = 1; i < index; i++)
{
head = head ->next;
}
struct Node *ptr = head -> next;
head -> next = ptr -> next;
free(ptr);
}
10.按照数值来查找链表
int search_value(struct Node *head, int num)
{
int count = 0;
int place = 0;
while (head != NULL)
{
if (head -> value == num)
{
count ++;
printf("The index of the number is :%d\n", place);
}
head = head ->next;
place++;
}
printf("The sum of the number is:%d\n", count);
}
11.按照位置来查找链表
int search_index(struct Node *head, int index)
{
if (index < 0 || index > length(head))
{
printf("index is error!\n");
}
for (int i = 0; i < index; i++)
{
head = head ->next;
}
printf("The value of the index %d is :%d\n", index, head ->value);
return 0;
}
11.按照数值来更新链表
int update_value(struct Node *head , int x, int num)
{
while (head -> next != NULL)
{
if (head -> next -> value == num)
{
head -> next -> value = x;
}
head = head ->next;
}
return 0;
}
12.按照数值来删除链表
int delete_value(struct Node *head, int value)
{
int len = length(head);
for (int i = 0; i < len; i++)
{
if (head -> next -> value == value)
{
struct Node *str = head ->next;
head -> next = str -> next;
free(str);
}
else
{
head = head -> next;
}
}
}
12.给单向链表排序
int sort(struct Node *head)
{
int i = 0;
int j = 0;
struct Node *p = head;
int len = length(head);
for ( i = 0; i < len - 1; i++)
{
int flag = 0;
head = p;
for ( j = 0; j < len - i - 1; j++)
{
if (head ->next ->value > head ->next ->next ->value)
{
struct Node *ptr1 = head ->next;
struct Node *ptr2 = head ->next ->next;
ptr1 ->next = ptr2 ->next;
ptr2 ->next = ptr1;
head ->next = ptr2;
flag = 1;
}
head = head ->next;
if (flag = 0)
{
break;
}
}
}
}
13.将单向链表倒序(一般方法)
void reserve1(struct Node **head)
{
struct Node *prev = NULL;
struct Node *cur = (*head) -> next;
struct Node *Next = cur -> next;
while (Next != NULL)
{
cur ->next = prev;
prev = cur;
cur = Next;
Next = cur ->next;
}
if (cur != NULL)
{
cur ->next = prev;
}
(*head) ->next = cur;
}
14.将单向链表倒序(递归法)
struct Node *reserver2(struct Node *head)
{
struct Node *Next = NULL;
if (head ->next != NULL)
{
Next = reserver2(head ->next);
head ->next ->next = head;
head ->next = NULL;
return Next;
}
return head;
}
15.最后,释放链表空间
struct Node *freeall(struct Node *head)
{
while (head ->next != NULL)
{
struct Node *ptr = head;
head = head ->next;
free(ptr);
}
free(head);
head = NULL;
return head;
}
测试上述功能的主函数
int main()
{
struct Node *head;
init(&head);
int i = 1;
for ( i = 1; i < 10; i++)
{
insert_tail(head , i);
}
print(head);
for ( i = 1; i < 10; i++)
{
insert_head(head , i);
}
print(head);
insert_index(head, 0, 8);
print(head);
update_index(head, 99, 8);
print(head);
delete_index(head, 8);
print(head);
search_value(head, 9);
search_index(head, 6);
update_value(head, 88, 9);
print(head);
delete_value(head, 1);
print(head);
sort(head);
print(head);
reserve1(&head);
print(head);
head ->next = reserver2(head ->next);
print(head);
head = freeall(head);
print(head);
return 0;
}