C++学习之实现链表的增删查。

线性表的链表描述,废话少说,上代码。

节点头文件 ChainNode.h:

#ifndef CHAIN_NODE_H
#define	CHAIN_NODE_H
#include "ChainList.h"
template <typename T>
class ChainNode
{
	//friend ChainList<T>;
public:
	ChainNode();
	~ChainNode();
    template <typename T> friend class ChainList;
private:
	T data;
	ChainNode<T> * link;
};

template <typename T>
ChainNode<T>::ChainNode()
{
}

template <typename T>
ChainNode<T>::~ChainNode()
{
}

#endif

链表头文件:ChainList.h:

#ifndef CHAIN_LIST_H
#define CHAIN_LIST_H

#include <exception>
#include "ChainNode.h"
using namespace std;

template <typename T>
class ChainList
{
public:
	ChainList(){head = nullptr;};
	bool IsEmpty()const {return (0 == head)};
	int Length() const;
	int Search(const T& v) const;
	ChainList<T>& Delete(int k , T& v);
	ChainList<T>& Insert(int k , const T& v);
	void Output(ostream & out) const;
	~ChainList();
private:
	ChainNode<T> * head;
	bool Find(int k , T& v) const;
};

/*
*寻找某一位置的元素 
*将该位置的值传送给v
*如果不存在第K个元素 则返回false 否则返回true
*/
template <typename T>
bool ChainList<T>::Find(int k , T& v) const
{
	if( k < 1 ) return false;

	ChainNode<T> *current = head;
	int index = 1;
	while (current && index != k)
	{
		current = current->link;
		index ++;
	}
	if(current){
		//找到了
		v = current->data;
		return true;
	}
	return false;

}

/*返回链表的长度*/
template <typename T>
int ChainList<T>::Length() const
{ 
	ChainNode<T> *temp = head;
	int len = 0;
	while (temp)
	{
		temp = temp->link;
		len++;
	}
	return len;
}

/* 
* 寻找值v 
* 如果找到则返回v所在的位置 
* 否则返回0
*/
template <typename T>
int ChainList<T>::Search(const T&v) const
{
	ChainNode<T> *current = head;
	int index  = 1 ;
	while (current && current->data != v)
	{
		current = current->link;
		index ++;
	}
	if(current) return index;
	return 0;
}


/*
* 删除某一位置的node
* 将删除的元素值传递给v
*/
template <typename T>
ChainList<T>& ChainList<T>::Delete(int k , T& v)
{
	if(!head || k < 1) throw std::out_of_range("不存在第K个元素!");
	ChainNode<T>* current  = head;
	if(1 == k)
	{
		//删除第一个节点
		head  = head->link;
	}else
	{
		ChainNode<T>* DNode = head;
		//删除中间节点 先遍历到k-1个节点
		for (int index = 1; DNode && index < k - 1; index++)
		{
			DNode = DNode->link;
		}

		if (!DNode)
		{
			throw out_of_range("不存在第K个元素");
		}
		current = DNode->link;
		DNode->link = current->link; 		
	}
	v = current->data;
	delete current;
	return *this;
}

/*
* 添加节点
* 在第K个元素之后 添加值为V的节点
* 因为是添加节点 所以要动态创建
*/
template <typename T>
ChainList<T>& ChainList<T>::Insert(int k , const T& v)
{
	/**
	if(k < 0) throw out_of_range("k < 0 是不行的");

	ChainNode<T> * p = head;

	for(int index  = 1 ; index < k && p; index++ )
	{
		p = p->link;
	}
	if(k > 0 && !p) throw out_of_range(" 不存在第k 个元素");

	ChainNode<T> *y = new ChainNode<T>;

	y->data = v;
	if(k){
		y->link = p->link;
		p->link = y;
	}else
	{
		y->link = head;
		head = y;
	}

	return *this;
	**/
	ChainNode<T> *y;
	if(k < 0)
    {
        throw out_of_range("k < 0 是不行的");
    }
    else if( k == 0 )
    {
		ChainNode<T> *y = new ChainNode<T>;
		y->link = head;
		y->data = v;
        head = y;
    }
    else
    {
        ChainNode<T> * p = head;

		for(int index  = 1 ; index < k && p; index++ )
        {
            p = p->link;
        }
        if(k > 0 && !p) 
        {
            throw out_of_range(" 不存在第k 个元素");
        }
        else
        {

            y = new ChainNode<T>;

            y->data = v;
            y->link = p->link;
            p->link = y;
        }
    }

    return *this;
}

/*
 *将链表输出至输出流
 */
template<typename T>
void ChainList<T>::Output(ostream &out ) const
{
	ChainNode<T> *current = head;
	for(current = head;current;current = current->link)
	{
		out << current->data << " ";
	}
	out << endl;
}

/*
 * 重载<<
 */
template <class T>
ostream& operator << (ostream& out , const ChainNode<T>& x)
{
	x.Output(out);
	return out;
}

/*
 * 析构函数
 * 删除链表中所有节点
 */
template <typename T>
ChainList<T>::~ChainList()
{
	ChainNode<T> * next;
	while(head)
	{
		next = head->link;
		delete head;
		head = next;
	}
}
#endif

测试主函数 源.cpp:

#include <iostream>
#include <string>
#include "ChainList.h"

using namespace std;

int main()
{
	ChainList<char> myChain;

	string testData = "1234";

	for(int index = 0 ; index < testData.length(); index ++)
	{
		myChain.Insert(index , testData[index]);
	}
	myChain.Output(cout);
	char data = 'x';
    myChain.Delete(2 , data);
	myChain.Output(cout);
	cout << myChain.Search('3') << endl;
	system("pause");
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值