C++链表类

C++中的类就是C中结构体的化身,两者区别在于
C++中的成员默认为私有(private)
结构体中默认为(public)

#include<iostream>
using namespace std;
struct node//结构体默认公有
{
    node();
    node(int);
    int data;
    struct node* next;//单纯存地址,不指向任何空间
};
typedef struct node* pnode;
node::node():data(0),next(NULL){}
node::node(int data):data(data),next(NULL){}
class list
{
public:
        list();
        list(list&);
        bool insert(int);//头插
        bool insert1(int);//尾插
        bool delet(int);//删除某元素
        bool delet(int,int&);//删除第几个元素
        void show();
        int show_len();
private:
    pnode head;
    pnode rear;
    int len;
};
list::list():head(NULL),rear(NULL),len(0){}
list::list(list& p):len(p.len),head(NULL),rear(NULL)//拷贝构造中头也要置空!!
{
    pnode tmp=p.head;
    while(NULL!=tmp && this->insert1(tmp->data))//边遍历边拷贝//头插法的拷贝
        tmp=tmp->next;
    this->len++;
}
bool list::insert(int data)//头插法
{
    pnode newnode=new node(data);//C++中可省略struct,系统会自动识别
    if(NULL==newnode)
            return false;
    this->len++;

    newnode->next=head;
    head=newnode;
    return true;
}
bool list::insert1(int data)//尾插法
{
    pnode newnode=new node(data);
    if(NULL==newnode)
            return false;

    if(NULL==head)
        head=newnode;
    else
        rear->next=newnode;
    rear=newnode;
    this->len++;
    return true;
}
bool list::delet(int key)//删除某个元素
{
    pnode front,ploc;
    front=NULL;
    ploc=head;
    while(ploc!=NULL && key!=ploc->data)
    {
        front=ploc;  //你追我赶
        ploc=ploc->next;
    }
    if(NULL==ploc)
            return false;
    if(ploc==head)
            head=head->next;
    else
            front->next=ploc->next;
    delete ploc;
    this->len--;
    return true;
}
bool list::delet(int pos,int& data)//删除第几个元素
{
    pnode pfront=NULL,ploc=this->head;
    int i=1;
    while(i!=pos && ploc!=NULL)
    {
        pfront=ploc;
        ploc=ploc->next;
        i++;
    }
    if(NULL==ploc)//下标超出
        return false;
    data=ploc->data;//保存删除元素
    if(ploc==this->head)//如果要删除头结点
    {
        this->head=this->head->next;
    }
    else
            pfront->next=ploc->next;
    delete ploc;
    this->len--;
    return true;
}
void list::show()
{
    pnode tmp=head;
    while(tmp)
    {
        cout<<tmp->data<<" ";
        tmp=tmp->next;
    }
    cout<<endl;
}
int list::show_len()
{
    return this->len;
}
int main()
{
    list l;
    l.insert1(1);
    l.insert1(2);
    l.insert1(3);
    l.insert1(4);

    int i;
    l.delet(2); 
    l.delet(2,i);
    l.show();
    cout<<l.show_len();
}
面向对象程序设计课程作业 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、付费专栏及课程。

余额充值