单链表
1.头插法
template <class T>
struct LinkNode {
T data;//数据
LinkNode<T>* next;
};
//创建单链表不带头结点,前插法(顺序与输入相反)
template <class T>
LinkNode<T>* create0(T N) {
LinkNode<T>* head=NULL,*p=NULL;
for (int i = 0; i < N; i++)
{
p = new LinkNode<T>;
cin >> p->data;
p->next = head;
head = p;
}
return head;
}
2.单链表带头结点,前插法
//创建单链表带头结点,前插法
template <class T>
LinkNode<T>* create1(T endtag) {
LinkNode<T>* head, * tail;
int val;
head = new LinkNode<T>;
if (head == NULL)
{
cerr << "存储分配错误" << endl;
exit(1);
}
cin >> val;
while(val!= endtag) {
tail = new LinkNode<T>(val);
if (tail == NULL)
{
cerr << "存储分配错误" << endl;
exit(1);
}
tail->next = head->next;
head->next = tail; //插入到前端
cin >> val;
}
tail->next = NULL;
return head;
}
3.尾插法(不带表头结点) ( 相对于前插, 需要一个 尾指针)
//创建单链表不带表头结点 尾插法
template <class T>
LinkNode<T>* create11(T N)
{
LinkNode<T>* head=NULL, * tail = NULL,*p=NULL;
for (int i = 0; i < N; i++)
{
p = new LinkNode<T>();
cin >> p->data;
p->next = NULL;
if (first == NULL)
{
first = p;
}
else
{
tail->next = p;
}
tail = p;
}
return head;
}
4.尾插法(带表头结点)( 相对于前插, 需要一个 尾指针)
//创建单链表 尾插法
template <class T>
LinkNode<T>* create2(T endtag) {
LinkNode<T>* head, * tail, * p;
int val;
head = new LinkNode<T>; //加头结点
if (head == NULL)
{
cerr << "存储分配错误" << endl;
exit(1);
}
cin >> val;
tail = head;
while (val != endtag) {
p = new LinkNode<T>(val);
if (p == NULL)
{
cerr << "存储分配错误" << endl;
exit(1);
}
tail->next = p;
tail = p; //插入到前端
cin >> val;
}
tail->next = NULL;
return head;
}
单向循环链表
//创建单向循环链表 尾插法
//注意单向循环链表,尾节点更有价值,在删除时,影响不大。
//如果是头结点,则删除之后,会报错
template <class T>
LinkNode<T>* create3(T endtag) {
LinkNode<T>* head, * tail, * p;
int val;
head = new LinkNode<T>; //加头结点
head->next = NULL;
if (head == NULL)
{
cerr << "存储分配错误" << endl;
exit(1);
}
cin >> val;
tail = head;
while (val != endtag) {
p = new LinkNode<T>;
p->data = val;
if (p == NULL)
{
cerr << "存储分配错误" << endl;
exit(1);
}
tail->next = p;
tail = p; //插入到前端
cin >> val;
}
tail->next = head->next;
return tail;
}
对链表进行插入
template <class T>
bool List<T>::Insert(int i, T x)
{
//插入第i个元素之后
LinkNode<T>* current = first, *newNode=NULL;
if (current == 0) return false;
for (int m = 1; m <=i; m++)
{
current = current->link;
}
newNode->link = current->link;
current->link = newNode;
return true;
}
改进可以用互换法;
如果只知道这个结点,要插入前面。
则可以先插入后面,再把值调换。
写一个链表类
#include <iostream>
using namespace std;
template <class T>
struct LinkNode
{
T data;
LinkNode<T>* link;
LinkNode() { link = 0;}
LinkNode(T item, LinkNode<T>*ptr =0)
{
data = item; link = ptr;
}
bool operator ==(T x) { return data == x; }
bool operator !=(T x){ return data != x; }
};
template <class T>
class List
{
protected:
LinkNode<T>* first;
public:
List() { first = new LinkNode<T>; }
List(T x) { irest = new LinkNode<T>(x); }
~List() {};
void Print(); //输出链表的结点值
bool Insert(int i, T x);
};
/// <summary>
///搜索,求表长等不一一列举
/// </summary>
/// <typeparam name="T"></typeparam>
template <class T>
void List<T>::Print()
{
LinkNode<T>* p = first->link;
while (p != NULL)
{
cout << p->data;
p = p->link;
}
}
template <class T>
bool List<T>::Insert(int i, T x)
{
//插入第i个元素之后
LinkNode<T>* current = first, *newNode=NULL;
if (current == 0) return false;
for (int m = 1; m <=i; m++)
{
current = current->link;
}
newNode->link = current->link;
current->link = newNode;
return true;
}
template <class T>
List<T>::~List() {
LinkNode<T>* q;
while (first != NULL)
{
q = first;
first = first->link;
delete q;
}
}
问题:两个链表合并成一个线性表
这里想到循环链表
因为尾结点不好找到,但是循环链表的尾结点可以保存,不需要头结点,这样就可以比较方便的链接两条链。
`
//初始化循环链表
template <class T>
LinkNode<T>* initial(LinkNode<T>* head)
{
head = new LinkNode<T>;
LinkNode<T>* tail,*p;
tail = head;
p = head;
cin >> p->data;
if (p->data == 0)
{
return NULL;
}
while(p->data!=0)
{
tail->link = p;
tail = p;
p = new LinkNode<T>;
cin >> p->data;
}
tail->link = head;
return tail;
}
int main()
{
LinkNode<int>* tail1 = 0, * tail2 = 0 ,*p=0;
tail1 = initial(tail1);
tail2 = initial(tail2);
p = tail1;
do
{
cout << p->data << " ";
p = p->link;
} while (p != tail1);
p = tail2;
do
{
cout << p->data << " ";
p = p->link;
}while (p != tail2);
p = tail1->link;
tail1->link = tail2->link;
tail2->link = p;
p = tail1;
do
{
cout << p->data << " ";
p = p->link;
} while (p != tail1);
return 0;
}
循环链表的创建(有一个空的头结点)
template <class T>
void printlist(LinkNode<T>* head) {
LinkNode<T>* p;
int i = 0;
p = head->next;
do
{
cout << p->data << endl;
p = p->next;
} while (p != head);
cout << endl;
}
template <class T>
LinkNode<T>* initial(LinkNode<T>* head,int n)
{
head = new LinkNode<T>;
LinkNode<T>* tail, * p;
tail = head;
int i = 0;
while (i<n)
{
i++;
p = new LinkNode<T>;
tail->next = p;
tail = p;
cin >> p->data;
}
tail->next = head;
return head;
}
int main() {
LinkNode<int>* list=NULL;
int n = 0;
cin >> n;
list = initial<int>(list,n);
cout << " 输出链表" << endl;
printlist<int>(list);
return 0;
}