简单链表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// NodeTest.cpp : 定义控制台应用程序的入口点。
//简单链表
 
#include "stdafx.h"
#include "stdlib.h"
struct node {
    int data;
    struct node *next;
};
int _tmain(int argc, _TCHAR* argv[])
{
    struct node *head, *p, *q, *t;
    int i, n, a;
    q = NULL;//先初始化,否则报错
    scanf_s("%d", &n);//输入输入几个数
    head = NULL;//头指针初始化为空
    for(i=1;i<=n;i++) {
        scanf_s("%d", &a);
        p = (struct node *)malloc(sizeof(struct node));
        p->data = a;//将数据存储到当前节点中
        p->next = NULL;//将当前节点的后继指针指向空
        if(head == NULL) {
            head = p;//如果是第一个节点,则将头指针指向当前节点
        } else {
            q->next = p;//如果不是第一个节点,则将上一个节点的后继指针指向当前节点
        }
        q = p;
 
    }
    t = head;
    while(t != NULL) {
        printf("%d ", t->data);
        t = t->next;
    }
    getchar();
    getchar();
    return 0;
}

  

posted on   fancing  阅读(212)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示