您的位置:首页 > 其它

链表实现动态的数组开辟!

2009-12-18 00:28 211 查看
最近的iPhone开发遇到了一点小问题,鉴于我们的项目是要划线取路,对于存放路径的点的数组无法在开始时确定其长度,如果开辟过大会大大的浪费iPhone的内存,如果开辟过小,可能会发生意想不到的状况,而且为了使路径平滑,取点数又不能太少,这样就形成一个很大的矛盾,为此不得不采用链表是现动态的内存分配,同时用链表也为在路径中插入障碍物提供了更大的可能!因此我觉得这个想法还是不错的,但是鉴于oc的语法还不是很熟练,为此,一下先用C++进行表述,这周有时间翻译成oc语法!哈~~

#include <iostream>
using namespace std;
int main()
{
class WayPoint
{
public:
int point[10];
WayPoint *next;
};
WayPoint *ptr,*head,*delptr;
int i,j=0,num,k=0;
head=new WayPoint;
head->next=NULL;
ptr=head;
cout<<"enter numbers ending with -1:/n";
while (1)
{

for (i=0;i<10;i++)
{
cin>>num;
if (num==-1)
{
j=1;
break;
}
k++;
ptr->point[i]=num;
if (i==9)
{
delptr=new WayPoint;
delptr->next=NULL;
ptr->next=delptr;
ptr=delptr;
}

}
if (j==1)
{
break;
}
}
ptr=head;
//while (ptr->next!=NULL)
{
int numoflist,left,m;
numoflist=k/10;
left=k%10;
for(m=0;m<numoflist;m++)
{
for(i=0;i<10;i++)
cout<<ptr->point[i]<<" ";
delptr=ptr;
ptr=ptr->next;
delete delptr;
}
for (m=0;m<left;m++)
{
cout<<ptr->point[m]<<" ";
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐