链表(c++实现)

用C++写了一个链表,虽然不难,但也没有想象中那么简单,况且代码质量也不够好。

直接看代码:

list.h

#include<iostream>
using namespace std;
#ifndef LIST
#define LIST

typedef int ElemType;

class List
{
public:
List();
List(const List& other);
List& operator= (const List& other);
~List();

void CreateList(int n);
void Insert_L( int i,ElemType e);
void Delete_L( int num);
void Traverse();
private:
typedef struct LNode
{
ElemType data;
struct LNode* next;

LNode():data(0),next(NULL){}

} Node, *LinkList;
LinkList first;
int mySize;
};
#endif

list.c代码

#include"list.h"
using namespace std;
List::List():first(NULL),mySize(0)
{
}
List:: ~List()
{
    LinkList temp;
    while(first)
    {
        temp = first->next;
        delete first;
        first = temp;
    }
    cout << " end of destruction" << endl;
}
List::List(const List& other):mySize(other.mySize)
{
first = new Node;
if(first == NULL)
{
cerr<<" error"<<endl;
exit(1);
}
LinkList other_ptr = other.first;
first->data = other.first->data;
LinkList present_ptr = first;
LinkList temp_ptr;
while(other_ptr->next != NULL)
{
other_ptr = other_ptr->next;
temp_ptr = new Node;
temp_ptr->data = other_ptr->data;
present_ptr->next = temp_ptr;
present_ptr = temp_ptr;

}
temp_ptr = NULL;
}
List& List::operator= (const List& other)
{
LinkList present_ptr = first;
LinkList temp_ptr;
LinkList other_ptr = other.first;
if(this != &other)
{
if(first != NULL)
{
while(present_ptr)
{
temp_ptr = present_ptr->next;
delete present_ptr;
present_ptr = temp_ptr;
}
first = NULL;
}
first = new Node;
present_ptr = first;
first->data = other_ptr->data;
while(other_ptr->next != NULL)
{
other_ptr = other_ptr->next;
temp_ptr = new Node;
temp_ptr->data = other_ptr->data;
present_ptr->next = temp_ptr;
present_ptr = present_ptr->next;
}
}
return *this;
}
void List::CreateList(int n)
{
LinkList L = new Node;
first = L;
int i;
ElemType data_in;
LinkList temp;
cin >> data_in;
L->data = data_in;
for(i = n; i > 1; --i)
{
temp = new Node;
cin >> data_in;
temp->data = data_in;
L->next = temp;
L = L->next;
}
return;
}
void List::Insert_L( int i, ElemType e)
{
LinkList p = first, temp;

int j = 1;
//ElemType data_in;
while(p && j < i - 1)
{
p = p->next;
++j;
}
if(!p || j > i - 1)
{
cerr << "error insert " << endl;
exit(1);
}
temp = new Node;
temp->data = e;
temp->next = p->next;
p->next = temp;
return;
}
void List::Delete_L(int num)
{
LinkList p = first,q;
int j = 1;
ElemType data_out;
while(p->next && j < num - 1 )
{
p = p->next;
++j;
}
if(!(p->next) || j > num - 1)
{
cerr << "error delete" <<endl;
exit(1);
}
q = p->next;
p->next = q->next;
data_out = q->data;
delete q;
cout << "Delete element :"<< data_out << endl;
return;
}
void List::Traverse()
{
LinkList p = first;
while(p != NULL)
{
cout << p->data << ' ';
p = p->next;
}
cout << endl;
cout << "end of output" << endl;
}

main函数测试:

int main()
{  
    List l;
    l.CreateList(3);
    l.Traverse();
    l.Insert_L(2, 4);
    l.Traverse();
    l.Delete_L(2);
    l.Traverse();
    List l2(l);
    l2.Traverse();
    List l3;
    l3 = l;
    l3.Traverse();
    return 0;
}

主要注意两点: 1)类里面有指针成员,要用好复制控制(拷贝构造函数,赋值控制,析构函数)。

                          2)List l3 = l2;用的是拷贝构造函数,而没有用赋值控制,List l3 ;l= l2;这时用的才是赋值控制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值