c++ 链表

来源:百度知道 编辑:UC知道 时间:2024/06/29 03:09:49
#include "stdafx.h"
#include "conio.h"
#include "stdlib.h"

#define MAX_LEN 20

struct _text
{
char text_[MAX_LEN];
struct _text* pre;
struct _text* nex;
};

typedef struct _text text;

int initialize(text*, text*, text*);
void get_text(text*);

int _tmain(int argc, _TCHAR* argv[])
{
text *Pre = NULL;
text *Next = NULL;
text *New = NULL;
text *Current = NULL;
text *Head = NULL;
char* x;

if( initialize(New,Head,Current) )
{
puts("succed");
x = Current->text_;
get_text(Current);
puts(x);
}
else
{
puts("fail");
exit(1);
}

system("pause");
return 0;
}

int initialize(text* New, text* Head, text* Cur

重点在于指针作为函数参数时,不会改变指针的地址,
修改如下:

int initialize(text* New, text* Head, text* Current)
{
if( New = (text*)calloc(1,sizeof(text)) )
{
Head = New;
Current = New;
New->nex = NULL;
New->pre = NULL;
return 1;
}
else
return 0;
};

〉〉
int initialize(text **New, text **Head, text **Current)
{
if( *New = (text*)calloc(1,sizeof(text)) )
{
*Head = *New;
*Current = *New;
*New->nex = NULL;
*New->pre = NULL;
return 1;
}
else
return 0;
};

调用的时候:
if( initialize(New,Head,Current) )
>>
if( initialize(&New,&Head,&Current) )