C++封装链表 类

10 篇文章 0 订阅
5 篇文章 0 订阅

List.h

#ifndef LIST_H
#define LIST_H
#include<stdio.h>
#include<iostream>
#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;
	Node* tail;
	size_t _size;
	List(void)
	{
		head = NULL;
		tail = NULL;
		_size = 0;
	}
	~List(void);
	//头添加
	void head_add(TYPE data);
	//
	void tail_add(TYPE data);
	//
	bool head_del(void);
	//
	bool tail_del(void);
	size_t size(void);
	void show(void);
};

#endif//LIST_H

List.cpp

#include "list.h"

List::~List(void)
{
	cout << "我是List的析构函数" << endl;
}
//头添加
void List::head_add(TYPE data)
{
	Node* node = new Node(data);
	if(0 == _size)
	{
		head = node;
		tail = node;
	}
	else
	{
	node->next = head;
	head = node;
	}
	_size++;
}
//尾添加
void List::tail_add(TYPE data)
{
	Node* node = new Node(data);
	if(0 == _size)
	{
		head = node;
		tail = node;	
	}
	else
	{	
	tail->next = node;
	tail = node;
	}
	_size++;
}
//头删除
bool List::head_del(void)
{
	if(0>=_size) return false;
	Node*temp = head;
	head = temp->next;
	if(1 == _size)
	{
		tail = NULL;
	}
	delete temp;
	_size--;
	return true;
}
//尾删除
bool List::tail_del(void)
{
	Node* _tail = tail;
	if( 0 >= _size) return false;
	if(1 == _size)
	{
		head_del();
		return true;
	}
	
	else
	{
	Node* prev=head;
	while(prev->next!=_tail)
	{
		prev = prev->next;
	}
	tail = prev;
	prev->next = NULL;
	delete _tail;
	}
	_size--;
	return true;
}
//长度
size_t List::size(void)
{
	size_t size;
	size = _size;
	cout << "链表的长度是:"<< size << endl;
}
//遍历
void List::show(void)
{
	Node* node =head;
	for(head;head!=NULL;head = head->next)
	{
		cout << head->data << endl;
	}
}

main.pp

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

using namespace std;

int main()
{
	List list;
	list.head_add(1);
	list.head_add(2);
	list.head_add(3);
	list.tail_add(4);
	//list.head_del();
	list.tail_del();
	list.size();
	list.show();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【问题描述】 请设计一个链表,实现链表的初始化、插入、删除和打印操作。 节点的定义如下: typedef int elementType; typedef struct node { elementType data; node* next; } LList, *PList; 链表的定义及要求如下: class linkedList { public: linkedList();//构造函数 virtual ~linkedList();//析构函数,注意要链表中所有节点的资源 void insert( int value );//警告:必须初始化才能使用! bool initiate();//初始化单链表,使用new操作创建头结点。如果创建失败,则返回false,否则返回true bool isEmpty();//判断单链表是否为空 //删除单链表中第pos个元素结点,并将删除的节点的值存在value中。 //注意:如果链表为空、删除位置大于链表长度、以及删除位置为0的情况,需要终止删除并输出相应信息 bool remove( int pos, int& value ); void print();//顺序打印单链表,如果是单链表为空,则输出 Empty int Length();//返回单链表长度。如果是单链表为空,则返回-1 private: LList *head; int len; }; main函数: int main(int argc, char* argv[]) { linkedList L1; int n; int val; //初始化链表 if(!L1.initiate()) return 0; cin>>n;//输入链表中数据个数 for(int i=0; i<n; i++) //输入n个数,并插入链表 { cin>>val; L1.insert(val); } cout << "Origin Length:" << L1.Length() << endl;//输出链表长度 cout << "data:" ; L1.print();//打印链表 cin>>n;//输入需要删除的数据的位置 if (L1.remove(n,val)) { //删除位置n的数据,并将删除的数据值放在val中 cout<<"Delete the data at position("<<n<<"):"<<val<<endl; cout<< "New Length:" << L1.Length()<< endl;//输出链表长度 cout<< "data:" ; L1.print();//打印链表 } return 0; } 【输入形式】 输入包括3行。 第一行是链表元素个数n。第二行为n个元素的值。第三行为拟删除的数据位置。 【输出形式】 输出格式不固定。具体参见样例。 【样例输入1】 4 1 2 3 4 2 【样例输出1】 Origin Length:4 data:1 2 3 4 Delete the data at position(2):2 New Length:3 data:1 3 4 【样例输入2】 2 100 -1 3 【样例输出2】 Origin Length:2 data:100 -1 pos > len, failed 【样例输入3】 8 8 7 6 5 4 3 2 1 0 【样例输出3】 Origin Length:8 data:8 7 6 5 4 3 2 1 pos <= 0, failed
当我们在C++中实现链表时,通常不会将链表本身直接继承自节点。相反,我们会将链表作为一个独立的,而节点则是链表的一个私有内部。 下面是一个简单的示例,展示了如何在C++中实现一个链表,其中节点作为链表的私有内部: ```cpp #include <iostream> class LinkedList { private: class Node { public: int data; Node* next; Node(int value): data(value), next(nullptr) {} }; Node* head; public: LinkedList(): head(nullptr) {} void insert(int value) { Node* newNode = new Node(value); if (head == nullptr) { head = newNode; } else { Node* temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = newNode; } } void display() { Node* temp = head; while (temp != nullptr) { std::cout << temp->data << " "; temp = temp->next; } std::cout << std::endl; } }; int main() { LinkedList list; list.insert(1); list.insert(2); list.insert(3); list.display(); // 输出: 1 2 3 return 0; } ``` 在上述代码中,节点 `Node` 是链表 `LinkedList` 的私有内部链表具有一个头指针 `head`,默认初始化为 `nullptr`。链表中的 `insert` 函数用于向链表中插入新节点, `display` 函数用于遍历并输出链表中的所有节点。 通过将节点作为链表的私有内部,我们隐藏了节点的实现细节,并将其作为链表的一部分进行管理。这样可以更好地封装数据结构,提供更友好的接口和操作方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值