// list.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "malloc.h"
/*************************结构体与链表************************/
/*************************2014年4月23日 00:51:55************************/
typedef struct node//创建链表要有一个指向下一个该结构体的指针-|
{ // |
int name; // |
node*next; //<<-------------------------------------------|
};
struct node*create()
{
node*p11=(node*)malloc(sizeof(node));
p11->next=NULL; //链表的第一个节点next必须为空,检测用于
return p11;
};
void add(node*head,node*n1)
{
node*head1=head;
while(head1->next!=NULL)//遍历的方式检测每个节点的next是不是为空,为空说明是最后一个节点。
{
head1=head1->next;
}
head1->next=n1;//给最后一个链表的next添加新的链表(即n1)
}
void print(node*head)
{
node*head1=head;
while(head1->next!=NULL)
{
head1=head1->next;
printf("%d\n",head1->name);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
node*HEAD=create();
int arr[]={1,2,3};
for(int i=0;i<3;i++)
{
node*n1=(node*)malloc(sizeof(node));
n1->name=arr[i];
n1->next=NULL;
add(HEAD,n1);
}
print(HEAD);
return 0;
}
|