目前自己測試可以正常運作 , 可是卻一直吃RE
#include <iostream>
#include <stdlib.h>
using namespace std;
struct box{
int a;
struct box *next;
};
class Single_link_list{
private:
box *ptr;
box *head;
box *current;
box *prev;
box *modifynode;
public:
Single_link_list();
void insert_f(int n);
void move1_f(int x,int y);
void move2_f(int x ,int y);
void move3_f(int x ,int y);
void display_f();
void sum_f(int n,int re);
};
//建構函式
Single_link_list::Single_link_list()
{
head = new box;
head->next = NULL;
}
//成員函式
//建立串列
void Single_link_list::insert_f(int n)
{
prev = head;
for(int i=1;i<=n;i++){
//creat node
ptr = new box;
ptr->a = i;
//連接
prev->next = ptr;
ptr->next = NULL;
prev = ptr;
}
}
void Single_link_list::display_f()
{
if(head->next==NULL)
cout<<"There is nothing in the box\n";
else{
current = head->next;
while(current!=NULL)
{
cout<<current->a<<" ";
current = current->next;
}
}
}
void Single_link_list::move1_f( int x ,int y)
{
prev = head;
current = head->next;
int check=0;
//尋找位置
while(current!=NULL){
if(current->a==x){
++check;
if(current->next->a==y)return;
modifynode = current;//紀錄
prev->next = current->next;
if(check==2) break;
}
else if(current->a==y){
++check;
ptr = prev;//紀錄
if(check==2) break;
}
prev = current;
current = current->next;
}
modifynode->next =ptr->next;
ptr->next = modifynode;
}
void Single_link_list::move2_f( int x ,int y)
{
prev = head;
current = head->next;
int check=0;
//尋找位置
while(current!=NULL){
if(current->a==x){
++check;
if(prev->next->a==y)return;
modifynode = current;//紀錄
prev->next = current->next;
if(check==2) break;
}
else if(current->a==y){
++check;
ptr = current;//紀錄
if(check==2) break;
}
prev = current;
current = current->next;
}
modifynode->next = ptr->next;
ptr->next = modifynode;
}
void Single_link_list::move3_f( int x ,int y)
{
prev = head;
current = head->next;
//尋找位置
while((current!=NULL)&¤t->a!=x){
prev = current;
current = current->next;
}
modifynode =current;
prev = head;
current = head->next;
while((current!=NULL)&¤t->a!=y){
prev = current;
current = current->next;
}
modifynode->a =y;
current->a = x;
}
// 奇數位總和
void Single_link_list::sum_f(int n,int re)
{
long long int sum=0,cnt=0,check;
if(n%2!=0){
current = head->next;
check =(n+1)/2;
while(current!=NULL){
sum+=current->a;
++cnt;
if(cnt == check) break;
current=current->next->next;
}
}
else {
if(re%2!=0)
current = head->next->next;
else
current = head->next;
check =n/2;
while(current!=NULL){
sum+=current->a;
++cnt;
if(cnt == check) break;
current=current->next->next;
}
}
cout<<"sum = "<<sum<<"\n";
}
int main()
{
Single_link_list obj;
char option1;
int n,m,x,y,z,Case=0;
while(1){
Single_link_list obj;
int re = 0;
cin>>n>>m;
obj.insert_f(n);
for(int i=0;i<m;i++){
cin>>x;
if(x==4) {
++re;
continue;
}
cin>>y>>z;
if(x==1)
obj.move1_f(y,z);
else if(x==2)
obj.move2_f(y,z);
else
obj.move3_f(y,z);
}
cout<<"Case "<<++Case<<": ";
obj.sum_f(n,re);
}
}