数据结构:头结点线性链表

这两天在学习数据结构,很难,无暇顾及,很难坚持一天一篇了。

今天我研究的是头结点线性链表,我写了个头文件和.c文件,里面有链表的创造消除和一些基本的使用,方便以后用。

昨天发现我上次写的顺序表非常不标准,这次我做足了功夫,以后要是有机会,我把前面的也改了。

这次我没有发主函数,因为主函数在我调试的时候已经改的面目全非,留下了也没有什么意义。以后的代码都会很长,写完特有成就感。

明天开始要做点小项目了,有点小期待。


先是头文件

#ifndef __LinkList_H__
#define __LinkList_H__

#define FALSE -1
#define TRUE 0

typedef int LinkData;

typedef struct _node
{
	LinkData data;
	struct _node *next;
}NODE;

// 创建链表
NODE * Create_List();

// 插入元素:尾插
int Insert_Last(NODE* h,LinkData data);

// 插入元素:头插
int Insert_Head(NODE* h,LinkData data);

// 输出链表元素
void Risplay(NODE* h);

// 插入元素:在第pos个结点处插入数据
int Insert_Pos(NODE* h, int pos, LinkData data);

// 删除元素:删除第pos个结点的元素
int Del_Pos(NODE* h, int pos);

// 逆序
int Reverse_List(NODE* h);

// 插入元素:删除指定数据(只能删除第一个)
int Delete_Data(NODE* h, LinkData data);

// 查找元素: 通过值查找,返回位置
int Find_Element(NODE* h, LinkData data, int *x);

// 查找元素:通过位置获取,返回值
int Get_Element(NODE* h, int pos, LinkData *x);

// 求链表长度:通过函数返回值返回
int Get_Len(NODE* h);

// 清空所有结点
int Clean_List(NODE* h);

// 销毁链表
int Destroy(NODE* h);

#endif
再是.c文件

#include<stdio.h>
#include"LinkList.h"
#include<stdlib.h>

// 创建一个头结点线性链表
NODE * Create_List()
{
	NODE *head = (NODE *)malloc(sizeof(NODE)/sizeof(char));
	if (head == NULL)
	{
		printf("创建失败。\n");
		return NULL;
	}
	head->next = NULL;
	return head;
} 

// 插入元素:尾插
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;    
}

// 插入元素:头插
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;
}


// 输出链表元素
void Risplay(NODE * h)
{
	if (h == NULL)
		return ;
	int count = 0;
	while(h->next != NULL)
	{
		if(count++ %4 == 0)
			printf("\n");
		printf("%5d",h->next->data);
		h = h->next; 
	}
	printf("\n");	
}


// 插入元素:在第pos个结点处插入数据
int Insert_Pos(NODE *h, int pos, LinkData data)
{
	if(h == NULL || pos<1)
		return FALSE;
	
	NODE *node = (NODE *)malloc(sizeof(NODE)/sizeof(char));
	if (node == NULL)
		return FALSE;
	
	node->data = data;
	
	int i ;
	NODE *tmp = h;
	for (i=0;i<pos-1;i++)
	{
		if(tmp == NULL)
			break;
		tmp = tmp->next;
	}
	if (tmp == NULL)
	{
		printf("插入元素越界,插入失败。\n");
		return FALSE;
	}
	node->next = tmp->next;
	tmp->next = node;
	
	return TRUE;
}

// 删除元素:删除第pos个结点的元素
int Del_Pos(NODE* h, int pos)
{
	if(h == NULL || pos<1)
		return FALSE;
	
	int i ;
	NODE *tmp = h;
	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)
{
	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;   //这里不能写成tmp = cur;
		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 || h->next == NULL)
		return FALSE;
	
	NODE* tmp = h;
	while(tmp->next)
	{
		if(tmp->next->data == data)
			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 Find_Element(NODE* h, LinkData data, int *x)
{
	if (h == NULL || h->next == NULL)
		return FALSE;
	
	NODE* tmp = h->next;
	int count = 1;
	while(tmp)
	{
		if(tmp->data == data)
			break;
		count ++;
		tmp = tmp->next;
	}
	if (tmp == NULL)
	{
		printf("查找失败,没有指定数据。\n");
		return FALSE;
	}
	*x = count;
	return TRUE;
}

// 查找元素:通过位置获取,返回值
int Get_Element(NODE* h, int pos, LinkData *x)
{
	if(h == NULL || h->next==NULL || pos<1 || x==NULL)
		return FALSE;
	
	NODE* tmp = h;
	int i;
	for (i=0;i<pos;i++)
	{
		if(tmp == NULL)
			break;
		tmp = tmp->next;
	}
	if(tmp == NULL)
	{
		printf("位置越界,无法获取。\n");
		return FALSE;
	}
	*x = tmp->data;
	
	return TRUE;
}

// 求链表长度:通过函数返回值返回
int Get_Len(NODE* h)
{
	if(h == NULL )
		return FALSE;
	
	int count = 0;
	NODE* tmp = h;
	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)
	{
		NODE* p = tmp->next;
		tmp->next = p->next;
		free(p);
	}
	
	return TRUE; 
}

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



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值