#include
#include
#include
using namespace std;
typedef int T;
struct LinkNode //结点类
{
T data;
LinkNode link;
LinkNode(LinkNode ptr=NULL)
{
link=ptr;
}
LinkNode(const T&item,LinkNode* ptr=NULL)
{
data=item;
link=ptr;
}
};
class List:public LinkNode{
public:
LinkNode *first;//单链表类
List(){first=new LinkNode;}
List(const T&x){first=new LinkNode(x);}
void makeEmpty(List *&l)
{
LinkNode *q;
while(first->link==NULL)
{
q=first->link;
first->link=q->link;
delete q;
}
}
void ListInit(List &l){
first->link=NULL;
}
void createList(T endTag)
{
//endTag是输入序列结束的标志
LinkNode *newNode,*last;
first->link=NULL;
last=first;
while(1)
{
newNode=new LinkNode();
cin>>newNode->data;
if(newNode->data==endTag)
break;
last->link=newNode;
last=newNode;
}
last->link=NULL;
}
LinkNode *Locate(int i)
{
if(i<0)
return NULL;
LinkNode* current =first;
int k=0;
while(current!=NULL&&k<i)
{
current=current->link;
k++;
}
return current;
}
int locate(T &x)
{
LinkNode *current=first->link;
int i=0;
while(current!=NULL&¤t->data!=x)
{
current=current->link;
i++;
}
if(current==NULL)
return 0;
else
return i;
}
void Insert(int i,T& x)
{
LinkNode *current=Locate(i-1);
if(current==NULL)
cout<<"Error"<<endl;
LinkNode *newNode=new LinkNode(x);
if(newNode==NULL)
{
cout<<"Error!"<<endl;
exit(1);
}
newNode->link=current->link;
current->link=newNode;
}
void Search(T& x)
{
LinkNode *current=first->link;
int count=1;
int flag=0;
while(current!=NULL)
{
if(current->data==x)
{
flag=1;
break;
}
else
{
current=current->link;
}
count++;
}
if(flag==1)
cout<<" is located at index of "<<count<<endl;
if(flag==0)
cout<<" is not found"<<endl;
}
void deleteList(T &x)
{
LinkNode* del=first;
LinkNode *current=first->link;
while(current!=NULL)
{
if(current->data==x)
{
del->link=current->link;
current=current->link;
delete current;
}
else
{
del=current;
current=current->link;
}
}
}
void outputList()
{
LinkNode *current=first->link;
while(current!=NULL)
{
cout<data<<" ";
current=current->link;
}
}
};
```int main()
{
List list1,list2,list3;
int endTag;
scanf("%d",&endTag);
list1.createList(endTag);
cout<<"A is created as: ";
list1.outputList();
int i,x;
scanf("%d %d",&i,&x);
list1.Insert(i,x);
cout<<"After inserted A is ";
list1.outputList();
int y;
scanf("%d",&y);
list1.deleteList(y);
cout<<"After deleted A is ";
list1.outputList();
int m;
scanf("%d",&m);
cout<<m;
list1.Search(m);
int n;
scanf("%d",&n);
cout<<n;
list1.Search(n);
int endTag1;
scanf("%d",&endTag1);
list2.createList(endTag1);
cout<<"B is created as: ";
list2.outputList();
Intersection(list1,list2);
cout<<"A cross B is ";
list1.outputList();
}