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;

    }

  • 相关阅读:
    hdu 5833 Zhu and 772002 (高斯消元)
    1203事件对象
    作用域面试题
    1130 JS高级 面向对象
    1122JS中级复习
    1120浏览器对象模型 函数分析
    1119动画和复习
    1114面试题作用域
    1113Js操作CSS样式
    1112函数封装和元素的属性
  • 原文地址:https://www.cnblogs.com/zhangsf/p/2749131.html
Copyright © 2011-2022 走看看