#include<iostream>
#include<stdlib.h>
typedef struct _Node{
int data;
struct _Node* next;
}Node;
typedef Node* ptrtoNode;
typedef ptrtoNode List;
using namespace std;
void destoryList(List &L){
if(!L){
cout<<"已销毁"<<endl;
return;
}
List p;
while(L){
p=L;
L=L->next;
free(p);
}
}
void remakeList(List L){
if(!L){
cout<<"不存在"<<endl;
return;
}
List p;
List q;
p=L->next;
while(p){
q=p->next;
free(p);
p=q;
}
L->next=NULL;
L->data=0;
}
List makeList(List L){
if(L){
remakeList(L);
}
List P=(List)malloc(sizeof(Node));
P->next=NULL;
return P;
}
int getLength(List L){
int i=0;
while(L){
L=L->next;
i++;
}
return i-1;
}
int getElement(List L,int i){
if(!L->next){
cout<<"空"<<endl;
return 0;
}
if(i>=getLength(L)){
cout<<"不存在"<<endl;
return 0;
}
for(int j=0;j<=i;j++){
L=L->next;
}
return L->data;
}
int getPosition(List L,int n){
int cnt=-1;
while(L){
L=L->next;
if(!L){
cout<<"找不到"<<endl;
return -1;
}
cnt++;
if(L->data==n)
break;
}
return cnt;
}
int findPrevious(List L,int n){
return getElement(L,getPosition(L,n)-1);
}
int findNext(List L,int n){
return getElement(L,getPosition(L,n)+1);
}
void insert(List L,int i,int n){
if(!L){
cout<<"不存在"<<endl;
return;
}
for(int j=0;j<=i-1;j++){
if(!L->next){
List P=(List)malloc(sizeof(Node));
P->data=0;
P->next=L->next;
L->next=P;
}
L=L->next;
}
List P=(List)malloc(sizeof(Node));
P->data=n;
P->next=L->next;
L->next=P;
}
void deleteL(List L,int i){
if(i>=getLength(L)){
cout<<"不存在"<<endl;
return;
}
for(int j=0;j<=i-1;j++){
L=L->next;
}
List P=L->next;
L->next=P->next;
free(P);
}
void show(List L){
int cnt=0;
if(getLength(L)<=0){
cout<<"不存在"<<endl;
return;
}
int n=getLength(L);
for(int i=0;i<n;i++){
cnt++;
L=L->next;
if(cnt%5==0)
cout<<L->data<<endl;
else cout<<L->data<<" ";
}
}
void headCreateList(List L,int s[],int n){
remakeList(L);
for(int i=0;i<n;i++){
List P=(List)malloc(sizeof(Node));
P->data=s[i];
P->next=L->next;
L->next=P;
}
}
void reverse(List L){
int n=getLength(L);
List p=L;
int s[n];
for(int i=0;i<n;i++){
p=p->next;
s[i]=p->data;
}
headCreateList(L,s,n);
}
int main(){
List L;
int i;
do{
cout<<"1--初始化或重置链表\n"
<<"2--销毁链表\n"
<<"3--重置链表\n"
<<"4--链表长度\n"
<<"5--指定位置的元素\n"
<<"6--链表已存在元素的位序\n"
<<"7--求前驱\n"
<<"8--求后继\n"
<<"9--在第i位插入\n"
<<"10--删除第i位\n"
<<"11--show\n"
<<"12--初始化并用头插法输入\n"
<<"13--逆序\n"
<<"输入负数退出"<<endl;
cin>>i;
system("cls");
switch(i){
case 1:L=makeList(L);
break;
case 2:destoryList(L);
break;
case 3:remakeList(L);
break;
case 4:cout<<"长度为:"<<getLength(L)<<endl;
break;
case 5:
cout<<"输入位置"<<endl;
int t1;
cin>>t1;
cout<<"第"<<t1<<"位的元素是:"<< getElement(L,t1)<<endl;
break;
case 6:
cout<<"输入元素"<<endl;
int t2;
cin>>t2;
cout<<t2<<"是第"<<getPosition(L,t2)<<"位元素"<<endl;
break;
case 7:
int t3;
cin>>t3;
cout<<findPrevious(L,t3)<<endl;
break;
case 8:
int t4;
cin>>t4;
cout<<findNext(L,t4)<<endl;
break;
case 9:
cout<<"输入位置和元素"<<endl;
int t5;
int t6;
cin>>t5>>t6;
insert(L,t5,t6);
break;
case 10:
cout<<"输入位置"<<endl;
int t7;
cin>>t7;
deleteL(L,t7);
break;
case 11:show(L);
break;
case 12:
cout<<"输入个数"<<endl;
int n;
cin>>n;
int s[100];
cout<<"输入"<<endl;
for(int j=0;j<n;j++){
cin>>s[j];
}
headCreateList(L,s,n);
break;
case 13:reverse(L);
break;
default:
cout<<" "<<i<<'?'<<endl;
break;
}
cout<<"completed"<<endl;
system("pause");
system("cls");
}while(i>=0);
return 0;
}