初始化
返回长度
头插入/尾插入/位置插入
位置查找/数值查找(all)
位置删除/数值删除(all)
结点交换
反转
打印
释放
#include <iostream>
using namespace std;
#define LEN sizeof(NODE)
typedef struct node{
int data;
node *next;
}NODE;
NODE *Initlist(){
NODE *head;
head=new NODE;
head->next=nullptr;
return head;
}
int Listlen(NODE *head){
int len=0;
NODE *p;
p=head->next;
while(p){
len++;
p=p->next;
}
return len;
}
NODE *tailCreate(){
NODE *p1,*p2,*head;
int input;
head=p2=Initlist();
cout<<"Please input a number to be Inserted[Stop=(-1)]:\n";
cin>>input;
while(input!=-1){
p1=new NODE;
p1->data=input;
p2->next=p1;
p2=p1;
cin>>input;
}
p2->next=nullptr;
return head;
}
NODE *headCreate(){
NODE *p1,*p2,*head;
int input;
head=Initlist();
cout<<"Please input a number to be Inserted[Stop=(-1)]:\n";
cin>>input;
while(input!=-1){
p2=head->next;
p1=new NODE;
p1->data=input;
head->next=p1;
p1->next=p2;
cin>>input;
}
return head;
}
void Insert(int loc,int num,NODE *head){
NODE *p1,*p2,*p3;
p2=head;
while(--loc)
p2=p2->next;
if(p2->next){
p3=p2->next;
p1=new NODE;
p1->data=num;
p2->next=p1;
p2=p1;
p2->next=p3;
cout<<"Insert successfully!\n";
}
else{
p1=new NODE;
p1->data=num;
p2->next=p1;
p2=p1;
p2->next=nullptr;
cout<<"Insert successfully!\n";
}
}
void locSearch(int loc,NODE *head){
NODE *p=head;
cout<<"链表中的第"<<loc<<"个元素是: ";
while(loc--)
p=p->next;
cout<<p->data<<endl;
}
void numSearch(NODE*head){
NODE *p;
p=head->next;
int num,index=1,counter=0;
cout<<"Please input the number you want to search: ";
cin>>num;
while(p){
if(p->data==num){
counter++;
cout<<num<<"第"<<counter<<"次出现在链表中的结点位置是: "<<index<<endl;
p=p->next;
index++;
}
else{
p=p->next;
index++;
}
}
if(!counter)
cout<<num<<"不存在链表中\n";
}
void locDelete(int loc,NODE *head){
NODE *p,*temp;
p=head;
while(--loc)
p=p->next;
temp=p->next;
p->next=temp->next;
delete temp;
cout<<"Delete successfully!\n";
}
void numDelete(NODE *head){
NODE *p,*temp=nullptr;
p=head;
int num;
cout<<"Please input a number to be deleted(All-Delete): ";
cin>>num;
while(p->next){
if(p->next->data!=num)
p=p->next;
else{
temp=p->next;
p->next=temp->next;
delete temp;
}
}
if(temp)
cout<<"Delete successfully!\n";
else
cout<<num<<" is not in the list!\n";
}
void Swap(int x,int y,NODE *head){
NODE *px,*py,*pbx,*pax,*pby,*pay;
pbx=pby=head;
for(int i=0;i<x-1;i++)
pbx=pbx->next;
px=pbx->next;
pax=px->next;
for(int i=0;i<y-1;i++)
pby=pby->next;
py=pby->next;
pay=py->next;
if((y-x)==1){
pbx->next=py;
py->next=px;
px->next=pay;
}
else{
pbx->next=py;
py->next=pax;
pby->next=px;
px->next=pay;
}
cout<<"Swap successfully!\n";
}
void Reverse(NODE *&head){
NODE *p1,*p2,*p3;
p3=p2=Initlist();
int len=Listlen(head);
while(len--){
p1=head;
for(int i=0;i<len+1;i++)
p1=p1->next;
p2->next=p1;
p2=p2->next;
}
p2->next=nullptr;
cout<<"The list has been reversed!\n";
head=p3;
}
void Print(NODE *head){
NODE *p;
p=head->next;
if(p){
cout<<"Output list: \n";
while(p){
cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
else
cout<<"List is empty!\n";
}
void Freelist(NODE *head){
NODE *p;
while(head){
p=head;
head=head->next;
delete p;
}
}
void Menu(){
NODE *head;
head=Initlist();
char c;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Welcome to the startmenu!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
while(1){
cout<<"Please choose a selection!(C:Create / I:Insert / S:Search / D:Delete / W:Swap / R:Reverse / P:Print / F:Free / E:Exit): \n";
cin>>c;
switch(toupper(c)){
case 'C':
if(head->next)
cout<<"List already exist!Please free list first!\n";
else{
cout<<"'T'--[Tail-Create], 'H'--[Head-Create] : ";
cin>>c;
if(toupper(c)=='T')
head=tailCreate();
else if(toupper(c)=='H')
head=headCreate();
}
break;
case 'I':
int loc,num;
cout<<"Please input the Location(1~"<<Listlen(head)+1<<") and the number[Stop=(-1)]:\n";
cin>>loc;
while(loc!=-1){
cin>>num;
if(loc<1 || loc>Listlen(head)+1)
cout<<"Location is out of the list's lenth!"<<endl;
else
Insert(loc,num,head);
cout<<"Please input the Location(1~"<<Listlen(head)+1<<") and the number[Stop=(-1)]:\n";
cin>>loc;
}
break;
case 'S':
if(head->next){
cout<<"'L'--[Location-Search], 'N'--[Number-Search] : ";
cin>>c;
if(toupper(c)=='L'){
cout<<"Please input the Location(1~"<<Listlen(head)<<"): "<<endl;
int loc;
cin>>loc;
if(loc<1 || loc>Listlen(head))
cout<<"Location is out of the list's lenth!"<<endl;
else
locSearch(loc,head);
}
else if(toupper(c)=='N')
numSearch(head);
}
else
cout<<"List is empty!Please Insert first!\n";
break;
case 'D':
if(head->next){
cout<<"'L'--[Location-Delete], 'N'--[Number-Delete] : ";
cin>>c;
if(toupper(c)=='L'){
cout<<"Please input the Location(1~"<<Listlen(head)<<"): "<<endl;
int loc;
cin>>loc;
if(loc<1 || loc>Listlen(head))
cout<<"Location is out of the list's lenth!"<<endl;
else
locDelete(loc,head);
}
else if(toupper(c)=='N')
numDelete(head);
}
else
cout<<"List is empty!Please Insert first!\n";
break;
case 'W':
int x,y;
cout<<"Please input two location you want to swap: "<<endl;
cin>>x>>y;
if(x>y)
x^=y^=x^=y;
if(x<1 || y>Listlen(head))
cout<<"Location is out of the list's lenth!"<<endl;
else{
if(x==y)
cout<<"Swap successfully!\n";
else
Swap(x,y,head);
}
break;
case 'R':
if(head->next){
if(Listlen(head)==1)
cout<<"List has been reversed!\n";
else
Reverse(head);
}
else
cout<<"List is empty!Please Insert first!\n";
break;
case 'P':
if(head->next)
Print(head);
else
cout<<"List is empty!Please Insert first!\n";
break;
case 'F':
if(head->next){
cout<<"'Y'--[free the list], 'N'--[quit to the menu] : ";
cin>>c;
if(toupper(c)=='Y'){
Freelist(head);
head=Initlist();
cout<<"List has been freed!\n";
}
}
else
cout<<"List is empty!Please Insert first!\n";
break;
case 'E':
Freelist(head);
exit(0);
}
}
}
int main(){
Menu();
return 0;
}