假設現在有兩個LIST(圖片one two),我可以選擇要動哪一個list(選擇動哪張圖片移動到新的位置),如果沒動到的那張圖片,希望也要出一個空間,存的XY就是原本的位置。
此時希望可以有恢復的功能(linking list的返回鍵left),也希望可以下一步(linking list的right)
#include<iostream>
using namespace std;
struct list
{
list* left;
int x;
int y;
list* right;
};
int main()
{
list one;
list two;
cout<<"請輸入第1張圖的X和Y位置"<<endl;
cin>>one.x;
cin>>one.y;
one.left=NULL;
one.right=new list;
cout<<"請輸入第2張圖的X和Y位置"<<endl;
cin>>two.x;
cin>>two.y;
two.left=NULL;
two.right=new list;
list* pre;
list* current;
list* next;
int choice,count=0;
while(true)
{
cout<<"請輸入要更動哪一個圖片one or two"<<endl;
cin>>choice;
if(choice==1)
{
current=one.right; //initial 1
pre=&one;
for(int i=0;i<count;i++) //find new one
{
pre=current;
next=current->right;
current=next;
}
cout<<"請輸入第1張圖新的X和Y位置"<<endl;
cin>>current->x; //new
cin>>current->y;
current->left=pre;
current->right=new list;
current=two.right; //initial 2
pre=&two;
for(int j=0;j<count;j++) //find new two
{
pre=current;
next=current->right;
current=next;
}
current->left=pre;
current->right=new list;
current->x=current->left->x; //copy two data
current->y=current->left->y;
count++;
}
else if(choice==2)
{
current=two.right; //initial 2
pre=&two;
for(int j=0;j<count;j++) //find new two
{
pre=current;
next=current->right;
current=next;
}
cout<<"請輸入第2張圖新的X和Y位置"<<endl;
cin>>current->x; //new
cin>>current->y;
current->left=pre;
current->right=new list;
current=one.right; //initial 1
pre=&one;
for(int i=0;i<count;i++) //find new one
{
pre=current;
next=current->right;
current=next;
}
current->left=pre;
current->right=new list;
current->x=current->left->x; //copy one data
current->y=current->left->y;
count++;
}
else
break;
}
/*trace 使用
current=&one;
for(int k=0;k<=count;k++)
{
cout<<current->x<<" "<<current->y<<endl;
next=current->right;
current=next;
}
*/
return 0;
}
當然此程式可以看出choice==one or two 內有很多地方是做一樣的事情,應該可以額外寫個function去genrealize,但是由於懶惰,就不想優化了....
2013年6月26日 星期三
2013年6月24日 星期一
雙向linking list C++
先按1 在案2
#include<iostream>
using namespace std;
struct typeA
{
typeA *pre;
int x;
int y;
typeA *next;
};
int main()
{
typeA *left;
typeA data; //先弄第一個出來
data.pre=NULL;
data.x=10;
data.y=10;
data.next=new typeA;
left=&data; //讓left記路目前位置
typeA *right; //宣告一個right出來
right=data.next; //指到data的下一個
right->x=20;
right->y=20;
right->pre=left;
typeA *now;
now=&data;
while(true) //測試先按1 在案2
{ int n;
cin>>n;
if(n==1)
{
now=now->next;
cout<<now->x<<endl;
}
if(n==2)
{
now=now->pre;
cout<<now->x<<endl;
}
}
return 0;
}
2013年6月5日 星期三
NCNU LISP HW9
: finding the n-th leaf of a tree
(define (enumerate-tree tree)
(cond ((null? tree) nil)
((not (pair? tree)) (list tree))
(else (append (enumerate-tree (car tree))
(enumerate-tree (cdr tree))))))
(define nil '())
(define x (list 1 (list 2 (list 3 4)) 5))
(define (tree-ref L n)
(do-tree-ref L n 1)
)
(define (do-tree-ref L n flag)
(cond ((= flag 1) (do-tree-ref (enumerate-tree L) n 0))
((= n 0) (car L))
(else (do-tree-ref (cdr L) (- n 1) 0))
)
)
(define (enumerate-tree tree)
(cond ((null? tree) nil)
((not (pair? tree)) (list tree))
(else (append (enumerate-tree (car tree))
(enumerate-tree (cdr tree))))))
(define nil '())
(define x (list 1 (list 2 (list 3 4)) 5))
(define (tree-ref L n)
(do-tree-ref L n 1)
)
(define (do-tree-ref L n flag)
(cond ((= flag 1) (do-tree-ref (enumerate-tree L) n 0))
((= n 0) (car L))
(else (do-tree-ref (cdr L) (- n 1) 0))
)
)
訂閱:
文章 (Atom)