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;

    }

  • 相关阅读:
    postgresql强制删除数据库
    oracle ORA-31655
    oracle 删除表空间与用户
    Nginx 配置文件说明
    docker学习笔记---基本命令
    SSH的 Write failed: Broken pipe 问题
    nginx 修改文件上传大小限制
    "echo 0 /proc/sys/kernel/hung_task_timeout_secs" disable this message
    Centos8 配置静态IP
    Prometheus Node_exporter 详解
  • 原文地址:https://www.cnblogs.com/zhangsf/p/2749131.html
Copyright © 2011-2022 走看看