You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
3.8 KiB

1 year ago
#include<iostream>
using namespace std;
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int status;
typedef int ElemType;
//<2F>ýṹ<C3BD><EFBFBD><E5B6A8>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
typedef struct LNode
{
ElemType data; //<2F><EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
struct LNode *next; //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><E1B9B9>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
}LNode,*Linklist; //<2F><><EFBFBD><EFBFBD><EFB6A8><EFBFBD><EFBFBD> Linklist ==LNode *;
//---------<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>------------
//һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
status Initlist(Linklist &L,int n)
{
Linklist q,p;
L=(Linklist)malloc(sizeof(LNode)); //Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><E3A3AC>ע<EFBFBD><D7A2>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><E3B2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
L->next=NULL; //ͷ<><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
q=L; //<2F><><71><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>βָ<CEB2>
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>"<<endl;
for(int i=0;i<n;i++)
{
p=(Linklist)malloc(sizeof(LNode));
cin>>p->data;
q->next=p; //<2F><>q<EFBFBD><71>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>p
q=p; //<2F><>p<EFBFBD><70>Ϊ<EFBFBD>µ<EFBFBD>q
}
q->next=NULL;
return OK;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
status Insertlist(Linklist &L,int i,ElemType e)
{
Linklist p,s;
int j=0;
p=L;
while(p&&j<i-1)
{
p=p->next;
j++;
} //<2F><>whileѭ<65><D1AD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2>Ҳ<EFBFBD><D2B2><EFBFBD>λ<EFBFBD>õ<EFBFBD>ǰһ<C7B0><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(!p||j>i-1)
return ERROR; //<2F><>if<69><66><EFBFBD><EFBFBD><EFBFBD>жϲ<D0B6><CFB2><EFBFBD><EFBFBD>Ƿ<EFBFBD>ʧ<EFBFBD><CAA7>
s=(Linklist)malloc(sizeof(LNode)); //Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷ<EFBFBD><DDB7><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if(!s)
return ERROR;
s->data=e;
s->next=p->next; //<2F><>p<EFBFBD><70>ָ<EFBFBD><D6B8><EFBFBD>򴫸<EFBFBD>s<EFBFBD><73>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
p->next=s; //Ȼ<><C8BB><EFBFBD><EFBFBD>p<EFBFBD><70>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>s<EFBFBD><73><EFBFBD><EFBFBD>
return OK;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>
status Deletelist(Linklist &L,int i,ElemType &e)
{
Linklist p,q;
int j=0;
p=L;
while(p&&j<i-1)
{
p=p->next;
j++;
}
if(!(p->next)||j>i-1) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>p<EFBFBD><70><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
return ERROR;
q=p->next; //<2F><>Ҫɾ<D2AA><C9BE><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><71><D6B8>
e=q->data; //<2F><>e<EFBFBD><65><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
p->next=q->next; //<2F><>q<EFBFBD><71>ָ<EFBFBD><D6B8><EFBFBD>򴫸<EFBFBD>p;
free(q);
return OK;
}
//<2F>ġ<EFBFBD><C4A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
status GetElem(Linklist L,int e,int &i)
{
Linklist p=L->next;
if(p==NULL)
return ERROR;
i=1;
while(p)
{
if(p->data==e)
{
return i; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
}
p=p->next; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʋ<EFBFBD><C6B2><EFBFBD>
i++;
}
}
//<2F><EFBFBD><E5A1A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
status Printf(Linklist L)
{
Linklist p;
p=L->next; //<2F><>p<EFBFBD><70>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(p)
{
cout<<p->data<<" ";
p=p->next; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA><EFBFBD>p<EFBFBD><70><EFBFBD><EFBFBD>һλ
}
cout<<endl;
return OK;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD>ͷ<EFBFBD><EFBFBD><E5B7A8>
status Reservelist(Linklist &L)
{
Linklist p,q; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>룬p<EBA3AC>ǵ<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͽ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>룬ָ<EBA3AC><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
p=L->next; //<2F><>q<EFBFBD><71><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>p<EFBFBD><70><EFBFBD>ơ<EFBFBD>
L->next=NULL; //<2F><>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӶϿ<D3B6>
while(p)
{
q=p->next; //q<><71>Ϊ<EFBFBD><CEAA>ָ<EFBFBD><D6B8>ʣ<EFBFBD><CAA3>δ<EFBFBD><CEB4><EFBFBD>õ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>ַ
p->next=L->next;
L->next=p; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD><C7BD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>
p=q; //Ȼ<><C8BB><EFBFBD><EFBFBD>q<EFBFBD><71><EFBFBD><EFBFBD>p;
}
return OK;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
status Clearlist(Linklist &L)
{
Linklist p,q;
p=L->next;
while(p)
{
p=q;
q=p->next; //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><71><D6B8>ʣ<EFBFBD>µĽ<C2B5><C4BD>㣬֮<E3A3AC><D6AE>һ<EFBFBD><D2BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ա<EFBFBD>
free(p);
}
L->next=NULL;
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"<<endl;
return OK;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
status Destroylist(Linklist &L)
{
Linklist p;
while(L)
{
p=L;
L=L->next;
free(p);
}
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>١<EFBFBD>"<<endl;
return OK;
}
int main()
{
Linklist L;
int n,i;
ElemType e;
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD>ȣ<EFBFBD>"<<endl;
cin>>n;
Initlist(L,n);
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>"<<endl;
Printf(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D><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>"<<endl;
Reservelist(L);
Printf(L);
Clearlist(L);
Destroylist(L);
return OK;
}