Title Description
Single-chain containing the first node using C ++, and then the single-chain two nodes exchange position.
Note that two nodes can not simply exchange data comprising, two nodes must be achieved by modifying the position of the pointer exchange
Reference swap function can be defined:
swap (int pa, int pb) // pa and pb represent the position number two nodes in a single list
swap (ListNode * p, ListNode * q) // p and q represents two node pointers pointing
Entry
First input line 1 has n represents n data followed by the input data n
Second input line to exchange positions of the two nodes
3, line input to the two nodes exchange position
Export
All of the first line of output data to create a single linked list, with a space between the data
Single linked data output line of the second execution of the first sub-swap operation between the data separated by spaces
Single linked data output execution third row 2nd exchange operation between the data separated by spaces
If it is found not valid input position, the output string error, you do not have a single linked list output
Sample input
Sample Output
prompt
Pay attention to use the list to achieve Oh!
#include<iostream> using namespace std; #define ok 0 #define error -1 class CNode { int data; CNode *next; public: CNode() { next=NULL; } CNode(int n,CNode *p) { data=n; next=p; } int getdata() { return data; } CNode *getnext() { return next; } void setnext(CNode *p) { next=p; } }; class CList { friend class CNode; CNode *head; int nodenumber; public: CList() { head=NULL; nodenumber=0; } ~CList() { for(int i=0;i<nodenumber;i++) { CNode *p=head; head=head->getnext(); delete p; } } void nodeplus() { nodenumber++; } void nodeminus() { nodenumber--; } void createTailList(int *num,int n) { CNode *tail; CNode *s; head=new CNode(); tail=head; for(int i=0;i<n;i++) { s=new CNode(num[i],NULL); tail->setnext(s); tail=s; nodeplus(); } } CNode *findreal(int i) { if(i<=0||i>nodenumber) { cout<<"error"<<endl; return NULL; } else return indexfind(i); } CNode *indexfind(int i) { if(i<0) { return NULL; } CNode *p=head; int k=1; while(k<=i&&p!=NULL) { p=p->getnext(); k++; } return p; } int Insert(int i,int num) { if(i<=0||i>nodenumber+1) { cout<<"error"<<endl; return error; } nodeplus(); CNode *p=indexfind(i-1); CNode *temp=new CNode(num,p->getnext()); p->setnext(temp); return ok; } int Delete(int i) { if(i<1||i>nodenumber) { cout<<"error"<<endl; return error; } nodeminus(); CNode *p=indexfind(i-1); CNode *temp=p->getnext(); p->setnext(temp->getnext()); delete temp; return ok; } void display() { CNode *p=head->getnext(); while(p!=NULL) { cout<<p->getdata()<<" "; p=p->getnext(); } cout<<endl; } int nodeswap(int a1,int b1) { if(a1<=0||a1>nodenumber||b1<=0||b1>nodenumber) { cout<<"error"<<endl; return error; } CNode *t1=indexfind(a1-1); CNode *t2=indexfind(b1-1); CNode *a=indexfind(a1); CNode *b=indexfind(b1); CNode *temp=b->getnext(); t1->setnext(b); b->setnext(a->getnext()); t2->setnext(a); a->setnext(temp); return ok; } }; int main() { int n; cin>>n; int *num=new int[n]; for(int i=0;i<n;i++) { cin>>num[i]; } CList L; L.createTailList(num,n); L.display(); int a,b; cin>>a>>b; if(L.nodeswap(a,b)!=error) L.display(); cin>>a>>b; if(L.nodeswap(a,b)!=error) L.display(); delete []num; return 0; }