数据结构各种算法实现--单链表

本文档展示了单链表的定义及其在C++中的实现,包括头结点的使用、链表的基本操作如插入、删除、查找等。代码详细说明了如何创建、操作和销毁单链表,并给出了一个实际应用示例,演示了链表的填充、长度计算、元素查找和删除等操作。
摘要由CSDN通过智能技术生成

线性表的链式存储又称为单链表
通过一组任意的存储单元来存储线性表中的数据元素

(数据元素存储的位置地址可能连续也可能不连续)

通过指针连接不相邻的地址,实现线性逻辑关系

单链表的定义

L=([公式],[公式], [公式] , [公式] ........[公式])
单链表可定义为有头结点和无头结点的两种情况:
在这里插入图片描述
头结点一般不存放数据元素,但可能存放单链表的其它信息,如单链表的表长

  • 判断单链表为空的条件:
  • 无头结点:head为null时,单链表L为空

有头结点:head->next为null时,单链表L为空

定义带有头结点的单链表的优点:

  • 使得链表的第一个位置和其他位置的操作统一
  • 使得空表和非空表的操作统一

ListNode.h



template<typename Type> class SingleList;

template<typename Type> class ListNode{
private:
	friend typename SingleList<Type>;

	ListNode():m_pnext(NULL)
	{
	
	}
	ListNode(const Type item,ListNode<Type> *next=NULL):m_data(item),m_pnext(next)
	{
	
	}
	~ListNode()
	{
		m_pnext=NULL;
	}

public:
	Type GetData();

private:
	Type m_data;
	ListNode *m_pnext;
};

template<typename Type> Type ListNode<Type>::GetData()
{
	return this->m_data;
}

SingleList.h

#include "ListNode.h"

template<typename Type> class SingleList{
public:
	SingleList():head(new ListNode<Type>()){}
	~SingleList(){
		MakeEmpty();
		delete head;
	}

public:
	void MakeEmpty();                       //make the list empty
	int Length();                           //get the length
	ListNode<Type> *Find(Type value,int n); //find thd nth data which is equal to value
	ListNode<Type> *Find(int n);            //find the nth data
	bool Insert(Type item,int n=0);         //insert the data in the nth position
	Type Remove(int n=0);                   //remove the nth data
	bool RemoveAll(Type item);              //remove all the data which is equal to item
	Type Get(int n);                        //get the nth data
	void Print();                           //print the list

private:
	ListNode<Type> *head;
};

template<typename Type> 
void SingleList<Type>::MakeEmpty()
{
	ListNode<Type> *pdel;
	while(head->m_pnext!=NULL)
	{
		pdel=head->m_pnext;
		head->m_pnext=pdel->m_pnext;
		delete pdel;
	}
}

template<typename Type> 
int SingleList<Type>::Length()
{
	ListNode<Type> *pmove=head->m_pnext;
	int count=0;
	while(pmove!=NULL){
		pmove=pmove->m_pnext;
		count++;
	}
	return count;
}

template<typename Type> 
ListNode<Type>* SingleList<Type>::Find(int n)
{
	if(n<0)
	{
		cout<<"The n is out of boundary"<<endl;
		return NULL;
	}
	ListNode<Type> *pmove=head->m_pnext;
	for(int i=0;i<n&&pmove;i++)
	{
		pmove=pmove->m_pnext;
	}
	if(pmove==NULL)
	{
		cout<<"The n is out of boundary"<<endl;
		return NULL;
	}
	return pmove;
}

template<typename Type> 
ListNode<Type>* SingleList<Type>::Find(Type value,int n)
{
	if(n<1)
	{
		cout<<"The n is illegal"<<endl;
		return NULL;
	}
	ListNode<Type> *pmove=head;
	int count=0;
	while(count!=n&&pmove)
	{
		pmove=pmove->m_pnext;
		if(pmove->m_data==value)
		{
			count++;
		}

	}
	if(pmove==NULL)
	{
		cout<<"can't find the element"<<endl;
		return NULL;
	}
	return pmove;
}

template<typename Type> 
bool SingleList<Type>::Insert(Type item, int n)
{
	if(n<0)
	{
		cout<<"The n is illegal"<<endl;
		return 0;
	}
	ListNode<Type> *pmove=head;
	ListNode<Type> *pnode=new ListNode<Type>(item);
	if(pnode==NULL)
	{
		cout<<"Application error!"<<endl;
		return 0;
	}
	for(int i=0;i<n&&pmove;i++)
	{
		pmove=pmove->m_pnext;
	}
	if(pmove==NULL)
	{
		cout<<"the n is illegal"<<endl;
		return 0;
	}
	pnode->m_pnext=pmove->m_pnext;
	pmove->m_pnext=pnode;
	return 1;
}

template<typename Type> 
bool SingleList<Type>::RemoveAll(Type item)
{
	ListNode<Type> *pmove=head;
	ListNode<Type> *pdel=head->m_pnext;
	while(pdel!=NULL)
	{
		if(pdel->m_data==item)
		{
			pmove->m_pnext=pdel->m_pnext;
			delete pdel;
			pdel=pmove->m_pnext;
			continue;
		}
		pmove=pmove->m_pnext;
		pdel=pdel->m_pnext;
	}
	return 1;
}

template<typename Type> 
Type SingleList<Type>::Remove(int n)
{
	if(n<0)
	{
		cout<<"can't find the element"<<endl;
		exit(1);
	}
	ListNode<Type> *pmove=head,*pdel;
	for(int i=0;i<n&&pmove->m_pnext;i++)
	{
		pmove=pmove->m_pnext;
	}
	if(pmove->m_pnext==NULL)
	{
		cout<<"can't find the element"<<endl;
		exit(1);
	}
	pdel=pmove->m_pnext;
	pmove->m_pnext=pdel->m_pnext;
	Type temp=pdel->m_data;
	delete pdel;
	return temp;
}

template<typename Type> 
Type SingleList<Type>::Get(int n)
{
	if(n<0)
	{
		cout<<"The n is out of boundary"<<endl;
		exit(1);
	}
	ListNode<Type> *pmove=head->m_pnext;
	for(int i=0;i<n;i++)
	{
		pmove=pmove->m_pnext;
		if(NULL==pmove)
		{
			cout<<"The n is out of boundary"<<endl;
			exit(1);
		}
	}
	return pmove->m_data;
}

template<typename Type> 
void SingleList<Type>::Print()
{
	ListNode<Type> *pmove=head->m_pnext;
	cout<<"head";
	while(pmove)
	{
		cout<<"--->"<<pmove->m_data;
		pmove=pmove->m_pnext;
	}
	cout<<"--->over"<<endl<<endl<<endl;
}

main.c

#include <iostream>
using namespace std;

#include "SingleList.h"


int main()
{
	SingleList<int> list;
	for(int i=0;i<20;i++){
		list.Insert(i*3,i);
	}
	for(int j=0;j<5;j++){
		list.Insert(3,j*3);
	}
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();

	list.Remove(5);
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();

	list.RemoveAll(3);
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();
	cout<<"The third element is "<<list.Get(3)<<endl;
	list.Find(100);
	list.MakeEmpty();
	cout<<"the Length of the list is "<<list.Length()<<endl;
	list.Print();

	return 0;
}

运行效果
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C君莫笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值