LinkedList子类

LinkedList子类是基于链表形式实现的List接口标准。该类除了实现了List接口外,也实现了Deque接口(双端队列)。
在这里插入图片描述

范例:使用LinkedList子类实现集合操作

package com.lxh.eighteenchapter;

import java.util.LinkedList;
import java.util.List;

public class JavaCollectionDemo484 {
       public static void main(String[] args) {
		List<String> all=new LinkedList<String>();
		all.add("jdwfjhn");
		all.add("dsafw");
		System.out.println(all);
	}
}

执行结果

[jdwfjhn, dsafw]

本关共有 8 个文件。其中 List.h 定义了 List 类及其成员函数。ListOp.h 和 ListOp.cpp 定义了与 List 相关的运算符重载。ArrayList.h 和 ArrayList.cpp 定义了 ArrayList 类及其成员函数。LinkedList.h 和 LinkedList.cpp 定义了 LinkedList 类及其成员函数。main.cpp 定义了主函数。 用户的任务是编写 List.h、ArrayList.cpp 和 LinkedList.cpp 文件,以使得 main.cpp 正确运行。 List.h 内容如下: #ifndef _LIST_H_ #define _LIST_H_ #include <iostream> using std::ostream; class List{ protected: int size; public: //兼具默认构造函数和功能构造函数 List(int s=0):size(s){} //拷贝构造函数 List(const List&rhs):size(rhs.size){} /*以下为虚函数*/ //作为继承的基类的析构函数一定要是虚的 virtual ~List(){} //普通的非虚函数 int getSize()const{return size;} /*以下为纯虚函数,即没有实现*/ virtual void insert(int pos,int value)=0; virtual void remove(int pos)=0; virtual int at(int pos)const=0; virtual void modify(int pos,int newValue)=0; /*你的工作在这里:此处设计disp函数*/ }; #endif // _LIST_H_ ListOp.h 内容如下: #include "List.h" //流输出运算符重载声明 ostream operator << (ostream& os,const List& rhs); ListOp.cpp 内容如下: #include "List.h" #include <iostream> using std::ostream; ostream& operator << (ostream&os, const List&rhs){ rhs.disp(os); return os; } ArrayList.h 的内容如下: #ifndef _ARRAYLIST_H_ #define _ARRAYLIST_H_ #include "List.h" class ArrayList : public List{ private: int *data; //真正保存数据的地方 int capacity;//容量 public: //默认构造函数,构造一个逻辑为空的顺序表 ArrayList(); //拷贝构造函数,构造一个逻辑上与参数内容相同的顺序表 ArrayList(const ArrayList&rhs); //原生数组构造函数,构造一个内容与给定数组相同的顺序表 ArrayList(int const a[],int n); //填充构造函数,构造一个内容为n个value的顺序表 ArrayList(int n,int value); //析构函数,一定要自行实现,否则有内存泄漏 ~ArrayList(); //子类当中须覆盖并实现父类中的纯虚函数 void insert(int pos,int value); void remove(int pos); int at(int pos)const; void modify(int pos,int newValue); //对于父类中已实现的虚函数,可以选择覆盖或者不覆盖 //void disp(ostream&os)const;//这个函数可以直接使用父类中的实现 private: void setCapacity(int newCapa); }; #endif // _ARRAYLIST_H_ LinkedList.h 内容如下: #ifndef _LINKEDLIST_H_ #define _LINKEDLIST_H_ #include "List.h" class LinkedList : public List{ public: //这是单链表节点的结构体 struct Node{ int data; Node *next; Node(int a=0,Node *b=nullptr):data(a),next(b){} }; private: Node *head;//链表的头结点 public: //默认构造函数,构造一个逻辑为空的链表 LinkedList(); //拷贝构造函数,构造一个逻辑上与参数内容相同的链表 LinkedList(const LinkedList&rhs); //原生数组构造函数,构造一个内容与给定数组相同的链表 LinkedList(int const a[],int n); //填充构造函数,构造一个内容为n个value的链表 LinkedList(int n,int value); //析构函数,一定要自行实现,否则有内存泄漏 ~LinkedList(); //子类当中须覆盖并实现父类中的纯虚函数 void insert(int pos,int value); void remove(int pos); int at(int pos)const; void modify(int pos,int newValue); //对于父类中已实现的虚函数,可以选择覆盖或者不覆盖 //对于LinkedList子类,必须重新实现disp函数 void disp(ostream&os)const; private: Node* advance(int pos)const; }; #endif // _LINKEDLIST_H_ main.cpp 内容如下: #include "List.h" #include "ArrayList.h" #include "LinkedList.h" using namespace std; int A[1000]; int main(){ List *p = new ArrayList; int n; cin>>n; for(int i=0;i<n;++i){ cin>>A[i]; p->insert(p->getSize(),A[i]); } cout<<*p<<endl; for(int i=0;i<3&&p->getSize()!=0;++i){ p->remove(0); } cout<<*p<<endl; for(int i=0;i<p->getSize();i+=2){ p->modify(i,p->at(i)*10); } cout<<*p<<endl; delete p; p = new LinkedList; for(int i=0;i<n;++i){ p->insert(p->getSize(),A[i]); } cout<<*p<<endl; for(int i=0;i<3&&p->getSize()!=0;++i){ p->remove(0); } cout<<*p<<endl; for(int i=0;i<p->getSize();i+=2){ p->modify(i,p->at(i)*10); } cout<<*p<<endl; delete p; return 0; }
最新发布
05-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值