zoukankan      html  css  js  c++  java
  • 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;
            }    
            }    
    }
  • 相关阅读:
    关于树状数组区间最值
    Gym 100500B
    RQNOJ Bus
    关于加权的LIS问题
    vs tip1
    小常识
    我的魔方主力
    killer驱动
    从日升的mecha anime看mecha genre的衰退
    关于供给移动端的视频压制
  • 原文地址:https://www.cnblogs.com/netcn/p/4392043.html
Copyright © 2011-2022 走看看