zoukankan      html  css  js  c++  java
  • 一个简单链表的C++实现

    /*  LList.cpp
    *   Author: Qiang Xiao
    *   Time: 2015-07-12
    */
    
    #include<iostream>
    using namespace std;
    
    class Node{
        public:
            int data;
        Node* ptr;
        Node(int elem= 0, Node* node= NULL){this->data= elem; this->ptr= NULL;}
    };
    
    class LList{
        private:
        Node* head;
        Node* tail;
        int length;
        public:
        LList();
        ~LList();
        bool append(Node*);
        bool insert(int, Node*);
        void print();
        int getLength(){return this->length;}
    };
    
    LList::LList(){
        Node* init= new Node(4);
        this->head= new Node();
        this->tail= new Node();
        this->head= init;
        this->tail= init;
        this->length= 0;
    }
    
    LList::~LList(){
        delete head;
        delete tail;
    }
    
    bool LList::insert(int pos, Node* node){
        int i= 0;
        Node* fence= new Node();
        fence= this->head;
        while(i< pos){
        fence= fence->ptr;
        i++;
        }
        node->ptr= fence->ptr;
        fence->ptr= node;
        this->length++;
        return true;
    }
    
    bool LList::append(Node* node){
        this->tail->ptr= node;
        this->tail= node;
        this->length++;
        return true;
    }
    
    void LList::print(){
        Node* p= this->head->ptr;
        while(p){
        cout<<p->data<<"	";
        p= p->ptr;
        }
        cout<<endl;
        delete p;
    }
    
    int main(){
        cout<<"
    ******************Begin Test**********************
    ";
        Node* node1= new Node(1);
        Node* node2= new Node(2);
        Node* node3= new Node(3);
        Node* node4= new Node(4);
        LList* list= new LList();
        cout<<"
    ******************Empty List**********************
    ";
        list->print();
        list->append(node1);
        list->append(node2);
        list->append(node3);
        list->append(node4);
        cout<<"
    ******************After Append********************
    ";
        list->print();
        cout<<"
    
    ";
        Node* node5= new Node(10);
        int pos= 2;
        list->insert(pos,node5);
        Node* node6= new Node(30);
        pos= 4;
        list->insert(pos,node6);
    
        cout<<"
    
    *****************After Insert*******************
    ";
        list->print();
        return 0;
    }

    Console display:

    xiaoq@xq-ubun:~/C/DataStructure$ g++ LList.cpp -o LList.o
    xiaoq@xq-ubun:~/C/DataStructure$ ./LList.o 
    
    ******************Begin Test**********************
    
    ******************Empty List**********************
    
    
    ******************After Append********************
    1    2    3    4    
    
    
    
    
    *****************After Insert*******************
    1    2    10    3    30    4    
    xiaoq@xq-ubun:~/C/DataStructure$ 

    写这个程序主要是练习一下链表的用法。代码中有许多需要改进的地方,敬请指正。

    欢迎交流!

  • 相关阅读:
    thinkphp 前后端分离
    git常用命令总结
    DIV常用属性大全
    shell编程学习之使用jq对json数据进行提取
    shell编程之if语句
    shell编程之变量赋值
    【总结】sqli-labs Less(1-35) 小结
    【总结】sqlmap常用命令
    【总结】kali(amd64)中安装nessus
    【总结】ettercap工具之DNS劫持
  • 原文地址:https://www.cnblogs.com/ruchicyan/p/4640665.html
Copyright © 2011-2022 走看看