您的位置:首页 > 理论基础 > 数据结构算法

[数据结构算法学习] 单向链表

2012-10-16 18:44 766 查看
c是链表
#include <stdio.h>#include <stdlib.h>struct _node{        int data;        struct _node *next;};typedef struct _node list;void main(){        int i, n, input_data;        list *h, *now_node, *next_node;        printf("Enter num n:");        scanf("%d", &n);        h = (list*) malloc(sizeof(list));        h->data = -1;        h->next = NULL;        for(i = 0; i<n; i++)        {                scanf("%d", &input_data);                if(i == 0)                {                        h->data = input_data;                        now_node = h;                }else{                        next_node = (list*) malloc(sizeof(list));                        next_node->data = input_data;                        now_node->next = next_node;                        now_node = next_node;                }        }        //打印          now_node = h;        while(now_node != NULL)        {                printf("%d",now_node->data);                now_node = now_node->next;        }        return 1;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: