转载自https://blog.csdn.net/tianzhaixing2013/article/details/22786001
#include <iostream>
#include <stdio.h>
#include <string>
#include <conio.h>
/**
* cstdio是将stdio.h的内容用C++头文件的形式表示出来。
*stdio.h是C标准函数库中的头文件,即:standard buffered input&output。
*提供基本的文字的输入输出流操作(包括屏幕和文件等)。
*/
/**
*conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函数,
*主要是一些用户通过按键盘产生的对应操作,比如getch()()函数等等。
*/
/** 查找链表的倒数第k个元素
* 为了实现只遍历链表一次就能找到倒数第k个节点,我们定义两个指针。
* 第一个指针从链表的头指针开始遍历向前走k-1步,第二个指针保持不动。
* 从第k步开始,第二个指针也开始从链表的头指针开始遍历。由于两个指针
* 的距离保持在k-1,当第一个指针到达链表的尾节点时,第二个指针刚好走到
* 倒数第k个节点。
*/
using namespace std;
struct node
{
int data; //链表的数据域
node *next; //链表的指针域
};
// 创建单链表
node *creat()
{
node *head, *p; //head链表头结点
head = new node; //没有内存,何来指针
p = head;
int x, cycle = 1; //x从键盘输入的数据 cycle算是标志位
while (cycle)
{
cout << "Please input the data for single linker : ";
cin >> x; //2 4 5 0
if (x != 0)
{
node *s = new node;
s->data = x; //s结点指向的数据域为键盘输入的x
cout << "Input data : " << x << endl;
p->next = s;
//cout << p->data << endl; //一串数字、 2、4
//cout <<s->data << endl; //2、4、5
p = s;
//cout << p->data << endl; //2、4、5
}
else
{
cycle = 0;
cout << "Input done! " << endl;
}
} //返回while,判断while(cycle),此时cycle为0,直接跳到head = head->next;
//cout << head->data << endl; //一串数字
head = head->next;
//cout << head->data << endl; //2 将head指向了下一个结点
p->next = NULL;
//cout << p->data << p->next << endl; //5 00000000
return head;
}
// 单链表测长
int length(node *head)
{
int n = 0;
node *p = head;
while (p != NULL)
{
p = p->next;
n++;
}
return n;
}
// 单链表打印 从头到尾
void PrintListFormHeadToTail(node *head)
{
node *p = head;
while (p != NULL)
{
cout << "Single Linker data is " << p->data << endl;
p = p->next;
}
}
// 单链表打印 从尾到头
void PrintListFormTailToHead(node *pHead)
{
if (pHead == NULL)
{
return;
}
else
{
PrintListFormTailToHead(pHead->next);
cout << "Single Linker data is " <<pHead->data << endl;
}
}
// 单链表插入
node *insert(node *head, int num)
{
node *p0, *p1, *p2;
p1 = head;
p2 = new node;
p0 = new node; // 插入节点
p0->data = num;// 插入数据
while (p0->data > p1->data && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;// p0,p1和p2位置: p2->p1->p0
}
if (p0->data <= p1->data)
{
if (p1 == head)
{// 头部前段插入 p0和p1位置: p0->p1->...
head = p0;
p0->next = p1;
}
else
{// 插入中间节点 p0,p1和p2位置: p2-> p0 -> p1
p2->next = p0;
p0->next = p1;
}
}
else
{ // 尾部插入节点 p0,p1和p2位置: p2->p1->p0->NULL
p1->next = p0;
p0->next = NULL;
}
return head;
}
// 单链表删除
node *del(node *head, int num)
{
node *p1, *p2;
p2 = new node;
p1 = head;
while (num != p1->data && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;// p1和p2位置: p2->p1
}
if (num == p1->data)
{
if (p1 == head)// 删除头节点
{
head = p1->next;
delete p1;
}
else
{
p2->next = p1->next;
delete p1;
}
}
else
{
cout << num << " could not been found in the current single linker!" << endl;
}
return head;
}
// 单链表排序
node *sort(node *head)
{
if (head == NULL || head->next == NULL)
{
return head;
}
node *p = head;// 初始化p指针为头结点
int n, temp;
n = length(head);
for (int j = 0; j < n; j++)
{
p = head;
for (int i = j + 1; i < n; i++)
{
if (p->data > p->next->data)
{
temp = p->data;
p->data = p->next->data;
p->next->data = temp;
}
else
{
p = p->next;
}
}
}
return head;
}
// 单链表逆置
node *reverse(node *head)
{
node *p1, *p2, *p3;
if (head == NULL || head->next == NULL)
{
return head;
}
p1 = head;
p2 = head->next;
while (p2)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = NULL;
head = p1;
cout << head->data << endl;
return head;
}
//单链表倒数第k个元素
node *findKthToTail(node *head, int k)
{
node *fast = head;
node *slow = NULL;
if (head == NULL || k <= 0){
return NULL;
}
for (int i = 0; i < k - 1; i++){
fast = fast->next;
}
slow = head;
while (fast->next != NULL){
slow = slow->next;
fast = fast->next;
}
cout << "The " << k << "th to tail is " << slow->data << endl;
return slow;
}
//单链表的中间结点
node *SearchMid(node *head) //如果有两个中间结点,返回的是第一个
{
node *slow = head;
while (head->next != NULL && head->next->next != NULL)
{
head = head->next->next;
slow = slow->next;
}
cout << "The mid is " << slow->data << endl;
return slow;
}
int main()
{
cout << "***创建单链表***" << endl;
node *head = creat();
cout << endl;
cout << "***计算链表长***" << endl;
int n = length(head); //计算上一步创建的head链表的长度
cout << "The length of input single linker is " << n << "." << endl;
cout << endl;
cout << "***打印单链表 从头到尾***" << endl;
PrintListFormHeadToTail(head); //从头到尾打印创建的head链表
cout << endl;
cout << "***打印单链表 从尾到头***" << endl;
PrintListFormTailToHead(head); //从尾到头打印创建的head链表
cout << endl;
cout << "****插入节点****" << endl;
cout << "Please input the data for inserting operate : ";
int inData;
cin >> inData;
head = insert(head, inData); //在head链表中插入结点
PrintListFormHeadToTail(head);
cout << endl;
cout << "****删除节点****" << endl;
cout << "Please input the data for deleting operate : ";
int outData;
cin >> outData;
head = del(head, outData); //在head链表中删除结点
PrintListFormHeadToTail(head);
cout << endl;
cout << "***单链表排序***" << endl;
head = sort(head); 对head链表进行排序
PrintListFormHeadToTail(head);
cout << endl;
//一层一层下来 head已经被改变了,此时是从小到大排序好的head
cout << "***单链表逆置***" << endl;
head = reverse(head);
PrintListFormHeadToTail(head); //将head链表逆置,但是逆置结果给了rev,此时不改变原来head链表的顺序
cout << endl;
cout << "***查找倒数第k个元素***" << endl;
cout << "Please input the data for find Kth to tail : ";
int findData;
cin >> findData;
node *Kth = findKthToTail(head, findData); //在排序好的head链表中查找倒数第K个元素,结果返回给Kth
cout << endl;
cout << "***单链表的中间结点***" << endl;
node *mid = SearchMid(head); //在排序好的head链表中查找中间的结点,结果返回给mid
cout << endl;
return 0;
}
单链表的基本操作 C++
最新推荐文章于 2024-05-16 21:12:43 发布