用C++编写单向链表

本文档介绍了如何使用C++编程语言实现单向链表,包括链表的头文件list.h和源文件list.c中的关键代码实现。
摘要由CSDN通过智能技术生成

list.h

#ifndef LIST_H
#define LIST_H

#include <iostream>
#include <cstdio>

#define TYPE int 

using namespace std;

class Node
{
public:
	TYPE data;
	Node* next;

	Node(TYPE _data)
	{
		data = _data;
		next = NULL;
	}
};

class List
{
public:
	Node* head;
	size_t size;
	List(void)
	{
		head = NULL;
		size = 0;
		cout << "我是链表" << endl;
	}

	~List(void);
	
	//头添加
	void head_add(TYPE data);
	//尾添加
	void tail_add(TYPE data);
	//头删除
	bool head_del(void);
	//尾删除
	bool tail_del(void);
	//插入
	bool insert(int index,TYPE data);
	//位置删除
	bool del_index(int index);
	//值删除
	bool del_value(TYPE data);
	//遍历
	void show_list(void);
	//排序
	void sort_list(void);
	//求链表长度
	size_t _size(void);

};


#endif//LIST_H

list.c

#include "list.h"

List::~List(void)
{
	for(int i=0;i<size;i++)
	{
		head_del();
	}
	cout << "数据清理完成" << endl;
	cout << "size=" << size << endl;	
}


//头添加
void List::head_add(TYPE data)
{
	Node* node = new Node(data);
	
	if (0 == size)
	{
		head = node;
	}
	else 
	{
		node->next = head;
		head = node;
	}
	size++;
}
//尾添加
void List::tail_add(TYPE data)
{
	Node* node = new Node(data);
	if (0 == size)
	{
		head = node;
	}
	else
	{
		Node* prev = head;
		while(prev->next !=NULL)
		{
			prev = prev->next;
		}
		prev->next = node;
	}
	size++;
}
//头删除
bool List::head_del(void)
{
	if (0 == size) return false;
	else if (1 == size) 
	{
		head = NULL;
	}
	else 
	{
		Node* prev =head;
		head = prev->next;
		delete prev;
	}
	size--;
	return true;
}
//尾删除
bool List::tail_del(void)
{
	if (0 == size) return false;
	else if (1 == size)
	{
		head = NULL;
	}
	else 
	{
		Node* prev = head;
		while(prev->next->next != NULL)
		{
			prev = prev->next;
		}
		Node* next = prev->next;
		prev->next = NULL;
		delete next;
	}
	size--;
	return true;
}
//插入
bool List::insert(int index,TYPE data)
{
	if (index > size) return false;
	else if (1 == index)
	{
		head_add(data);
	}
	else
	{
		Node* node = new Node(data);
		Node* prev = head;
		for(int i=1;i<index-1;i++)
		{
			prev = prev->next;
		}
		node->next = prev->next;
		prev->next = node;
	}
	size++;
	return true;
}
//位置删除
bool List::del_index(int index)
{
	if (index > size) return false;
	else if (1 == index)
	{
		head_del();
	}
	else 
	{
		Node* prev = head;
		for(int i=1;i<index-1;i++)
		{
			prev = prev->next;
		}
		Node* next = prev->next;
		prev->next = next->next;
		delete next;
	}
	size--;
	return true;
}
//值删除
bool List::del_value(TYPE data)
{
	Node* prev = head;
	if (data == head->data)
	{
		head_del();
		return true;
	}
	while(prev->next != NULL)
	{
		if (data == prev->next->data)
		{
			Node* next = prev->next;
			prev->next = next->next;
			delete next;
			size--;
			return true;
		}
		prev = prev->next;
	}
	return false;
}
//遍历
void List::show_list(void)
{
	Node* prev = head;
	while(prev != NULL)
	{
		cout << prev->data << endl;
		prev = prev->next;
	}
	cout << "------------------" << endl;
}
//排序
void List::sort_list(void)
{

	for(Node* prev =head;NULL != prev->next;prev = prev->next)
	{
		for(Node* next =prev->next;NULL!=next;next = next->next)
		{
			if (prev->data > next->data)
			{
				TYPE temp = prev->data;
				prev->data =next->data;
				next->data =temp; 
			}
		}
	}
}
//求链表长度
size_t List::_size(void)
{
	cout << "size=" << size << endl;
}```

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值