实验目的
掌握线性表结构、链式描述方法(链式存储结构)、链表的实现。
掌握链表迭代器的实现与应用。
二、实验内容
1、题目描述:
要求封装链表类,链表迭代器类;
链表类需提供操作:在指定位置插入元素,删除指定元素,搜索链表中是否有指定元素,原地逆置链表,输出链表;
不得使用与链表实现相关的STL。
输入输出格式:
输入:第一行两个整数 N 和 Q。
第二行 N 个整数,作为节点的元素值,创建链表。
接下来 Q 行,执行各个操作,具体格式如下:
插入操作 : 1 idx val,在链表的idx位置插入元素val;
删除操作 : 2 val,删除链表中的 val 元素。若链表中存在多个该元素,仅删除第一个。若该元素不存在,输出 -1;
逆置操作 : 3,原地逆置链表;
查询操作 : 4 val,查询链表中的val元素,并输出其索引。若链表中存在多个该元素,仅输出第一个的索引。若不存在该元素,输出 -1;
输出操作 : 5,使用链表迭代器,输出当前链表索引与元素的异或和。f(chain)=∑_(i=0)^(n-1)▒〖i⊕chain[i],n=len(chain)〗;
2、题目描述:
要求使用题目一中实现的链表类,迭代器类完成本题;
不得使用与题目实现相关的STL;
给定两组整数序列,你需要分别创建两个有序链表,使用链表迭代器实现链表的合并,并分别输出这三个有序链表的索引与元素的异或和。
注:给定序列是无序的,你需要首先得到一个有序的链表。
输入输出格式:
输入:
第一行两个整数 N 和 M;
第二行 N 个整数,代表第一组整数序列;
第三行 M 个整数,代表第二组整数序列。
输出:
三行整数。分别代表第一组数、第二组数对应的有序链表与合并后有序链表的索引与元素的异或和。
#include<iostream>
using namespace std;
template<class T>
struct Node{
T element;
Node<T> *next;
Node(){
}
Node(const T&element){
this->element=element;
}
Node(const T&element,Node<T>*next){
this->element=element;//新建的这个节点的元素==element(输入的)
this->next=next;//新建的节点的next=next,你输入的。
}
};
template<class T>
class CHList{
private:
int listsize;
Node<T>*firstnode;
public:
CHList(int space=10);//构造
CHList(const CHList<T>&);//复制构造
~CHList();//析构
void insert(int index,const T &theElement);
int Delete(const T &theElement);
void reverse();
int search(const T & theElement)const;
void output()const;
class CHIter;
CHIter begin() {
return CHIter(firstnode); }
CHIter end() {
return CHIter(NULL); }
class CHIter{
private:
Node<T>*node;
public:
CHIter(Node<T>*theNode=NULL){
node=theNode;}
CHIter begin() {
return CHIter(firstnode); }
CHIter end() {
return CHIter(NULL); }
//解引用操作符
T&operator*()const {
return node->element;}
T*operator->()const {
return &node->element;}
//加法
CHIter&operator++(){
//前加
node=node->next;
return *this;}
CHIter operator++(int){
//后加
CHIter old=*this;
node=node->next;
return old; }
//相等校验
bool operator!=(const CHIter right)const{
return node!=right.node;}
bool operator==(const CHIter right)const{
return node==right.node;}
};
};
template<class T>
CHList<T>::CHList(int space){
firstnode=NULL;
listsize=0;
}
template<class T>
CHList<T>::CHList(const CHList<T>&theList){
listsize=theList.listsize;
if(listsize==0){
firstnode=NULL;
return ;
}
CHList<T>*sourcenode=theList.firstnode;
firstnode=new Node<T>(sourcenode->element);//本列表第一个节点
//代表将这个元素付给这个节点结构,使之成为代有值的节点
sourcenode=sourcenode->next;
Node<T>*lastnode=firstnode;//本列表最后一个节点
while(sourcenode!=NULL){
lastnode->next=new Node<T>*(sourcenode->element);
lastnode=lastnode->next;
sourcenode=sourcenode->next;
}
lastnode->next=NULL;
}
template<class T>
CHList<T>::~CHList(){
while(firstnode!=NULL){
Node<T>*nextnode=firstnode->next;
delete firstnode;
firstnode=nextnode;
}
}
template<class T>
void CHList<T>::insert(int index,const T &theElement){
if(index==0){
firstnode=new Node<T>(theElement,firstnode);//插到头 更新第一个元素
}else{
Node<T>* insertnode=firstnode;//有元素
for(int i=0;i<index-1;i++){
insertnode=insertnode->next;
}
insertnode->next=new Node<T>(theElement,insertnode->next);//更新,实现插队
}
listsize++;
}
template<class T>
int CHList<T>::Delete(const T &theElement){
Node<T>* p=firstnode;
Node<T>* Deletenode=p;
if(Deletenode->element==theElement){
firstnode=firstnode->next;
listsize--;
delete Deletenode;
return 0;
}else{
while(p->next!=NULL){
Deletenode=p->next;
if(Deletenode->element==theElement){
p->next=p->next->next;
listsize--;
delete Deletenode;
return 0;
}
p=p->next;
}
return -1;
}
return -1;
}
template<class T>
void CHList<T>::reverse(){
Node<T>*headnode=new Node<T>(-1);
Node<T>*p,*r;
headnode->next=NULL;//头节点断开
p=firstnode;
r=p->next ;
while(p!=NULL){
p->next=headnode->next;//p断开
headnode->next=p;
p=r;
if(r!=NULL){
r=r->next;
}
firstnode=headnode->next;//首节点接上头节点
}
}
template<class T>
int CHList<T>::search(const T & theElement)const{
Node<T>* currentNode=firstnode;
int index=0;
while(currentNode!=NULL && currentNode->element!=theElement){
currentNode=currentNode->next;
index++;
}
if(currentNode==NULL)
return -1;
else
return index;
}
template<class T>
void CHList<T>::output()const{
int sum=0,i=0;
for(Node<T>*currentnode=firstnode;currentnode!=NULL;
currentnode=currentnode->next){
cout<<currentnode->element<<ends;
}
}
int main(){
int N,Q,x;cin>>N;cin>>Q;
CHList <int>chlist(N);
for(int i=0;i<N;i++){
cin>>x;
chlist.insert(i,x);
}
int sum,j;
int idx=0,val=0;
for(int i=0;i<Q;i++){
int n;cin>>n;
switch(n){
case 1:
cin>>idx>>val;
chlist.insert(idx,val);
break;
case 2:
cin>>val;
cout<<chlist.Delete(val)<<endl;
break;
case 3:
chlist.reverse();
break;
case 4:
cin>>val;
cout<<chlist.search(val)<<endl;
break;
case 5:
sum=0,j=0;//更新
for(CHList<int>::CHIter i=chlist.begin();i!=chlist.end();i++){
sum=sum+((*i)^j);
j++;
}
cout<<sum<<endl;
break;
default:
break;
}
}
return 0;
}
#include<iostream>
using namespace std;
template<class T>
struct Node{
T element;
Node<T> *next;
Node(){
}
Node(const T&element){
this->element=element;
}
Node(const T&element,Node<T>*next){
this->element=element;//新建的这个节点的元素==element(输入的)
this->next=next;//新建的节点的next=next,你输入的。
}
};
template<class T>
class CHList{
private:
int listsize;
Node<T>*firstnode;
public:
CHList(int space=10);//构造
CHList(const CHList<T>&);//复制构造
~CHList();//析构
void insert(int index,const T &theElement);
int Delete(const T &theElement);
void reverse();
int search(const T & theElement)const;
void output()const;
void merger(CHList &a, CHList &b);
void bubblesort();
class CHIter;
CHIter begin() {
return CHIter(firstnode); }
CHIter end() {
return CHIter(NULL); }
class CHIter{
private:
Node<T>*node;
public:
CHIter(Node<T>*theNode=NULL){
node=theNode;}
CHIter begin() {
return CHIter(firstnode); }
CHIter end() {
return CHIter(NULL); }
//解引用操作符
T&operator*()const {
return node->element;}
T*operator->()const {
return &node->element;}
//加法
CHIter&operator++(){
//前加
node=node->next;
return *this;}
CHIter operator++(int){
//后加
CHIter old=*this;
node=node->next;
return old; }
//相等校验
bool operator!=(const CHIter right)const{
return node!=right.node;}
bool operator==(const CHIter right)const{
return node==right.node;}
};
};
template<class T>
CHList<T>::CHList(int space){
firstnode=NULL;
listsize=0;
}
template<class T>
CHList<T>::CHList(const CHList<T>&theList){
listsize=theList.listsize;
if(listsize==0){
firstnode=NULL;
return ;
}
CHList<T>*sourcenode=theList.firstnode;
firstnode=new Node<T>(sourcenode->element);//本列表第一个节点
//代表将这个元素付给这个节点结构,使之成为代有值的节点
sourcenode=sourcenode->next;
Node<T>*lastnode=firstnode;//本列表最后一个节点
while(sourcenode!=NULL){
lastnode->next=new Node<T>*(sourcenode->element);
lastnode=lastnode->next;
sourcenode=sourcenode->next;
}
lastnode->next=NULL;
}
template<class T>
CHList<T>::~CHList(){
while(firstnode!=NULL){
Node<T>*nextnode=firstnode->next;
delete firstnode;
firstnode=nextnode;
}
}
template<class T>
void CHList<T>::insert(int index,const T &theElement){
if(index==0){
firstnode=new Node<T>(theElement,firstnode);//插到头 更新第一个元素
}else{
Node<T>* insertnode=firstnode;//有元素
for(int i=0;i<index-1;i++){
insertnode=insertnode->next;
}
insertnode->next=new Node<T>(theElement,insertnode->next);//更新,实现插队
}
listsize++;
}
template<class T>
int CHList<T>::Delete(const T &theElement){
Node<T>* p=firstnode;
Node<T>* Deletenode=p;
if(Deletenode->element==theElement){
firstnode=firstnode->next;
listsize--;
delete Deletenode;
return 0;
}else{
while(p->next!=NULL){
Deletenode=p->next;
if(Deletenode->element==theElement){
p->next=p->next->next;
listsize--;
delete Deletenode;
return 0;
}
p=p->next;
}
return -1;
}
return -1;
}
template<class T>
void CHList<T>::reverse(){
Node<T>*headnode=new Node<T>(-1);
Node<T>*p,*r;
headnode->next=NULL;//头节点断开
p=firstnode;
r=p->next ;
while(p!=NULL){
p->next=headnode->next;//p断开
headnode->next=p;
p=r;
if(r!=NULL){
r=r->next;
}
firstnode=headnode->next;//首节点接上头节点
}
}
template<class T>
int CHList<T>::search(const T & theElement)const{
Node<T>* currentNode=firstnode;
int index=0;
while(currentNode!=NULL && currentNode->element!=theElement){
currentNode=currentNode->next;
index++;
}
if(currentNode==NULL)
return -1;
else
return index;
}
template<class T>
void CHList<T>::output()const{
int sum=0,i=0;
for(Node<T>*currentnode=firstnode;currentnode!=NULL;
currentnode=currentnode->next){
cout<<currentnode->element<<ends;
}
}
template<class T>
void CHList<T>::merger(CHList &a, CHList &b){
CHList::CHIter l1 = a.begin();
CHList::CHIter l2 = b.begin();
for (int i = 0; i < b.listsize; i++)
{
insert(i, *l2);
l2++;
}
for (int i = 0; i < a.listsize; i++)
{
insert(i, *l1);
l1++;
}
listsize = a.listsize + b.listsize;
}
template<class T>
void CHList<T>::bubblesort(){
if (listsize<1){
return;
}
Node<T>*p = NULL;
Node<T>*q = NULL;
for(p=firstnode;p!=NULL;p=p->next){
for(q=p->next;q!=NULL;q=q->next){
if (p->element>q->element){
T temp=q->element;
q->element=p->element;
p->element=temp;
}
}
}
}
int main(){
int N,M,x;cin>>N;cin>>M;
CHList <int>chlist1(N);
CHList <int>chlist2(M);
for(int i=0;i<N;i++){
cin>>x;
chlist1.insert(i,x);
}
for(int i=0;i<M;i++){
cin>>x;
chlist2.insert(i,x);
}
chlist1.bubblesort();//排序
int sum=0,j=0;
for(CHList<int>::CHIter i=chlist1.begin();i!=chlist1.end();i++){
sum=sum+((*i)^j);
j++;
}
cout<<sum<<endl;
chlist2.bubblesort();//排序
sum=0,j=0;
for(CHList<int>::CHIter i=chlist2.begin();i!=chlist2.end();i++){
sum=sum+((*i)^j);
j++;
}
cout<<sum<<endl;
CHList<int>chlist3(N+M);
chlist3.merger(chlist1,chlist2);//合并
chlist3.bubblesort();//排序
sum=0,j=0;
for(CHList<int>::CHIter i=chlist3.begin();i!=chlist3.end();i++){
sum=sum+((*i)^j);
j++;
}
cout<<sum<<endl;
return 0;
}