zoukankan      html  css  js  c++  java
  • 单向链表>创建节点

    #include <stdio.h>
    #include <iostream>
    #include <stdlib.h>

    using namespace std;

    struct Person{
        int age;
        struct Person *next;
    };
    Person *CrtTailNode(){
        Person *head ,*tmp;
        head = tmp = NULL;
        int n;
        cout<<"Node Number:"<<endl;
        cin>>n;
        for(int i = 0;i<n;i++){
        
            tmp = new Person;//(Person*)(malloc(sizeof(Person)));
            cout<<"Age:" <<endl;
            cin >>tmp->age;
            tmp->next = head;
            head  = tmp ;
        
        }
        return head;


    }

    Person *CrtHeadNode(){
        Person *head,*tail,*tmp;
        int n;
        cout<<"Node Number:"<<endl;
        cin>>n;
        head = tail = tmp = NULL;
        for(int i = 0;i<n;i++){
            tmp = new Person;//(Person*)(malloc(sizeof(Person)));
            cout<<"Age:" <<endl;
            cin >>tmp->age;
            if(head){
                while(tail->next!=NULL){
                    tail = tail->next;
                }
                tail->next = tmp;
            }else{
                head = tail = tmp;
            }
        
        }
        return head;


    }
    void show(Person *head){
        while(head !=NULL){
            cout<<head->age<<endl;
            head = head->next;
        }
    }
    int main(){
        Person *p;
    //    p = CrtHeadNode();
        p = CrtTailNode();
        show(p);
        return 0;

    }

  • 相关阅读:
    第一个Django demo
    内建函数
    Git积累
    区间dp
    【Spring】Spring AOP详解(转载)
    【git】git 常用命令(含删除文件)
    【idea】idea如何在maven工程中引入jar包
    【Java】Scanner类nextInt后使用nextLine无法读取输入
    【Java_Eclipse】Eclipse插件如何卸载?
    【MySQL】MySQL5.7等以上版本在Windows上的配置
  • 原文地址:https://www.cnblogs.com/zhangsf/p/2749131.html
Copyright © 2011-2022 走看看