/*!
* @file 链表基本操作.cpp
* @Date: 2018/01/28 10:32
* @author: sicaolong
* @Contact: sicaolong@163.com
* @brief:
* @TODO:
*/
#include<iostream>
#include <stack>
using namespace std;
typedef struct Node
{
int data;//数据域
Node *next;//指针域
}*PNODE;
PNODE creat_list();//创建整个链表(尾插法)
void treverse_list(PNODE PHead);//遍历链表
bool Is_empty(PNODE PHead);//判断链表是否为空
int length_list(PNODE PHead);//求链表的长度
bool insert_list(PNODE PHead,int pos,int val);//在链表的pos位置插入数据源val
bool delete_list(PNODE, int pos, int *val);//删除第pos位置上的数据
bool remove_list(PNODE, int val);//只删除单链表中的出现的第一个val值
void sort_list(PNODE PHead);//遍历链表;
void reverse_out1(PNODE PHead);//逆序输出,但是不改变数组(栈的用法)
void reverse_out2(PNODE PHead);//逆序输出,递归使用
//===============主函数
int main()
{
PNODE PHead = NULL;
int val;//要删除的结点的数据保存到val中
/*if (Is_empty(PHead))
cout << "链表为空。" << endl;
else
cout << "链表不为空。" << endl;*/
PHead = creat_list();
treverse_list(PHead);
if (Is_empty(PHead))
cout << "链表为空。" << endl;
else
cout << "链表不为空。" << endl;
int length = length_list(PHead);
cout << "长度为:" << length << endl;
cout << "升序排列如下:" << endl;
sort_list(PHead);
treverse_list(PHead);
insert_list(PHead, 2, 100);
treverse_list(PHead);
if (delete_list(PHead, 3, &val))
{
cout << "删除成功" << endl;
cout << "您删除的第三个位置的元素是" << val << endl;
}
cout << "此时的的链表为:" << endl;
treverse_list(PHead);
int data_to_remove;
cout << "您要删除的数据为:";
cin >> data_to_remove;
if (remove_list(PHead,data_to_remove))
{
cout << "删除成功" << endl;
cout << "删除后的链表为:" << endl;
treverse_list(PHead);
}
else
{
cout << "链表中不存在该数据" << endl;
}
reverse_out1(PHead);
cout << "递归的逆序输出:" << endl;
reverse_out2(PHead);
}
//===============判断链表是否为空
bool Is_empty(PNODE PHead)
{
if (!PHead->data)
return true;
else
return false;
}
//===============创建整个链表(尾插法)
PNODE creat_list()
{
int len;
int val;
PNODE PHead = (PNODE)malloc(sizeof(Node));
if (NULL==PHead)
{
cout << "分配失败" << endl;
exit(-1);
}
//pTail永远指向尾结点;
PNODE PTail = PHead;
PTail->next = NULL;
cout << "请输入您要输入的有效结点的个数len:";
cin >> len;
for (int i = 0; i < len;i++)
{
cout << "请输入第" << i + 1 << "个数的值:";
cin >> val;
PNODE PNew = (PNODE)malloc(sizeof(Node));
if (PNew==NULL)
{
cout << "分配失败;" << endl;
exit(-1);
}
PNew->data = val;
//将新生成的结点挂到整个链表的最后位置
PTail->next = PNew;
PNew->next = NULL;
PTail = PNew;//ptail再指向最后一个结点;
}
return PHead;
}
//===============遍历链表;
void treverse_list(PNODE PHead)
{
cout << "遍历链表输出:" << endl;
PNODE p = PHead->next;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
return;
}
//===============求链表的长度
int length_list(PNODE PHead)
{
PNODE p = (PNODE)malloc(sizeof(Node));
p = PHead->next;
int len = 0;
while (p)
{
len++;
p =p->next;
}
return len;
}
//===============链表的升序排列
void sort_list(PNODE PHead)
{
PNODE p;
PNODE q;
int t;
int len = length_list(PHead);
int i, j;
for ( i = 0, p = PHead->next; i < len - 1; ++i, p = p->next)
{
for (j = i + 1, q = p->next; j < len; ++j, q = q->next)
{
if ((p->data)>(q->data))
swap(p->data, q->data);
}
}
}
//===============在链表的pos位置插入数据源val
bool insert_list(PNODE PHead, int pos, int val)
{
int i = 0;
PNODE p = PHead;
while (p&&i<pos-1)
{
p = p->next;
++i;
}
if (!p || i>pos - 1)
return false;
PNODE PNew = (PNODE)malloc(sizeof(Node));
if (NULL==PNew)
{
cout << "动态内存分配失败" << endl;
exit(-1);
}
PNew->data = val;
//新建一个指针替换p->next
/*PNODE q = p->next;
p->next = PNew;
PNew->next = q;*/
//直接后、前;
PNew->next = p->next;
p->next = PNew;
return true;
}
//===============删除第pos位置上的数据
bool delete_list(PNODE PHead, int pos, int *Pval)
{
int i = 0;
while (PHead->next&&i<pos-1)
{
PHead = PHead->next;
i++;
}
if (i>pos-1||!PHead->next)
{
return false;
}
PNODE q = PHead->next;
*Pval = q->data;
PHead->next = q->next;
free(q);
q = nullptr;
return true;
}
//===============只删除单链表中的出现的第一个val值
bool remove_list(PNODE PHead, int val)
{
if (PHead == NULL)
return false;
while (PHead->next)
{
if (PHead->next->data != val)
{
PHead = PHead->next;
}
else
{
PNODE q = PHead->next;
PHead->next = q->next;
free(q);
return true;
}
}
return false;
//else
//{
// while (PHead->next&&PHead->next->data!=val)
// {
// PHead = PHead->next;
// }
// if (PHead->next&&PHead->next->data==val)
// {
// p = PHead->next;
// PHead->next = p->next;
// }
//
}
//===============逆序输出1(栈操作)
void reverse_out1(PNODE PHead)
//void reverse_out1(PNODE p)
{
cout << "栈的链表的逆序输出:" << endl;
stack <PNODE>my_stack;
PNODE p = PHead->next;//p代表首节点吧!
while (p)
{
my_stack.push(p);
p = p->next;
}
while (!my_stack.empty())
{
PNODE q = my_stack.top();
cout << q->data << " ";
my_stack.pop();
}
cout << endl;
}
//===============逆序输出2(递归)
void reverse_out2(PNODE PHead)//问题:结果为什么有一个垃圾数字;
{
PNODE p = PHead->next;
if (p)
{
reverse_out2(p);//p递归到函数中;
cout << p->data << " ";
}
return;
}