干货1:头结点链表

#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
Node * Create_List()
{
	Node *list = (Node*)malloc(sizeof(Node)/sizeof(char));
	if (list == NULL)
		return NULL;
	
	list->next = NULL;   // 空表
	
	return list;
}

// 头插
int Insert_Head(Node *h, LinkData data)
{
	if (h == NULL)
		return FALSE;
	Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
	if (node == NULL)
	{
		return FALSE;
	}
	
	node->data = data;
	node->next = h->next;
	
	h->next = node;
	
	return TRUE;
}

// 尾插
int Insert_Last(Node *h, LinkData data)
{
	if (h == NULL)
		return FALSE;
	
	Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
	if (node == NULL)
	{
		return FALSE;
	}
	node->data = data;
	node->next = NULL;
	
	Node* tmp = h;
	while (tmp->next)
	{
		tmp = tmp->next;
	}
	
	tmp->next = node;
	
	return TRUE;
}

// 从pos处插入
int Insert_Pos(Node *h, int pos, LinkData data)
{
	if (h == NULL || pos < 1)
		return FALSE;
	
	// 找要插入位置的前一个结点
	Node *tmp = h;
	int i;
	for (i = 0; i < pos-1; i++)
	{
		if (tmp == NULL)
			break;
		tmp = tmp->next;
	}
	
	if (tmp == NULL)   // 越界
	{
		printf("插入位置越界\n");
		return FALSE;
	}
	
	Node *node = (Node*)malloc(sizeof(Node)/sizeof(char));
	if (node == NULL)
	{
		return FALSE;
	}
	node->data = data;
	node->next = tmp->next;
	tmp->next  = node;
	
	return TRUE;
}

// 根据下标删除结点
int Delete_Pos(Node* h, int pos)
{
	if (h == NULL || pos < 1)
		return FALSE;
	
	// 找要删除位置的前一个结点
	Node *tmp = h;
	int i;
	for (i = 0; i < pos-1; i++)
	{
		if (tmp->next == NULL)
			break;
		tmp = tmp->next;
	}
	
	if (tmp->next == NULL)   // 越界
	{
		printf("删除位置越界\n");
		return FALSE;
	}
	
	Node *p = tmp->next;
	tmp->next = p->next;
	free(p);
	
	return TRUE;
}

// 逆序
int Reverse_List(Node *h)
{
	// h->next 空表
	// h->next->next 只有一个结点
	if (h == NULL || h->next == NULL || h->next->next == NULL)
		return FALSE;
	
	Node *pre = h->next;
	Node *cur = h->next->next;
	Node *tmp;
	
	while (cur)
	{
		tmp = cur->next;
		cur->next = pre;
		pre = cur;
		cur = tmp;
	}
	
	h->next->next = NULL;
	h->next = pre;
	
	return TRUE;
}

// 根据数据删除结点
int Delete_Data(Node* h, LinkData data)
{
	if (h == NULL)
		return FALSE;
	
	Node *tmp = h;
	while (tmp->next)
	{
		if (tmp->next->data == data)
			break;
		tmp = tmp->next;
	}
	
	if (tmp->next == NULL)
		return FALSE;
	
	Node *p = tmp->next;
	tmp->next = p->next;
	free(p);
	
	return TRUE;
}

// 搜索结点
int Find_Element(Node* h, LinkData data, int *x)
{
	if (h == NULL)
		return FALSE;
	
	Node *tmp = h->next;
	int k = 1;
	while (tmp)
	{
		if (tmp->data == data)
		{
			*x = k;
			return TRUE;
		}
		k++;
		tmp = tmp->next;
	}
	
	return FALSE;
}

// 获取结点数据
int Get_Element(Node* h, int pos, int *x)
{
	if (h == NULL || pos < 1)
		return FALSE;
	
	int i;
	Node *tmp = h;
	for (i = 0; i < pos; i++)
	{
		if (tmp == NULL)
			break;
		tmp = tmp->next;
	}
	
	if (tmp == NULL)
		return FALSE;
	else
		*x = tmp->data;
	
	return TRUE;
}

// 获取链表长度
int Get_Len(Node * h)
{
	if (h == NULL)
		return 0;
	
	Node *tmp = h;
	int count = 0;
	while (tmp->next)
	{
		count++;
		tmp = tmp->next;
	}
	
	return count;
}

// 清空链表
int Clean_List(Node * h)
{
	if (h == NULL)
		return FALSE;
	
	Node *tmp = h;
	while (tmp->next)
	{
		Delete_Pos(h, 1);
	}
	
	return 0;
}

// 打印链表
void Display(Node *h)
{
	if (h == NULL)
		return;
	
	int count = 0;
	Node *tmp = h->next;
	while (tmp)
	{
		if (count++ % 4 == 0)
			printf ("\n");
		printf ("%8d", tmp->data);
		
		tmp = tmp->next;
	}
	printf ("\n");
}

// 销毁链表
int Destroy(Node *h)
{
	if (h == NULL)
		return FALSE;
	Clean_List(h);
	free(h);
	
	return TRUE;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值