1.昨天的 链表类
1.首先确定链表类都需要一些什么
1.需要定义一个节点来存放内容,typedef struct NODE;
2.需要 链表的头指针、尾指针以及链表的长度信息;
3.链表类的构造函数以及析构函数;
4.链表类的成员函数:在末尾添加节点 PushBack、在表头弹出节点 PopFront、显示链表信息;
class CList
{
private:
typedef struct NODE
{
int n_value;
struct NODE *p_next;
}Node;
private:
Node *n_p_head;
Node *n_p_end;
int n_length;
public:
CList()
{
n_p_head = 0;
n_p_end = 0;
n_length = 0;
}
CList(int count, int value)
{
n_p_head = 0;
n_p_end = 0;
n_length = 0;
for(int i=0; i<count; i++)
{
PushBack(value);
}
}
~CList()
{
while(n_p_head)
PopFront();
}
public:
void PushBack(int value)
{
Node *p_new_node = new Node;
p_new_node->n_value = value;
p_new_node->p_next = 0;
if(n_p_head)
{
n_p_end->p_next = p_new_node;
}
else
{
n_p_head = p_new_node;
}
n_p_end = p_new_node;
++n_length;
}
void PopFront()
{
// 没有节点
if(!n_p_head)
{
return;
}
// 只有一个节点
else if(n_length == 1)
{
n_p_head = 0;
n_p_end