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;

    }

  • 相关阅读:
    非常实用的php各种文件操作函数
    两个自用的Dota2 自走棋辅助工具:阵容模拟器与UI Mod插件
    Scratch 数字游戏
    初识Scratch 3.0
    何时重头来
    cocos2d-x 3.0 Armature jsb 初体验
    cocosbuilder中的Callbacks和sound effects
    cocos2dx js文件加密为jsc文件
    cocos2dx jsb 在IOS与安卓下的一些不同之处
    安卓打包记录
  • 原文地址:https://www.cnblogs.com/zhangsf/p/2749131.html
Copyright © 2011-2022 走看看