无聊时写的——纪念连指针都不懂时,看链表一头雾水的那段时光。。。
/**
此链表有头指针,头节点,尾指针(无有效元素)
输入输出样例::
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
3
还未建表,请先输入 1 建表
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
1
creatlink 输入type型:a,a==-1结束:
3 2 4 4 5 6 5 -1
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
2
遍历节点:输入了7个节点,节点值为:
3 2 4 4 5 6 5
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
3
输入elemtype型a,查找a所在节点的位置,a==-1结束
2
2 为此链表第 2 个节点的值
3
3 为此链表第 1 个节点的值
-1
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
4
输入int型a,在第a个节点*后*插入elemtype型(a==0代表在第一个位置前插入) b,a==-1结束。格式 (a b)
3 4
-1
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
2
遍历节点:输入了8个节点,节点值为:
3 2 4 4 4 5 6 5
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
5
输入int型a,删除第a个节点,a==-1结束
5
-1
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
2
遍历节点:输入了7个节点,节点值为:
3 2 4 4 5 6 5
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 -1结束
6
从小到大 排序
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 -1结束
2
遍历节点:输入了7个节点,节点值为:
2 3 4 4 5 5 6
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
7
逆置链表
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
2
遍历节点:输入了7个节点,节点值为:
6 5 5 4 4 3 2
输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束
-1
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef int elemtype;
struct Node
{
elemtype data;
Node *next;
//Node *pre;
};
Node *head;
Node *first; //首元节点 记录总元素节点数
Node *tail;
bool Is_NullList() //是否建表
{
if(head->next!=NULL)
return 1;
cout<<"还未建表,请先输入 1 建表"<<endl;
return 0;
}
void Init() //为判断是否已建表用
{
head=(Node*)malloc(sizeof(Node));
head->next=NULL;
}
void Creat()
{
//Node *head; 全局:head只是用来找到此链表
head=(Node*)malloc(sizeof(Node));
head->next=NULL; //init(head)
//Node *first; //首元节点 记录总元素节点数
first=(Node*)malloc(sizeof(Node));
first->data=0;
head->next=first; //head 指向首元
elemtype a;
cout<<"creatlink 输入type型:a,a==-1结束:"<<endl;
cin>>a;
Node *p=first;
while(a!=-1)
{
first->data++;
//cout<<"creatlink 输入type型a,a==-1结束:"<<endl;
Node *cur;
cur=(Node *)malloc(sizeof(Node));
cur->data=a;
p->next=cur;
p=cur;
cin>>a;
//cout<<p->data<<endl;
}
if(a==-1)
{
//Node *tail; //尾节点
tail=(Node *)malloc(sizeof(Node));
p->next=tail;
p=tail;
p->next=NULL;
}
}
void Traverse()
{
if(!Is_NullList()) return; //判断是否建表
cout<<"遍历节点:";
cout<<"输入了"<<first->data<<"个节点,节点值为:"<<endl;
Node *cur;
//cur=(Node *)malloc(sizeof(Node));
cur=head;
cur=head->next; //cur 首元节点
cur=cur->next; //cur 第一个赋值节点
while(cur->next!=NULL)
{
cout<<cur->data<<" ";
cur=cur->next;
}
//cout<<cur->data;
cout<<endl;
}
void Find_node()
{
if(!Is_NullList()) return;
cout<<"输入elemtype型a,查找a所在节点的位置,a==-1结束"<<endl;
elemtype a;
while(cin>>a&&(a!=-1))
{
Node *cur;
//cur=(Node *)malloc(sizeof(Node));
cur=head;
cur=head->next; //cur 首元节点
cur=cur->next; //cur 第一个赋值节点
int count=0,flag=0; //flag 表示 是否有值为a的节点
while(cur->next!=NULL)
{
count++;
if(cur->data==a)
{
cout<<a<<" 为此链表第 "<<count<<" 个节点的值"<<endl;
flag=1;
}
//cout<<cur->data<<" ";
cur=cur->next;
}
if(flag==0)
cout<<"无值为 "<<a<<" 的节点"<<endl;
}
}
void Insert_node()
{
if(!Is_NullList()) return;
cout<<"输入int型a,在第a个节点*后*插入elemtype型(a==0代表在第一个位置前插入) b,a==-1结束。格式 (a b)"<<endl;
int a;
elemtype b;
while(cin>>a&&(a!=-1))
{
cin>>b;
Node *cur;
//cur=(Node *)malloc(sizeof(Node));
cur=head;
cur=head->next; //cur 首元节点
int i=0; //第i个节点
//cur=cur->next; //cur 第一个赋值节点
while(cur->next->next!=NULL)
{
if(i==a)
{
Node *p,*q; //*q 保存cur->next; *p保存b的值;
p=(Node *)malloc(sizeof(Node));
q=(Node *)malloc(sizeof(Node));
q=cur->next;
p->data=b;
cur->next=p;
p->next=q;
first->data++;
break;
}
i++;
cur=cur->next;
}
}
}
void Delete_node()
{
if(!Is_NullList()) return;
cout<<"输入int型a,删除第a个节点,a==-1结束"<<endl;
int a;
Init();
while(cin>>a&&(a!=-1))
{
Node *cur;
//cur=(Node *)malloc(sizeof(Node));
cur=head;
cur=head->next; //cur 首元节点
//cur=cur->next; //cur 第一个赋值节点
int i=0;
while(cur->next->next!=NULL)
{
//cout<<cur->data<<" ";
i++;
if(i==a)
{
cur->next=cur->next->next;
first->data--;
break;
}
cur=cur->next;
}
}
}
void QuickSort_line(Node *first,Node *tail) //快速排序
{
if(!Is_NullList()) return;
if(first->next==tail||first->next->next==tail) ///
return;
Node *mid=first->next; //所取的点
Node *p=first; //比mid小的数
Node *q=mid; //比mid大的数
elemtype pivot=mid->data; //mid的值
Node *t=mid->next; //开始遍历起点
while (t!=tail)
{
if (t->data<pivot )
{
p->next=t;
p=p->next;
}
else
{
q->next=t;
q=q->next;
}
t=t->next;
}
p->next=mid;
q->next=tail;
QuickSort_line(first, mid);
QuickSort_line(mid, tail);
}
void Inverse_link()
{
if(!Is_NullList()) return;
cout<<"逆置链表"<<endl; //第i个和第first->data-i+1个互换 值互换(非节点互换)
int i=0;
int num=(first->data+1)/2;
Node *p;
while(num--)
{
p=first;
i++;
for(int ii=1;ii<=i;ii++)
{
p=p->next;
}
elemtype l=p->data; //记录第i个值
p=first;
for(int ii=1;ii<=first->data-i+1;ii++)
p=p->next;
elemtype r=p->data; //记录第first->data-i+1个值
p->data=l;
p=first;
for(int ii=1;ii<=i;ii++) //为互换而用
p=p->next;
p->data=r;
}
}
void Destroylist() //销毁链表
{
cout<<"此链表已销毁"<<endl;
Node *p = head;
while(head!=NULL)
{
head = head->next;
free(p);
p = head;
}
Init();
}
int main()
{
cout<<"输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束"<<endl;
Init();
int a;
while(cin>>a&&a!=-1)
{
switch (a)
{
case 1:{Creat();break;};
case 2:{Traverse();break;};
case 3:{Find_node();break;};
case 4:{Insert_node();break;};
case 5:{Delete_node();break;};
case 6:{QuickSort_line(first,tail);cout<<"从小到大 排序"<<endl;break;};
case 7:{Inverse_link();break;};
case 8:{Destroylist();break;};
default :cout<<a<<"为非法数据,请输入1~8的数"<<endl;
}
cout<<"输入: 1.建立链表 2.遍历链表 3.查找 4.插入 5.删除 6.排序 7.逆置 8.销毁 -1结束"<<endl;
}
Destroylist();
return 0;
}