• C语言学习--链表


    #include "node.h"
    #include<stdio.h>
    #include<stdlib.h>
    //typedef struct _node {
    //    int value;
    //    struct _node *next;
    //} Node;
    
    int main(int agrc,char const *argv[])
    {
        int number;
        Node * head = NULL;        
        do {        
            scanf("%d",&number);
            if (number != -1) {
                //add to link list
                Node *p = (Node*)malloc(sizeof(Node));
                p->value = number;
                p->next = NULL;
                //finde the last
                Node *last = head;
                if (last) {
                    while (last->next) {
                    last = last->next;
                    }
                    last->next = p;
                } else {
                    head = p;
                }
            }        
        }while (number != -1);
        return 0;
    }

    加入LIST后

    #include "node.h"
    #include<stdio.h>
    #include<stdlib.h>
    //typedef struct _node {
    //    int value;
    //    struct _node *next;
    //} Node;
    typedef struct _list {
        Node* head;
    } List;
    
    void add(List* pList,int number);
    int main(int argc,char const *argv[])
    {
        int number;
        List list;
        list.head = NULL;
        do {
            scanf("%d",&number);
            add(&list,number);
        } while (number != -1);
        
        return 0;
    }
    
    void add(List* pList,int number)
    {
        while (number != -1) {
                //add to link-list
                Node *p = (Node*)malloc(sizeof(Node));
                p->value = number;
                p->next = NULL;                
            // find the last
            Node *last = pList->head;
            if (last) {        
                while (last->next) {
                    //attach
                    last = last->next;
                }
                last->next = p;
            } else {
                pList->head = p;
            }    
            }    
    }
  • 相关阅读:
    java正则表达式校验密码必须是包含大小写字母、数字、特殊符号的8位以上组合
    ActiveMQ入门
    枚举的使用
    SSM整合
    springmvc 狂神说的详细笔记
    狂神说Java Mybatis笔记
    Spring5入门基础知识
    Ajax的使用详解
    Filter 过滤器的使用详解
    mysql备份与恢复命令
  • 原文地址:https://www.cnblogs.com/netcn/p/4392043.html
Copyright © 2020-2023  润新知