#include<bits/stdc++.h>
using namespace std;
#define Elemtype int
typedef struct node{
Elemtype data;
struct node* next;
}LNode,*LinkList;
void InitList(LNode* &L){
L=new LNode;
L->next=NULL;
}
void Hinput(LNode* &L,int n){
LNode *p;
for(int i=1;i<=n;i++){
cout<<"please input the "<<i<<"-th number"<<endl;
p=new LNode;
cin>>p->data;
p->next=L->next;
L->next=p;
}
}
void show(LNode* L){
LNode *p;
p=L->next;
while(p!=NULL){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int getLength(LNode *L){
int Len=0;
LNode *p;
p=L->next;
while(p!=NULL){
Len++;
p=p->next;
}
return Len;
}
void Tinput(LNode *L,int n){
LNode *p;
LNode *r;
r=L;
for(int i=1;i<=n;i++){
cout<<"please input the "<<i<<"-th number"<<endl;
p=new LNode;
cin>>p->data;
r->next=p;
r=p;
}
r->next=NULL;
}
void insertList(LNode* &L,int num){
LNode *p;
p=new LNode;
p->data=num;
p->next=NULL;
LNode *r;r=L;
while(r->next!=NULL){
r=r->next;
}
r->next=p;
}
void findData(LNode *L,int key){
LNode *p;
int loc=1;
p=L->next;
while(p !=NULL){
if(p->data==key){cout<<loc<<"-th is the location"<<endl;return;}
p=p->next;
loc++;
}
cout<<"no such data"<<endl;
return;
}
void insertt(LinkList L,Elemtype inserit,int loc){
LNode *p;
p=new LNode;
p->data=inserit;
LNode *r;
r=L;
int location=0;
while(r->next!=NULL){
if(location==loc-1){
p->next=r->next;
r->next=p;
return;
}
location++;
r=r->next;
}
}
void deleteit(LinkList L,int deleloc){
int location=0;
LNode *p;
LNode *r;
p=L;
while(p->next!=NULL){
if(location==deleloc-1){
r=p->next;
p->next=r->next;
free(r);
}
location++;
p=p->next;
}
}
void integrate(LinkList L1,LinkList L2){
LNode *p=L1;
while(p->next!=NULL){p=p->next;}
p->next=L2->next;
show(L1);
return;
}
int main(){
//LinkList head;
LNode *head;
while (true){
cout<<"\n0.exit\n1.init\n2.headAppend\n3.showList\n4.getTheLength\n5.tailAppend\n6.endInsert\n7.find\n8.insert\n9.delete\n10.integrate\n";
int choice;cin>>choice;
switch(choice){
case 0:
return 0;
break;
case 1:
cout<<"init\n";
InitList(head);
break;
case 2:
cout<<"please input the number of your inputs\n";
int num;cin>>num;
Hinput(head,num);
break;
case 3:
cout<<"show the list\n";
show(head);
break;
case 4:
int len;
cout<<"length\n";
len=getLength(head);
cout<<"Length is:"<<len<<endl;
break;
case 5:
cout<<"please input the number of your inputs\n";
cin>>num;
Tinput(head,num);
break;
case 6:
Elemtype da;
cout<<"insert the data\n";
cin>>da;
insertList(head,da);
break;
case 7:
Elemtype findit;
cout<<"input the data you wanna find"<<endl;
cin>>findit;
findData(head,findit);
break;
case 8:
cout<<"input the data and the location"<<endl;
Elemtype inserit;int loc;
cin>>inserit>>loc;
insertt(head,inserit,loc);
break;
case 9:
cout<<"input the data location you wanna delete"<<endl;
int deleloc;
cin>>deleloc;
deleteit(head,deleloc);
break;
case 10:
cout<<"integrate two linear list"<<endl;
LNode *head2;
InitList(head2);
cout<<"please input the number of your inputs\n";
cin>>num;
Hinput(head2,num);
show(head2);
cout<<"ready to integrate?(press any key)"<<endl;
int cho;cin>>cho;
integrate(head,head2);
break;
default:
cout<<"no that kind of choice"<<endl;
break;
}}
return 0;
}
数据结构C++链表:头插法、尾插法、获取长度、两个链表组合、删改查
最新推荐文章于 2024-08-24 14:01:36 发布