构建一个大小已知的链表
2018-08-12 本文已影响0人
zjh3029
#include <iostream>
using namespace std;
struct node
{
int data;
int length;
node *pnext;
node(int inp,int inpl)
{
if (inpl<0)
{
return;
}
data = inp;
length = inpl;
pnext = new node(inp, inpl - 1);
}
};
int main()
{
node *poi1 = new node(1,10);
node *poi2 = poi1;//将poi1保存的地址给了poi2
for (int i = 0; i < 10; i++)
{
poi1->data = i*5;
poi1=poi1->pnext;
}
poi1->data = -1;
poi1->pnext = nullptr;
for (int i = 0; i < 8; i++)
{
cout << poi2->data << endl;
poi2 = poi2->pnext;
}
system("pause");
delete poi1;
return 0;
}