心动C++情牵链表

本文介绍了一个简单的循环链表的数据结构实现,包括插入、删除等基本操作,并提供了完整的源代码示例。通过一个测试程序展示了如何使用该链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*--------------------------------------------------------------------------------
 * Project: TList.h
 * Name: zwp
 * Date: 2013.4
 *--------------------------------------------------------------------------------*/


#ifndef TLIST_H_
#define TLIST_H_

#include <iostream>

typedef int ListType; // 元素类型

class List
{// 循环链表
private:
	struct Node
	{
		ListType data;
		Node* next;

		Node(ListType value, Node* link = 0)
		{
			data = value;
			next = link;
		}
	};
	typedef Node* NodePointer;
	NodePointer head;

public:
	List();

	~List();

	List(const List& list);

	const List& operator=(const List& list);

	bool empty(void)const;

	void insert(ListType value);

	void deleted(void);

	friend std::ostream& operator<<(std::ostream& os, const List& list);

};


#endif




/*------------------------------------------------------------------------
 * Project: TList.cpp
 * Name: zwp
 * Date: 2013.4
 *-------------------------------------------------------------------------*/

#include <cassert>
#include "TList.h"


List::List()
	:head(0)
{ }

List::~List()
{
	if(!empty())
	{
		List::NodePointer ptr = head;
		List::NodePointer prev;

		while(ptr != 0)
		{
			prev = ptr->next;
			delete ptr;
			ptr = prev;
		}
	}	
}

bool List::empty(void)const
{
	return (head == 0);
}

List::List(const List& list)
{
	if(!empty())
	{
		head = new List::Node(list.head->data);

		List::NodePointer ptr = list.head->next;
		List::NodePointer prev = head;

		while(ptr != 0)
		{
			prev->next = new List::Node(ptr->data);
			ptr = ptr->next;
			prev = prev->next;
		}
	}
}

const List& List::operator=(const List& list)
{
	if(this != &list)
	{
		this->~List(); // 销毁之前的链表
		if(list.empty())
			head = 0;
		else
		{
			head = new List::Node(list.head->data);
			List::NodePointer ptr = list.head->next;
			List::NodePointer prev = head;

			while(ptr != 0)
			{
				prev->next = new List::Node(ptr->data);
				prev = prev->next;
				ptr = ptr->next;
			}
			
		}
	}
	return *this;
}


void List::insert(ListType value)
{
	List::NodePointer prev = head;
	List::NodePointer ptr;
	ptr = new List::Node(value);

	assert(ptr != 0);

	if(head == 0)
		head = ptr;
	else
	{
		while(prev->next != 0)
			prev = prev->next; // 找到链表末尾
		prev->next = ptr; // 将新的节点插入
	}
}

void List::deleted(void)
{
	if(empty())
		std::cerr <<"The List is empty..."<<std::endl;
	else
	{
		List::NodePointer ptr = head;
		head = head->next;
		delete ptr;
	}
}

std::ostream& operator<<(std::ostream& os, const List& list)
{
	//assert(!list.empty());

	List::NodePointer ptr = list.head->next;
	while(ptr != 0)
	{
		std::cout <<ptr->data<<"  ";
		ptr = ptr->next;
	}
	return os;
}

/*-------------------------------------------------------------------------------
 * Project: Test.cpp
 * Name: zwp
 * Date: 2013.4
 *-------------------------------------------------------------------------------*/


#include <iostream>
#include "TList.h"


int main(void)
{
	List list1;

	for(int index = 1; index < 10; index ++)
		list1.insert(index);
	
	list1.deleted();

	List list2(list1);
	std::cout <<list2<<std::endl;
	List list3 = list2;
	std::cout <<list3<<std::endl;
	std::cout <<list1<<std::endl;

	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值