zoukankan      html  css  js  c++  java
  • 数据结构:链表的创建和打印

    #include "stdafx.h"
    struct node {
        int num;
        struct node *next;//指向下一节点
    };
    //创建链表
    struct node * creat() {
        struct node *head, *temp, *newp;//分别表示头节点、中间节点、新节点
        int n;//节点数据
        head = temp = NULL;//头节点和中间节点都指向NULL(初始化)
        printf("Input Number:");
        scanf("%d", &n);//输入节点数据
        while (n > 0) {//当数据大于0
            newp = (struct node *)malloc(sizeof(struct node));//新节点向系统分配内存,(struct node *)做数据类型转换(void可以转换为任意类型的数据类型)
            newp->num = n;//跟新节点数据
            newp->next = NULL;//新节点为最后一个节点,指向NULL
            if (head == NULL) {//如果为空链表
                head = temp = newp;//首节点和中间节点都指向新节点
            }
            else {//如果不为空链表
                temp->next = newp;//连接temp和newp
                temp = newp;//新节点变为中间节点,为了增加新的节点
            }
            printf("Input Number:");
            scanf("%d", &n);
        }
        return head;
    }
    //打印链表
    void show_node(struct node *head) {
        struct node *temp = head;
        while (temp != NULL) {//如果指向不为NULL
            printf("%d", temp->num);
            temp = temp->next;//指向下一个
        }
    }
    int main() {
        struct node *head;
        head = creat();
        show_node(head);
        return 0;
    }
    
    
  • 相关阅读:
    rpc
    route
    ROLLBACK
    rmdir -删除空目录
    rm
    rlogin
    REVOKE
    resolver
    RESET
    repquota
  • 原文地址:https://www.cnblogs.com/cnsec/p/13286832.html
Copyright © 2011-2022 走看看