用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 ;l3 = l2;这时用的才是赋值控制。