Code:
#include<iostream>
using namespace std;
struct linklist{
int val;
linklist* node;
} ;
linklist* list_init(){
linklist *address =new linklist;
address->node=NULL;
return (address);
}
int getNodeCount(linklist* address){
int count=0;
while(address->node!=NULL) {
count++;
address=address->node;
}
return count;
}
linklist* getNodeAddress(linklist* address, int pos=-1, bool read=false){
if(pos>=0)
for(int i = 0; i<pos;i++)
if(address->node!=NULL && !(read && address->node->node==NULL))
address=address->node;
else break;
else
while(address->node!=NULL && !(read && address->node->node==NULL)) address=address->node;
return address;
}
int getNodeValue(linklist* address, int pos=-1){
return getNodeAddress(address,pos,true)->val;
}
void setNodeValue(linklist* address,int value, int pos=-1){
int nCount=getNodeCount(address);
if(pos>nCount) throw 1;
address=getNodeAddress(address, pos);
if(address->node!=NULL) address->val=value;
else {
address->val=value;
address->node=new linklist;
address->node->node = NULL;
address->node->val = 666;
}
}
linklist* swapNodes(linklist* address, int node1, int node2){
linklist* n1=NULL,*n2=NULL,*p1=NULL,*p2=NULL,*tmp;
cout<<node1<<" "<<node2<<endl;
int count=getNodeCount(address);
if(node1 > count || node2 >count){cout<<"if"<<endl; return NULL;}
if(node1 > -1){ n1= getNodeAddress(address, node1);}
if(node2 > -1) n2= getNodeAddress(address, node2);
if(node1-1 >= 0) p1= getNodeAddress(address, node1-1);
else p1 = NULL;
if(node2-1 >= 0) p2= getNodeAddress(address, node2-1);
else p2= NULL;
if(p1!=NULL && p2!=NULL && (p1 == n2 || p2 == n1)) {
p1->node=n1->node;
tmp=n2->node;
n2->node=n1;
n1->node=tmp;
return address;
}
else if(p1==NULL && p2!=NULL && (p1 == n2 || p2 == n1)){
tmp=n2->node;
n2->node=address;
n1->node=tmp;
return n2;
}
else if(p1!=NULL && p2==NULL && (p1 == n2 || p2 == n1)){
tmp=n1->node;
n1->node=address;
n2->node=tmp;
return n1;
}
else{
tmp= n1->node;
n1->node=n2->node;
n2->node= tmp;
if(p1!=NULL && p2!= NULL){
tmp=p1->node;
p1->node=p2->node;
p2->node=tmp;
return address;
}
else if(p1==NULL) {cout<<"NULL"<<endl;tmp= p2->node; p2->node=address; return tmp;}
else if(p2==NULL) {cout<<"NULL"<<endl;tmp= p1->node; p1->node=address; return tmp;}
else {cout<<"else"<<endl; return NULL;}
}
}
int destroyAllNodes(linklist* address){
int count=0;
while(address->node!=NULL) {
count++;
linklist* nextNodeAddress=address->node;
delete[] address;
address=nextNodeAddress;
}
delete[] address;
return count;
}
int main(){
linklist *hList = list_init();
for(int i=0;i<10;i++) setNodeValue(hList,i);
int limit = getNodeCount(hList);
for(int i=0; i<limit;i++) cout<<getNodeAddress(hList,i)<<" -> "<<getNodeAddress(hList,i)->node<<" "<<getNodeValue(hList,i)<<endl;
for(int i=0; i<limit/2;i++){
int p =getNodeValue(hList,limit-i-1);
setNodeValue(hList,getNodeValue(hList,i),limit-i-1);
setNodeValue(hList,p,i);
}
limit = getNodeCount(hList);
cout<<"After reverse"<<endl;
for(int i=0; i<limit;i++) cout<<getNodeAddress(hList,i)<<" -> "<<getNodeAddress(hList,i)->node<<" "<<getNodeValue(hList,i)<<endl;
cout<<endl;
for(int i=0; i<limit/2;i++){
if((hList = swapNodes(hList,i,limit-i-1)) == NULL) break;
}
for(int i=0; i<limit;i++) cout<<getNodeAddress(hList,i)<<" -> "<<getNodeAddress(hList,i)->node<<" "<<getNodeValue(hList,i)<<endl;
cout<<endl;
hList = swapNodes(hList,0,1);
for(int i=0; i<limit;i++) cout<<getNodeAddress(hList,i)<<" -> "<<getNodeAddress(hList,i)->node<<" "<<getNodeValue(hList,i)<<endl;
cout<<endl;
cin.get();
return 0;
}