#include <iostream>
#include "kudetouwenjian.h"
using namespace std;
int main()
{
linknode *L;
A a[5]={1,2,3,4,5};
int n=5,b=4,c=3;
A e;
toucha(L,a,n);
shuchu(L);
cout<<endl;
cuihui(L);
shuchu(L);
cout<<"(被摧毀的連結串列輸出的空白。)"<<endl;
cout<<endl;
cout<<panduanshifouweikong(L)<<endl;;
cout<<"(如果為空輸出1,不為空輸出0.)"<<endl;
cout<<endl;
weicha(L,a,n);
shuchu(L);
cout<<endl;
cout<<"該連結串列的長度為:"<<changdu(L)<<endl;
cout<<endl;
shunxuchazhao(L,b,e);
cout<<"順序查詢的第四個為:"<<e<<endl;
cout<<endl;
e=3;
cout<<"查詢3,如果在連結串列中找到3,輸出這1,如果沒有,輸出0"<<endl;
cout<<chazhaodange(L,e)<<endl;
cout<<endl;
e=0;
cout<<"在第三個位置差入一個0"<<endl;
charudange(L,c,e);
shuchu(L);
return 0;
}
#include <iostream>
#include <malloc.h>
#include "kudetouwenjian.h"
using namespace std;
void toucha(linknode *&L,A a[],int n)
{
linknode *s;
L=(linknode *)malloc(sizeof(linknode));
L->next=NULL;
for(int i=0;i<n;i++)
{
s=(linknode *)malloc(sizeof(linknode));
s->shuju=a[i];
s->next=L->next;
L->next=s;
}
}
void shuchu(linknode *L)
{
linknode *p=L->next;
while(p!=NULL)
{
cout<<p->shuju<<" ";
p=p->next;
}
cout<<endl;
}
void weicha(linknode *&L,A a[],int n)
{
linknode *s,*r;
L=(linknode *)malloc(sizeof(linknode));
r=L;
for(int i=0;i<n;i++)
{
s=(linknode *)malloc(sizeof(linknode));
s->shuju=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
void chushihua(linknode *&L)
{
L=(linknode *)malloc(sizeof(linknode));
L->next=NULL;
}
void cuihui(linknode *&L)
{
linknode * pre=L,* p=L->next;
while(p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
L->next=NULL;
}
int panduanshifouweikong(linknode *L)
{
return(L->next==NULL);
}
int changdu(linknode *L)
{
int e=0;
linknode *p=L->next;
while(p!=NULL)
{
e++;
p=p->next;
}
return(e);
}
int shunxuchazhao(linknode * L,int i,A &e)
{
int j=1;
linknode *p=L->next;
if(i<=0)j=j;;
while(j<i)
{
j++;
p=p->next;
}
if(p==NULL)
j=j;
else
{
e=p->shuju;
return(e);
}
}
int chazhaodange(linknode *L,A e)
{
int j=1;
linknode *p=L->next;
if(p==NULL)
return 0;
while(p!=NULL&&e!=p->shuju)
{
p=p->next;
j++;
}
if(p==NULL)
return 0;
else
return(1);
}
bool charudange(linknode *&L,int i,A e)
{
int j=0;
linknode * p=L,* s;
if(i<=0)return false;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=(linknode *)malloc(sizeof(linknode));
s->shuju=e;
s->next=p->next;
p->next=s;
}
}
#ifndef KUDETOUWENJIAN_H_INCLUDED
#define KUDETOUWENJIAN_H_INCLUDED
#include <iostream>
typedef int A;
typedef struct lianbiao
{
A shuju;
struct lianbiao *next;
}linknode;
void toucha(linknode *&L,A a[],int n);
void shuchu(linknode *L);
void weicha(linknode *&L,A a[],int n);
void chushihua(linknode *&L);
void cuihui(linknode *&L);
int panduanshifouweikong(linknode *L);
int changdu(linknode *L);
int shunxuchazhao(linknode *L,int i,A &e);
int chazhaodange(linknode *L,A e);
bool charudange(linknode *&L,int i,A e);
#endif // KUDETOUWENJIAN_H_INCLUDED