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;
}

沒有留言:

張貼留言