**
单链表的初始化、删除、插入、查找、清空、输出、交换两结点位置等操作
**
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
/*
* 注意下面两者的区别:
* Node *node1 = new Node();
* Node *node2;
* 前者是声明一个 Node 类型的指针,并且该指针指向一个具体的对象;
* 后者只是声明了一个 Node 类型的指针
*/
class Linklist
{
Node Head;
int Length;
void Init(int n); // 初始化,一共有 n 个数据
bool Del(int n); // 删除第 n 个结点
void Show(); // 显示
int Locate(int n); // 查找 n, 返回其结点位置,找不到则返回 0
void Reverse(); // 反转
void Clear(); // 清空链表
void Add(int n); // 在链表尾部插入
void Insert(int x,int n); // 在链表中间 x 处插入数据 n
bool ListEmpty(); // 检查链表是否为空
int getLength(); // 获取链表的长度
void Exchange(int x, int y); // 交换两个位置的结点
};
void Linklist::Init(int n)
{
Head.data = 0; // 初始化头结点
Length = n;
Node *final_node = &Head;
for (int i = 1; i <= n; i++)
{
Node *next = new Node(); // 新结点
cin >> next -> data;
final_node-> next = next;
final_node = next; // 每个新结点都先把它当成最后一个结点
}
final_node->next = NULL; // 真正的最后一个结点
}
void Linklist::Insert(int x, int n)
{
Node *temp = &Head;
int count = 0;
while (temp->next != NULL)
{
count++;
if (count == x)
break;
temp = temp->next;
}// 循环结束时,temp 指向的是要插入位置的前一个结点
if (temp == NULL) // 在最后一个结点后插入
Push(n);
Node *Newnode = new Node(); // 插入一个新结点,理所当然要 new 一个
Newnode -> data = n;
Newnode->next = temp->next; // 新结点指向原来那个位置的结点
temp->next = Newnode;
Length++;
}
bool Linklist::Del(int n)
{
if (n < 1 || n > Length)
return false;
int count = 0;
Node *temp = Head.next;
while (temp->next != NULL)
{
count++;
if (count == n)
break;
temp = temp->next;
} // 循环结束时,temp 指向的是要删除结点的前一个结点
Node *tempnode = temp->next;
temp->next = tempnode->next;
delete tempnode;
Length--;
return true;
}
void Linklist::Show()
{
Node *temp = Head.next; // temp 指向第一个结点
while (temp->next != NULL)
{
cout << temp->data << endl;
temp = temp->next;
}
cout << temp->data << endl; // 最后一个结点
}
void Linklist::Add(int n)
{
Node *temp = &Head;
while (temp->next != NULL)
temp = temp->next; // 循环结束时,temp 指向的是倒数第二个结点
Node *Newnode = new Node();
Newnode->data = n;
temp->next = Newnode;
Newnode->next = NULL;
}
int Linklist::Locate(int n)
{
int flag = 0;
Node *temp = Head.next;
while (temp->next != NULL)
{
flag++;
if (temp->data == n)
return flag;
temp = temp->next;
}
if (temp->data == n) // 如果前面一直都没 return,说明到最后一个结点了
return Length;
}
void Linklist::Clear() // 清空链表,但 Head 还在
{
Node *temp1 = Head.next; // 从第一个结点开始清空
// 要是想销毁整个链表,则从 Head 开始
Node *temp2;
while (temp1)
{
temp2 = temp1->next;
delete temp1;
temp1 = temp2;
}
Head.next = NULL;
}
void Linklist::Reverse()
{
Node *temp1 = Head.next->next;
Node *temp2;
Head.next->next = NULL; // 这段代码我背下来的
while (temp1->next != NULL)
{
temp2 = temp1->next;
temp1->next = Head.next;
Head.next = temp1;
temp1 = temp2;
temp2 = temp1->next;
}
temp1->next = Head.next;
Head.next = temp1;
}
bool Linklist::ListEmpty()
{
Node *temp = &Head;
if (temp->next == NULL)
return true;
else
return false;
}
int Linklist::getLength()
{
int count = 0;
Node *temp = &Head;
while (temp->next != NULL)
{
count++;
temp = temp->next;
}
count++;
return count;
}
void Linklist::Exchange(int x, int y)
{
Node *xNode = &Head;
Node *yNode = &Head;
Node *temp;
int countx = 0, county = 0;
while (xNode->next != NULL)
{
countx++;
if (countx == x)
break;
xNode = xNode->next;
}
while (yNode->next != NULL)
{
county++;
if (county == y)
break;
yNode = yNode->next;
}
temp = xNode->next; // 交换指针
xNode->next = yNode->next;
yNode -> next = temp;
}
void main() {}