链表的C++实现

  有的时候,处于内存中的数据并非连续的。那么这时候。我们就须要在数据结构中加入一个属性。这个属性会记录以下一个数据的地址。有了这个地址之后。全部的数据就像一条链子一样串起来了,那么这个地址属性就起到了穿线连结的作用。

链表有:单链表。双链表,单循环链表。双循环链表。

理解单链表,其它几种也就大同小异。

    相比較普通的线性结构,链表结构的优势是什么呢?我们能够总结一下:
    (1)单个节点创建很方便。普通的线性内存通常在创建的时候就须要设定数据的大小
    (2)节点的删除很方便,不须要像线性结构那样移动剩下的数据
    (3)节点的訪问方便,能够通过循环或者递归的方法訪问到随意数据,可是平均的訪问效率低于线性表

    那么在实际应用中,链表是怎么设计的呢?我们能够以int数据类型作为基础,设计一个简单的int链表:

#include <iostream>
#include <cstdlib>

 using namespace std;

struct list_node{
    int data;
    list_node *next;

};

class list{
public:
    list():head(NULL){};
    ~list();
    void print();
    void insert(int value);
/*you had better insert from the front list,that costs O(1),
*here i just insert into the taile, costing O(n)
*/
    void insert_back(int pos, int value);//inisert a node with value equals to value after the node with value equals to pos
    void delete_back(int pos);//delete the node after the node with value equals to pos
protected:
    list_node* find(int pos); //find the node with the value equals to pos

private:
    list_node* head; //point to the first node

};


list::~list()
{

}

list_node* list::find(int pos)
{
    if(NULL==head)
        return NULL;
    list_node* tmp=head;
    while((NULL!=tmp)&&(tmp->data!=pos))
        tmp=tmp->next;
    return tmp;

}

void list::insert(int value)
{
    if(head==NULL)
    {
        head = new list_node();
        head->data=value;
        head->next=NULL;

    }
    else
    {
        list_node *tmp= head;
        while(tmp->next!=NULL)
            tmp=tmp->next;
        list_node *node = new list_node();
        node->data=value;
        node->next=NULL;
        tmp->next=node;
    }
}

void list::insert_back(int pos, int value)
{
    list_node *tmp = find(pos);
    if(NULL==tmp)
        return;
    else
    {
        list_node *node = new list_node();
        node->data=value;
        node->next=tmp->next;
        tmp->next=node;
    }

}

void list::delete_back(int pos)
{
    list_node *tmp = find(pos);
    if(NULL==tmp)
    {
        cout<<"the value of pos isnot exist"<<endl;
        return;
    }
    else
    {
        if(tmp->next==NULL)
        {
            cout<<"pos is the last node"<<endl;
            return;
        }
        else
        {
            tmp->next=tmp->next->next;
        }
    }
}

void list::print()
{
    list_node *tmp = head;
    while(tmp!=NULL)
    {
        cout<<tmp->data<<" ";
        tmp=tmp->next;
    }
    cout<<endl;
}

int main()
{
    list myList;
    for(int i=1;i<10;++i)
    {
        myList.insert(i);
    }
    myList.print();
    myList.insert(11);
    myList.print();
    myList.insert_back(5, 555);
    myList.print();
    myList.delete_back(6);
    myList.print();
}

执行结果:


转载于:https://www.cnblogs.com/ldxsuanfa/p/10596433.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值