用C++和Java實現帶頭節點的雙向循環鏈表,要繼承linearList類,並實現它的所有功能,另外,必須實現雙向迭代器。
實現帶頭節點的雙向循環鏈表,要具有以下的功能:
- 判斷表是否爲空,如果爲空則返回true,不空返回false.
- 給出表中數據元素的個數。
- 給定一個索引(位置),返回指向該位置的數據元素的指針,如果給定的位置號不合法,則返回空指針。
- 給定數據元素x,如果表中有該元素,則返回x第一次出現的索引,若x 不存在,則返回-1.
- 刪除給定索引的數據元素。
- 給定索引index ,數據元素x,將x插入到index的位置。
完整資源請去https://download.csdn.net/download/qq_38971487/10903512
C++:設計一個結構體struct chainNode,再設計一個具體類 class doubleChain 作爲抽象類 class linearList的派生類實現類linearList中的所有方法,外加實現雙向迭代器;
C++:
linearList.h:
#ifndef SRC_LINEARLIST_H_
#define SRC_LINEARLIST_H_
#include <iostream>
#include <iterator>
#include <algorithm>
template <typename T>
class linearList {
public:
virtual ~linearList(){};//需要定義
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& get(int theIndex) const = 0;
virtual int indexOf(const T& theElement) const = 0;
virtual void erase(int theIndex) = 0;
virtual void insert(int theIndex, const T& theElement) = 0;
virtual void output(std::ostream& out) const = 0;
};
#endif /* SRC_LINEARLIST_H_ */
doubleChain.h:
#ifndef DOUBLECHAIN_H_
#define DOUBLECHAIN_H_
#include<iostream>
#include<sstream>
#include<string>
#include<iterator>
#include"linearList.h"
template<typename T>
struct chainNode
{
T element;
chainNode<T>* next;//可以將chainNode<T>簡寫爲chainNode,因爲這是在class chainNode的作用域內
chainNode<T>* prev;
chainNode(){next = NULL;prev = NULL;}
chainNode(const T& element){
this->element = element;
next = NULL;
prev = NULL;
}
chainNode(const T& element,chainNode* next,chainNode* prev)//可以將chainNode<T>簡寫爲chainNode,因爲這是在class chainNode的作用域內
{
this->element = element;
this->next = next;
this->prev = prev;
}
};
template<class T>
class doubleChain : public linearList<T>{
public:
doubleChain(int initialCapacity = 10);
~doubleChain();
bool empty()const{return listSize == 0;}
int size() const{return listSize;}
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output(std::ostream& out) const;
public:
void checkIndex(int theIndex) const;
chainNode<T>* headNode;
int listSize;
class iterator;
iterator begin() const{
return iterator(headNode->next);
}
iterator end() const{
return iterator(headNode);
}
class iterator : public std::iterator<std::bidirectional_iterator_tag,T>{//一個類,提供返回類型的iterator_category表示的雙向迭代器函數
public:
iterator(chainNode<T>* theNode = NULL){
node = theNode;
}
T& operator*() const{
return node->element;
}
T* operator->()const{
return &node->element;
}
bool operator == (const iterator right) const{
return node == right.node;
}
iterator& operator++(){
node = node->next;
return *this;
}
iterator operator++(int){
iterator old = *this;
node = node->next;
return old;
}
protected:
chainNode<T>* node;
};
};
#endif /* DOUBLECHAIN_H_ */
doubleChain.cpp:
#include"doubleChain.h"
template<class T>
doubleChain<T>::doubleChain(int initialCapacity)//建立一個空表
{
if (initialCapacity < 1)
throw std::invalid_argument("Initial capacity = " + std::to_string(initialCapacity) + " Must be > 0");
headNode = new chainNode<T>();
headNode->next = headNode;
headNode->prev = headNode;
listSize = 0;
}
template<class T>
doubleChain<T>::~doubleChain()//析構函數
{
chainNode<T>* currentNode = headNode->next;
while(currentNode != headNode)//循環刪除所有節點
{
chainNode<T>* nextNode = currentNode->next;
delete currentNode;
currentNode = nextNode;
}
delete headNode;//刪除頭結點
}
template<class T>
void doubleChain<T>::checkIndex(int theIndex) const//判斷索引是否有效
{
if(theIndex < 0 || theIndex > listSize)
{
throw std::out_of_range("index = "+ std::to_string(theIndex)+ "size = "+ std::to_string(listSize));
}
}
text.cpp:
#include"doubleChain.cpp"
#include<numeric>
using std::cout;
using std::endl;
int main(){
doubleChain<int> y(2);
cout<<"y的初始尺寸爲 "<<y.size()<<endl;
if(y.empty())
cout<<"y爲空"<<endl;
else
cout<<"y不爲空"<<endl;
y.insert(0,1);
y.insert(1,2);
y.insert(2,3);
y.insert(3,4);
y.insert(4,5);
y.insert(4,6);
cout<<"插入6後y應該爲 1 2 3 4 5 6"<<endl;
cout<<"y的尺寸爲"<<y.size()<<endl;
if(y.empty())
cout<<"y爲空"<<endl;
else
cout<<"y不爲空"<<endl;
cout<<y<<endl;
cout<<"元素4的索引爲:"<<y.indexOf(4)<<endl;
y.erase(0);
cout<<"刪除0號元素後應該爲 2 3 4 6 5:"<<endl;
cout<<y<<endl;
y.erase(3);
cout<<"刪除3號元素後應該爲 2 3 4 5:"<<endl;
cout<<y<<endl;
return 0;
}
Java:設計一個接口interface linearList<T>,設計一個類class doubleChainNode<T>相當於C++中的結構體,設計一個類class doubleChainjava<T>實現了了接口linearList<T>所有方法,以及實現迭代器接口Iterable<T>中的一個方法Iterator<T> iterator();
Java:
linearList.java:
public interface linearList<T> {
public boolean empty();
public int size();
public T get(int theIndex) throws Exception;
public int indexOf(T theElement);
public void erase(int theIndex) throws Exception;
public void insert(int theIndex,T theElement) throws Exception;
public void output();
}
doubleChainNode.java:
public class doubleChainNode<T> {
T element;
doubleChainNode<T> next;
doubleChainNode<T> prev;
public doubleChainNode() {next = prev = null;}
public doubleChainNode(T element) {
this.element = element;
next = prev = null;
}
public doubleChainNode(T element,doubleChainNode<T> prev,
doubleChainNode<T> next) {
this.element = element;
this.prev = prev;
this.next = next;
}
}
doubleChain.java:
import java.util.*;
public class doubleChain<T> implements linearList<T>,Iterator<T>{
public doubleChainNode<T> headNode;//指向鏈表的頭結點
public int listSize;//鏈表的元素個數
private void checkIndex(int theIndex) throws Exception{//判斷索引是否有效
if(theIndex<0 || theIndex >= listSize)//索引要在0和listSize之間
throw new Exception("index = "+theIndex+"size = "+listSize);//拋出異常
}
public doubleChain(int initialCapacity) throws Exception{//doubleChain的構造函數,創建一個空鏈表
if(initialCapacity < 1) {//定義時鏈表長度<1就拋出異常
throw new Exception("Initial capacity = "+initialCapacity+"Must be > 0 ");
}
headNode = new doubleChainNode<T>(null);//空的頭結點
headNode.next = headNode;//頭結點的next指向頭結點
headNode.prev = headNode;//頭結點的prev指向頭結點
listSize = 0;//鏈表內沒有元素
}
public Iterator<T> iterator(){//迭代器
class Myiterator implements Iterator<T>{
public doubleChainNode<T> currentNode = headNode;
public boolean hasNext() {//判斷currentNode是否等於頭節點
return currentNode.next != headNode;
}
public T next() {//返回element
if(currentNode.next != headNode) {
currentNode = currentNode.next;
return currentNode.element;
}
return null;
}
}
return new Myiterator();
}
public boolean hasNext() {
return false;
}
public T next() {
return null;
}
}
text.java:
import java.util.Iterator;
public class text {
public static void main(String[] args)throws Exception{
doubleChain<Integer> p = new doubleChain<Integer>(5);
p.insert(0,1);
p.insert(1,2);
p.insert(2,3);
p.insert(3,4);
p.insert(0,5);
if(p.empty())
System.out.println("y爲空");
else
System.out.println("y不爲空");
System.out.println("p的尺寸爲 "+p.size());
System.out.print("p是");
p.output();
System.out.print("\n 元素1的索引爲 "+p.indexOf(1));
System.out.println("\n 索引爲2的元素爲 "+p.get(2));
p.erase(3);
System.out.println("刪除3後p爲");
p.output();
p.insert(2, 6);
System.out.println("\n 2號位置上插入6後p爲");
p.output();
System.out.println("\n 迭代器列表是:");
for(Iterator<Integer> iter = p.iterator(); iter.hasNext();) {
Integer i = (Integer)iter.next();
System.out.println(i);
}
}
}