السلام عليكم
هل يوجد طريقة أخف تعقيدا ً أو ذات فكرة برمجية أخرى غير هذه في عملية الـSWAP في القوائم المتصلة Linked List
#include "stdafx.h"
#include<iostream>
using namespace std;
struct node
{
int item;
node *next;
};
void main()
{
node *List=new node;
node *header=List;
int length;
cin>>length;
for(int i=0;i<length;++i)
{
List->item=i;
List->next=new node;
List=List->next;
}
List->next=NULL;
List=header;
// PRint
List=header;
while(List->next!=NULL)
{
cout<<List->item<<endl;
List=List->next;
}
cout<<"\n==================================\n";
// SWAP 1 & 4
List=header;
node *tempA1=new node;
node *tempA2=new node;
int tempV1;
int tempV2;
for(int i=0;i<length;++i)
{
if(i==1)
{
tempV1=List->item;
tempA1=List;
}
if(i==4)
{
tempA2=List;
tempV2=List->item;
}
List=List->next;
}
List=tempA1;
List->item=tempV2;
List=tempA2;
List->item=tempV1;
// PRint
List=header;
while(List->next!=NULL)
{
cout<<List->item<<endl;
List=List->next;
}
//pause
int ii;cin>>ii;
}بغض النظر عن طريقة الجمع و الطرح التي تخفف علينا استعمال المتحول temp
مع جزيل الشكر