C语言链表(基本功能函数)

  1. 创建链表
Node* CreateLinkList(const int n)
{
	Node *head     = NULL;
	Node *previous = NULL;
	Node *current  = NULL;
	int i = 0;

	for (i = 0; i < n; i++){
		current = (Node *)malloc(sizeof(Node));
		if (current == NULL){
			printf("内存分配失败!\n");
			exit(-1);
		}
		if (head == NULL)     //头结点的地址指向头指针
			head = current;
		if (previous != NULL)
			previous->next = current;
		printf("请输入数值: ");
		scanf("%d", ¤t->data);
		current->next = NULL;
		previous = current;
	}
	return head;
}

2.遍历链表

void TraverseLinkList(Node* const head)
{
	Node *current = NULL;  //临时指针

	current = head;
	printf("链表遍历结果为:");
	while (current != NULL){
		printf(" %d", current->data);
		current = current->next;
	}
	printf("\n");
}

3.获取链表长度

int GetListLength(Node* const head)
{
	Node *current = NULL;  //临时指针
	int len = 0;

	current = head;
	while (current != NULL){
		len++;
		current = current->next;
	}
	return len;
}

4.插入链表

int InsertList(Node **phead, int i, int val)  
{
	Node *head     = NULL;  //临时指针
	Node *previous = NULL;
	Node *current  = NULL;
	Node *new_code = NULL;
	int len = 0;
	int j = 1;
	head = *phead;

	len = GetListLength(head);
	if (i > len + 1 || i < 1){
		printf("节点不存在...\n");
		return FALSE;
	}

	new_code = (Node *)malloc(sizeof(Node));  //为新节点分配内存
	//新元素插入到链表头部,更新头指针的值
	if (i == 1){   
		new_code->data = val;
		new_code->next = head;
		*phead = new_code;   //更新头指针的值
		return TRUE;
	}

	//新元素插入到链表非头部的其他位置
	current = head;
	while (j < i){
		previous = current;
		current = current->next;
		j++;
	}
	previous->next = new_code;
	new_code->next = current;
	new_code->data = val;
	return TRUE;
}

5.删除链表中节点

int DeleteElem(Node **phead, int i)
{
	Node *head     = NULL;   //临时指针
	Node *previous = NULL;
	Node *current  = NULL;
	int j = 1;
	int len = 0;

	head = *phead;
	len = GetListLength(head);
	if (i > len){
		printf("不再该节点...\n");
		return FALSE;
	}
	current = head;
	if (i == 1){   //删除头节点
		*phead = current->next;  //更新头指针
		free(current);
		current = NULL;
		return TRUE;
	}
	while (j < i){
		previous = current;
		current = current->next;
		j++;
	}
	previous->next = current->next;
	free(current);
	current = NULL;
	return TRUE;
}

6.销毁链表

void DestroyList(Node **phead)
{
	Node *previous = NULL;
	Node *current  = NULL;

	current = *phead;
	while (current != NULL){
		previous = current;
		current = current->next;
		free(previous);
		previous = NULL;
	}
	*phead = NULL;
}

7.链表排序(冒泡排序法)

void SortList(Node *head)
{
	Node *current  = NULL;
	Node *p = NULL;
	int tmp = 0;

	current = head;
	if (current == NULL || current->next == NULL)
		return;
	
	for (current = head; current->next != NULL; current = current->next){
		for (p = head; p->next != NULL; p = p->next){
			if (p->data > p->next->data){
				tmp = p->data;
				p->data = p->next->data;
				p->next->data = tmp;
			}
		}
	}
}

8.主函数

#define  TRUE  1
#define  FALSE 0

/* 单链表存储结构 */
typedef struct Node
{
	int data;
	struct Node *next;
}Node;

int main(void)
{
	Node *head = NULL;
	int val = 0;
	int n = 0;

	head = CreateLinkList(5);  //创建链表
	TraverseLinkList(head);  //遍历

	printf("链表长度为: %d\n", GetListLength(head));

	SortList(head);          //排序
	printf("排序之后");
	TraverseLinkList(head);  //遍历
	printf("\n");

	/* 插入新节点 */
	printf("新节点的位置: ");
	scanf("%d", &n);
	printf("新节点的值: ");
	scanf("%d", &val);
	InsertList(&head, n, val);
	printf("插入新节点之后");
	TraverseLinkList(head);
	printf("链表长度为: %d\n", GetListLength(head));
	printf("\n");

	/* 删除节点 */
	printf("要删除的节点的位置: ");
	scanf("%d", &n);
	DeleteElem(&head, n);
	printf("删除节点之后");
	TraverseLinkList(head);
	printf("链表长度为: %d\n", GetListLength(head));
	printf("\n");

	/* 销毁链表 */
	printf("销毁链表之后");
	DestroyList(&head);
	printf("head = %p\n", head);  //打印头节点的值(head = NULL 头节点释放成功)
	printf("链表长度为: %d\n", GetListLength(head));
	printf("\n");
	
	printf("hello...\n");
	return 0;
}

9.测试结果
C语言链表(基本功能函数)_第1张图片

你可能感兴趣的:(C语言链表(基本功能函数))