zoukankan      html  css  js  c++  java
  • 链表的操作 C/C++

    一、创建链表,顺序增加数据,遍历链表操作

    /**
     *C语言实现,用了三个指针操作,没用构造函数
     */
    #include<iostream> 
    using namespace std;
    struct student {
        int data;
        struct student* next;
    };
    int main()
    {
        int data;
        struct student *head,*tail,*temp;
        head = NULL;
        while(cin>>data && data!=-1) {
            temp = (struct student*)malloc(sizeof(struct student));
            temp->data = data;
            temp->next = NULL;
            if(head==NULL) {
            head = temp;
                tail = head;
            } else { 
                tail->next = temp;
                tail = temp;        
            }         
        }
           
        tail = head;
        while(tail!=NULL) {
            cout<<tail->data<<endl;
            tail = tail->next;
        }
        system("pause");
        return 0;
    }
    /**
     *C/C++实现,用了三个指针操作,用了构造函数
     */
    #include<iostream> 
    using namespace std;
    struct student {
        int data;
        struct student* next;
        student(int a) {
            this->data = a;
            this->next = NULL; 
        }
    };
    int main()
    {
        int data;
        struct student *head,*tail,*temp;
        head = NULL;
        while(cin>>data && data!=-1) {       
            if(head==NULL) {
                head = new student(data);
                tail = head;
             } else {
                 temp = new student(data);
                 //temp用来接收新节点,这样tail的next不会丢失 
                 tail->next = temp;
                 tail = temp;              
             }         
        }    
           
        tail = head;
        while(tail!=NULL) {
            cout<<tail->data<<endl;
            tail = tail->next;
        }
        system("pause");
        return 0;
    }
    /**
     *C/C++实现,用了两个指针操作,用了构造函数
     */
    #include<iostream> 
    using namespace std;
    struct student {
        int data;
        struct student* next;
        student(int a) {
            this->data = a;
            this->next = NULL; 
        }
        student(int a, struct student* p) {
            p->next = this;
            this->data = a;
            this->next = NULL;
        }
    };
    int main()
    {
        int data;
        struct student *head,*tail;
        head = NULL;
        while(cin>>data && data!=-1) {           
            if(head==NULL) {
                head = new student(data);
                tail = head;             
             } else {
                  tail = new student(data, tail);
             }        
        }
           
        tail = head;
        while(tail != NULL) {
            cout<<tail->data<<endl;
            tail = tail->next;
        }
        system("pause");
        return 0;
    }

    二、(待续)

  • 相关阅读:
    使用Index()+Match()函数实现更为灵活的VLookUp()
    Hexo搭建博客笔记
    Jenkins自动化部署项目
    Ubuntu安装docker
    Ubuntu的简单使用
    ansible之Ad-Hoc
    redis的集群
    redis的主从复制和哨兵
    redis的持久化存储
    redis数据库基础
  • 原文地址:https://www.cnblogs.com/yanghuahui/p/2451076.html
Copyright © 2011-2022 走看看