自己按着书上顺序表的那个写法改成链表,后面输出结果错得不是很理解..
是不是要重新复习一下指针再做?
代码如下:
#include
using namespace std;
#ifndef LIST_H
#define LIST_H
template
struct LinkNode
{
T data;
LinkNode < T > * link;
LinkNode( ){ link = NULL; }
LinkNode( T & item , LinkNode * ptr = NULL ){ data = item; link = ptr; }
};
template
class List
{
private:
LinkNode * pointer;
public:
List();
List(const T &x){pointer=new LinkNode(x);}
List(List& L);
LinkNode *getHead() {return pointer;}
bool remove(int position , T & x);
bool insert(int position , T & x);
void Union(List &seq1,List &seq2,List &seq3);
void Intersection(List &seq1,List &seq2,List &seq3);
void output();
LinkNode *search(T x);
LinkNode *Locate(int i);
int length();
bool getData(int position, T & x);
void setData(int position , T & x);
bool isEmpty();
void makeEmpty();
~List();
};
#include
using namespace std;
#ifndef LINKEDLIST_CPP
#define LINKEDLIST_CPP
template
List::List(List& L)
{
T value;
LinkNode*srcptr=L.getHead();
LinkNode*destptr=first=new LinkNode;
while(srcptr->link!=NULL)
{
value=srcptr->link->data;
destptr->link=new LinkNode(value);
destptr=destptr->link;
srcptr=srcptr->link;
}
}
template
List ::List()
{
pointer=new LinkNode;
}
template
List ::~List()
{
makeEmpty();
}
template
LinkNode *List::Locate(int i)
{
if(i
LinkNode *current=pointer;
int k=0;
while(current!=NULL && k
{
current = current->link;
k++;
}
return current;
}
template
bool List ::remove(int position , T & x)
{
LinkNode *current=Locate(position-1);
if(current == NULL||current->link == NULL) return false;
LinkNode *del = current->link;
current->link = del->link;
x=del->data;
delete del;
return true;
}
template
bool List ::insert(int position , T & x)
{
LinkNode *current=Locate(position);
if(current == NULL) return false;
LinkNode *NewNode=new LinkNode(x);
if(NewNode==NULL)
{
cerr<<"Wrong!"<
}
NewNode->link=current->link;
current->link=NewNode;
return true;
}
template
void List ::output()
{
LinkNode *current=pointer->link;
while(current!=NULL)
{
cout<data<<" ";
current=current->link;
}
cout<<endl;
}
template
LinkNode *List ::search(T x)
{
LinkNode *current=pointer->link;
while(current!=NULL)
{
if(current->data==x) break;
else current=current->link;
}
return current;
}
template
int List ::length()
{
LinkNode *p=pointer->link;
int count=0;
while(p!=NULL)
{
p=p->link;
count++;
}
return count;
}
template
bool List ::getData(int position,T & x)
{
if(position<=0) return NULL;
LinkNode *current=Locate(position);
if(current==NULL) return false;
else
{
x=current->data;
return true;
}
}
template
void List ::setData(int position , T & x)
{
if(position<=0) return ;
LinkNode *current=Locate(position);
if(current==NULL) return ;
else current->data=x;
}
template
bool List ::isEmpty()
{
return pointer->link==NULL?true:false;
}
template
void List ::makeEmpty()
{
LinkNode *q;
while(pointer->link!=NULL)
{
q=pointer->link;
pointer->link=q->link;
delete q;
}
}
template
void List ::Union(List &seq1,List &seq2,List &seq3)
{
seq3=seq1;
T x;
int q;
LinkNode *k;
for(int i=1;i<=seq2.length();i++)
{
seq2.getData(i,x);
k=seq1.search(x);
if(k == 0)
{
seq3.insert(i-1,x);
q++;
}
}
cout<<"union is"<<" ";
seq3.output();
}
template
void List ::Intersection(List &seq1,List &seq2,List &seq4)
{
T x;
LinkNode *p;
for(int o=1;o<=seq2.length();o++)
{
seq2.getData(o,x);
p=seq1.search(x);
if(p != 0)
{
seq4.insert(o,x);
}
}
cout<<"Intersection is"<<" ";
seq4.output();
}
void main()
{
List list1;
List list2;
List list3;
List list4;
while(1)
{
int i;
int x=0;
cin>>i;
if(i== -1) break;
list1.insert(x , i);
x++;
}
while(1)
{
int j;
int x=0;
cin>>j;
if(j== -1) break;
list2.insert(x , j);
x++;
}
list4.Intersection(list1,list2,list4);
list3.Union(list1,list2,list3);
list4.Intersection(list1,list2,list4);
list3.Union(list1,list2,list3);
}