面经 常见数据结构的算法 - 单向链表

常见数据结构的算法。总结一下现在遇到的常见的算法,也是给自己一个交待吧。

单向链表:

话不多说直接上代码。个人感觉无论原理多么理解还是需要自己去实现一遍。在实现过程中思考的会更加仔细。

结构体:

typedef struct CacheInfo
{
	int QryId;
	char* data;
};
typedef struct CacheMsg 
{
	CacheInfo Cache;
	struct CacheMsg *next;
};

头文件:

class link_table: public Base
{
public:
	link_table();
	~link_table();
	//代码是根据项目需求做了些封装。可能有些不尽人意。

	void UserArithmetic();
	//创建链表
	bool CacheMsg_CreateLinkTable();
	
	//根据位置插入元素
	bool CacheMsg_Insert(int pos, CacheInfo data);
	
	//获取链表长度
	int	 CacheMsg_GetLength();
	
	//查找元素
	bool CacheMsg_Find_Elem(int QryId, int* Outpos);
	
	//封装好的,直接可以插入元素。不用考虑位置等,直接插入数据
	bool CacheMsg_CacheInsert(CacheInfo data);

	//封装好的。直接删除
	bool CacheMsg_Cache_Delete(int QryId);
	
	//删除其他节点
	bool CacheMsg_Delete_Elem_Other(int pos);
	
	//删除头节点
	bool CacheMsg_Delete_Elem_Head(int pos);
	
	//删除元素
	bool CacheMsg_Delete_Elem(int pos);


protected:
	

private:
	//头节点
	CacheMsg *head;
};

代码实现:

#include "lianbiao.h"

link_table::link_table()
{

}
link_table::~link_table()
{

}

//程序入口
void link_table::UserArithmetic()
{
	//create link
	CacheMsg_CreateLinkTable();

	CacheInfo data;
	data.QryId = 1;
	data.data = "hello";
	CacheMsg_CacheInsert(data);

	data.QryId = 2;
	CacheMsg_CacheInsert(data);


	data.QryId = 3;
	CacheMsg_CacheInsert(data);

	

	data.QryId = 4;
	CacheMsg_CacheInsert(data);

	
	CacheMsg_Cache_Delete(0);
	CacheMsg_Cache_Delete(5);
	CacheMsg_Cache_Delete(1);
	CacheMsg_Cache_Delete(2);
	CacheMsg_Cache_Delete(4);
	CacheMsg_Cache_Delete(3);
	
}

//插入数据
bool link_table::CacheMsg_CacheInsert(CacheInfo data)
{
	bool ret = true;
	int len = CacheMsg_GetLength();
	int depth = len - 1;
	printf("%d\n", len);
	ret = CacheMsg_Insert(depth, data);

	return ret;
}

//创建
bool link_table::CacheMsg_CreateLinkTable()
{
	//CachMsg_CreateHead();
	head = (CacheMsg*)malloc(sizeof(CacheMsg));
	if (head == NULL)
	{
		return false;
	}
	memset(head, 0, sizeof(CacheMsg));
	return true;
}

bool link_table::CacheMsg_Insert(int pos, CacheInfo data)
{
	if (pos < 0)
	{
		return false;
	}
	CacheMsg* temp = head;
	CacheMsg* node = (CacheMsg*)malloc(sizeof(CacheMsg));
	if (node == NULL)
	{
		return false;
	}
	int i = 0;
	while((temp != NULL) && (i < pos))
	{
		temp = temp->next;
		i++;
	}
	if (temp == NULL)
	{
		//如果是尾节点
		return false;
	}
	node->next = NULL;
	temp->next = node;
	temp->Cache = data;
	return true;
}

int link_table::CacheMsg_GetLength()
{
	CacheMsg* temp = head;
	int len = 0;
	while(temp != NULL)
	{
		temp = temp->next;
		len++;
	}
	return len;
}

bool link_table::CacheMsg_Find_Elem(int QryId, int* Outpos)
{
	bool ret = false;
	CacheMsg* temp = head;
	int len = 0;
	while (temp != NULL)
	{
		if (temp->Cache.QryId == QryId)
		{
			ret = true;
			break;
		}
		++len;
		temp = temp->next;
	}
	*Outpos = len;
	return ret;
}

bool link_table::CacheMsg_Cache_Delete(int QryId)
{
	//判断是否有这个元素
	int pos = 0;
	bool ret = CacheMsg_Find_Elem(QryId, &pos);
	if (ret == false)
	{
		//当前没有此元素
		return false;
	}
	//删除这个元素
	ret = CacheMsg_Delete_Elem(pos);
	if (ret == false)
	{
		//删除失败
		return false;
	}

	return true;
}

bool link_table::CacheMsg_Delete_Elem(int pos)
{
	bool ret = true;
	if (pos == 0)
	{
		//删除头结点
		ret = CacheMsg_Delete_Elem_Head(pos);
		if (ret == false)
		{
			ret = false;
		}
	}
	else
	{
		//其他子结点
		ret = CacheMsg_Delete_Elem_Other(pos);
		if (ret == false)
		{
			ret = false;
		}
	}
	
	return ret;
}

bool link_table::CacheMsg_Delete_Elem_Head(int pos)
{
	bool ret = true;
	CacheMsg* temp = head;
	CacheMsg* node;
	CacheMsg* delete_node;
	if (pos < 0)
	{
		//没找到元素
		return false;
	}
	if (temp == NULL)
	{
		//尾节点
		return false;
	}
	//头结点
	delete_node = temp;
	node = temp->next;
	head = node;

	return ret;
}

bool link_table::CacheMsg_Delete_Elem_Other(int pos)
{
	bool ret = true;
	CacheMsg* temp = head;
	CacheMsg* node;
	CacheMsg* delete_node;

	int depth = pos - 1;
	int len = 0;
	while ((temp != NULL) && (len < depth))
	{
		++len;
		temp = temp->next;
	}
	if (temp == NULL)
	{
		//尾节点
		return false;
	}
	delete_node = temp->next;
	node = temp->next->next;
	temp->next = node;

	return ret;
}


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值