一.双向链表的增删改查
node.h
#ifndef NODE_H
#define NODE_H
struct Node
{
int _data;
Node* _next;
Node* _pro; //形成一个双向链表
};
#endif
list.h
#ifndef LIST_H //防止多次引用头文件,如果引用了直接跳到最后一行
#define LIST_H
#include"node.h"
#include<iostream>
using namespace std;
class List
{
public:
List()
{
_head = new Node();
_head->_next = NULL;
_head->_pro = NULL;
_tail = _head;
_len = 0;
}
List(const List& src) //拷贝构造函数
{
_head = new Node(); //先将原来的双向链栈置为空
_head->_next = NULL;
_head->_pro = NULL;
_tail = _head;
_len = 0;
Node* tmp = src._head->_next; //头节点是没有数据储存的
for (; tmp != src._tail; tmp = tmp->_next)
{
insert_tail(tmp->_data);
}
insert_tail(tmp->_data);
}
~List() //析构函数
{
for (int i = _len; i != 0; i--)
{
delete_tail();
}
delete _head;
_head = NULL;
_tail = NULL;
}
List& operator=(const List& src) //一个链表对另一个链表赋值
{
if (&src == this)
{
return *this;
}
for (int i = _len; i != 0; i--)
{
delete_head();
}
Node* tmp = src._head->_next;
for (; tmp != src._tail; tmp = tmp->_next)
{
insert_tail(tmp->_data);
}
insert_tail(tmp->_data);
}
int insert_head(int data)
{
Node* tmp = new Node();
tmp->_data = data;
if (!is_empty())
{
_head->_next->_pro = tmp;
tmp->_next = _head->_next;
tmp->_pro = _head;
_head->_next = tmp;
}
else
{
_head->_next = tmp;
tmp->_pro = _head;
tmp->_next = NULL;
_tail = tmp;
}
_len++;
return data;
}
int insert_tail(int data)
{
Node* tmp = new Node();
tmp->_data = data;
_tail->_next = tmp;
tmp->_pro = _tail;
tmp->_next = NULL;
_tail = tmp;
_len++;
return data;
}
int delete_head()
{
if (is_empty())
{
return -1;
}
Node* p = _head->_next;
if (NULL != p->_next)
{
p->_next->_pro = _head;
}
_head->_next = p->_next;
_len--;
int data = p->_data;
delete p;
return data;
}
int delete_tail()
{
if (is_empty())
{
return -1;
}
Node* p = _tail;
_tail->_pro->_next = NULL;
_tail = _tail->_pro;
int data = p->_data;
delete p;
return data;
}
int get_first()
{
if (is_empty())
{
return -1;
}
return _head->_next->_data;
}
int get_last()
{
if (is_empty())
{
return -1;
}
return _tail->_data;
}
int get_len()
{
return _len;
}
bool is_empty()
{
return _len == 0 ? 1 : 0;
}
void show()
{
Node* tmp = _head->_next;
for (int i = 0; i < _len; i++)
{
cout << tmp->_data << "->";
tmp = tmp->_next;
}
cout << endl;
}
private:
Node* _head;
Node* _tail;
int _len;
};
#endif
main.cpp
#include<iostream>
#include"list.h"
using namespace std;
int main()
{
List list1;
for (int i = 0; i < 5; i++)
{
list1.insert_head(i);
}
for (int i = 5; i < 10; i++)
{
list1.insert_tail(i);
}
list1.show();
List list2 = list1;
list2.show();
List list3;
list3 = list2;
list3.show();
return 0;
}
二.用上面的双向链表写一个栈
先看几个概念:
1.组合类 — 嵌套
class head
{
string _eye;
string _ear;
string _mouth;
};
class persion
{
string _name;
head _head;
};
class persion
{
class head
{
string _eye;
string _ear;
string _mouth;
};
string _name;
head _head;
};
2.类之间的代理关系
一个类完全套用另一个类的方法
一个的方法集合属于另一个类的方法集合,如:
private:
List _list; //类之间的代理关系
3.友元:友元类和友元函数
类的成员如果存在对象:先构造成员对象,在构造自身,先析构自身,在析构成员对象
把成员对像构造放在初始化链表之前,就算不写也会有默认构造函数,(初始化链表的行为发生在对象构造之前)
如果成员对象没有默认构造函数,或者需要在构造的时候传参数,就需要手动将成员函数的构造写入初始化列表里。
类内实现的成员方法默认是inline关键字
类外实现的不会默认inline
node.h
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int _data;
Node* _next;
Node* _pro;
};
#endif
stack.h
#ifndef STACK_H
#define STACK_H
#include"list.h"
class Stack
{
private:
List _list; //类之间的代理关系
public:
Stack()
//先构造list
:_list(10)
{
cout << "Stack() " << endl;
}
Stack(const Stack& src) :_list(src._list) {}
~Stack()
{
cout << "~Stack()" << endl;
}
Stack& operator=(const Stack& src)
{
if (&src == this)
{
return *this;
}
_list = src._list;
return *this;
}
int push(int data)
{
return _list.insert_tail(data);
}
int pop()
{
return _list.delete_tail();
}
int top()
{
return _list.get_last();
}
void show()
{
_list.show();
}
bool is_empty()
{
return _list.is_empty();
}
};
#endif
list.h
#ifndef LIST_H
#define LIST_H
#include"node.h"
#include<iostream>
using namespace std;
class List;
class Stack;
void show( List& list);
class List
{
public:
List(int a)
{
cout << "List()" << endl;
_head = new Node();
_head->_next = NULL;
_head->_pro = NULL;
_tail = _head;
_len = 0;
}
List(const List& src)
{
_head = new Node();
_head->_next = NULL;
_head->_pro = NULL;
_tail = _head;
_len = 0;
Node* tmp = src._head->_next;
for (; tmp != src._tail; tmp = tmp->_next)
{
insert_tail(tmp->_data);
}
insert_tail(tmp->_data);
}
~List()
{
cout << "~List()" << endl;
for (int i = _len; i != 0; i--)
{
delete_tail();
}
delete _head;
_head = NULL;
_tail = NULL;
}
List& operator=(const List& src)
{
if (&src == this)
{
return *this;
}
for (int i = _len; i != 0; i--)
{
delete_head();
}
Node* tmp = src._head->_next;
for (; tmp != src._tail; tmp = tmp->_next)
{
insert_tail(tmp->_data);
}
insert_tail(tmp->_data);
}
int insert_head(int data)
{
Node* tmp = new Node();
tmp->_data = data;
if (!is_empty())
{
_head->_next->_pro = tmp;
tmp->_next = _head->_next;
tmp->_pro = _head;
_head->_next = tmp;
}
else
{
_head->_next = tmp;
tmp->_pro = _head;
tmp->_next = NULL;
_tail = tmp;
}
_len++;
return data;
}
int insert_tail(int data)
{
Node* tmp = new Node();
tmp->_data = data;
_tail->_next = tmp;
tmp->_pro = _tail;
tmp->_next = NULL;
_tail = tmp;
_len++;
return data;
}
int delete_head()
{
if (is_empty())
{
return -1;
}
Node* p = _head->_next;
if (NULL != p->_next)
{
p->_next->_pro = _head;
}
_head->_next = p->_next;
_len--;
int data = p->_data;
delete p;
return data;
}
int delete_tail()
{
if (is_empty())
{
return -1;
}
Node* p = _tail;
_tail->_pro->_next = NULL;
_tail = _tail->_pro;
_len--;
int data = p->_data;
delete p;
return data;
}
int get_first()
{
if (is_empty())
{
return -1;
}
return _head->_next->_data;
}
int get_last()
{
if (is_empty())
{
return -1;
}
return _tail->_data;
}
int get_len()
{
return _len;
}
bool is_empty()
{
return _len == 0 ? 1 : 0;
}
private:
Node* _head;
Node* _tail;
int _len;
void show()
{
Node* tmp = _head->_next;
for (int i = 0; i < _len; i++)
{
cout << tmp->_data << "->";
tmp = tmp->_next;
}
cout << endl;
}
friend class Stack;
friend void show( List& list);
};
#endif
main.cpp
#include<iostream>
#include"list.h"
#include"stack.h"
using namespace std;
void show( List& list)
{
list.show();
}
int main()
{
/*
List list1;
for (int i = 0; i < 5; i++)
{
list1.insert_head(i);
}
for (int i = 5; i < 10; i++)
{
list1.insert_tail(i);
}
//list1.show();
List list2 = list1;
//list2.show();
List list3;
list3 = list2;
//list3.show();
Stack stack1;
for (int i = 0; i < 10; i++)
{
stack1.push(i);
}
stack1.show();
while (!stack1.is_empty())
{
cout << stack1.top() << " ";
stack1.pop();
}
*/
Stack stack1;
return 0;
}