c语言结构程序设计举例,c语言结构体 链表 应用的例子

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值