#include <iostream>
#include <malloc.h>
using namespace std;
struct celltype
{
int element ;
struct celltype *next ;/*结点型*/
};
typedef struct celltype *LIST; /*线性表的型*/
typedef struct celltype *position; /*位置型*/
void Insert(int x,position p,LIST &L)
{
position q;
q=new celltype;
q->element = x;
q->next = p->next;
p->next = q;
}
void Delete (position p,LIST &L)
{
position q;
if(p->next!=NULL)
{
q=p->next;
p->next = q->next;
delete q;
}
}
position Locate(int x,LIST &L)
{
position p ;
p = L ;
while ( p->next != NULL )
if ( p->next->element == x )
return p ;
else
p = p->next ;
return p ;
}
int Retrieve(position p,LIST &L)
{
return p->next->element;
}
position Previous(position p,LIST &L)
{
position q ;
if ( p ==L->next )
cout<<"不存在前驱位置";
else
{
q = L ;
while (q->next!=p)
q =q->next;
return q ;
}
}
position Next(position p,LIST &L)
{
position q ;
if (p->next == NULL)
cout<<"不存在后继位置";
else
{
q = p->next;
return q ;
}
}
position MakeNull(LIST &L)
{
L = new celltype;
L->next= NULL;
return L ;
}
position First(LIST L)
{
return L;
}
position End(LIST L)
{
position q;
q = L;
while(q->next!=NULL)
q=q->next;
return q;
}
int main()
{
struct celltype *L;
struct celltype *Head;
L = (celltype *)malloc(sizeof(celltype));
Head = (celltype *)malloc(sizeof(celltype));
position p;
Head->next = L;
L->next = NULL;
p = L;
Insert(2,p,L);
p = Locate(2,L);
p = p->next;
Insert(3,p,L);
Insert(4,Locate(2,L),L);
Insert(5,Locate(2,L),L);
Insert(6,Locate(4,L),L);
p = Locate(3,L);
p = p->next;
Insert(9,p,L);
position a;
a = L;
cout<<"插入结果为:"<<endl;
while(a->next!=NULL)
{
cout<<Retrieve(a,L)<<" ";
a = a->next;
}
cout<<endl;
position b;
b = L;
cout<<"返回L的第一个元素:"<<endl;
cout<<Retrieve(b,L)<<endl;
cout<<"从L中删除4:"<<endl;
Delete(Locate(4,L),L);
a = L;
while(a->next!=NULL)
{
cout<<Retrieve(a,L)<<" ";
a = a->next;
}
cout<<endl;
cout<<"返回L中3的位置:"<<endl;
cout<<Locate(3,L)<<endl;
cout<<"返回L中3的下一个元素:"<<endl;
a = Locate(3,L)->next;
cout<<Retrieve(a,L)<<endl;
cout<<"返回L的最后一个元素:"<<endl;
cout<<Retrieve(a,L)<<endl;
cout<<"删除L中的最后一个元素:"<<endl;
Delete(a,L);
a = L;
while(a->next!=NULL)
{
cout<<Retrieve(a,L)<<" ";
a = a->next;
}
cout<<endl;
cout<<"删除L中的第一个元素:"<<endl;
a = L;
Delete(a,L);
while(a->next!=NULL)
{
cout<<Retrieve(a,L)<<" ";
a = a->next;
}
cout<<endl;
cout<<"返回L中的第2个位置对应的元素:"<<endl;
a = L;
a = a->next;
cout<<Retrieve(a,L)<<endl;
return 0;
}
#include <malloc.h>
using namespace std;
struct celltype
{
int element ;
struct celltype *next ;/*结点型*/
};
typedef struct celltype *LIST; /*线性表的型*/
typedef struct celltype *position; /*位置型*/
void Insert(int x,position p,LIST &L)
{
position q;
q=new celltype;
q->element = x;
q->next = p->next;
p->next = q;
}
void Delete (position p,LIST &L)
{
position q;
if(p->next!=NULL)
{
q=p->next;
p->next = q->next;
delete q;
}
}
position Locate(int x,LIST &L)
{
position p ;
p = L ;
while ( p->next != NULL )
if ( p->next->element == x )
return p ;
else
p = p->next ;
return p ;
}
int Retrieve(position p,LIST &L)
{
return p->next->element;
}
position Previous(position p,LIST &L)
{
position q ;
if ( p ==L->next )
cout<<"不存在前驱位置";
else
{
q = L ;
while (q->next!=p)
q =q->next;
return q ;
}
}
position Next(position p,LIST &L)
{
position q ;
if (p->next == NULL)
cout<<"不存在后继位置";
else
{
q = p->next;
return q ;
}
}
position MakeNull(LIST &L)
{
L = new celltype;
L->next= NULL;
return L ;
}
position First(LIST L)
{
return L;
}
position End(LIST L)
{
position q;
q = L;
while(q->next!=NULL)
q=q->next;
return q;
}
int main()
{
struct celltype *L;
struct celltype *Head;
L = (celltype *)malloc(sizeof(celltype));
Head = (celltype *)malloc(sizeof(celltype));
position p;
Head->next = L;
L->next = NULL;
p = L;
Insert(2,p,L);
p = Locate(2,L);
p = p->next;
Insert(3,p,L);
Insert(4,Locate(2,L),L);
Insert(5,Locate(2,L),L);
Insert(6,Locate(4,L),L);
p = Locate(3,L);
p = p->next;
Insert(9,p,L);
position a;
a = L;
cout<<"插入结果为:"<<endl;
while(a->next!=NULL)
{
cout<<Retrieve(a,L)<<" ";
a = a->next;
}
cout<<endl;
position b;
b = L;
cout<<"返回L的第一个元素:"<<endl;
cout<<Retrieve(b,L)<<endl;
cout<<"从L中删除4:"<<endl;
Delete(Locate(4,L),L);
a = L;
while(a->next!=NULL)
{
cout<<Retrieve(a,L)<<" ";
a = a->next;
}
cout<<endl;
cout<<"返回L中3的位置:"<<endl;
cout<<Locate(3,L)<<endl;
cout<<"返回L中3的下一个元素:"<<endl;
a = Locate(3,L)->next;
cout<<Retrieve(a,L)<<endl;
cout<<"返回L的最后一个元素:"<<endl;
cout<<Retrieve(a,L)<<endl;
cout<<"删除L中的最后一个元素:"<<endl;
Delete(a,L);
a = L;
while(a->next!=NULL)
{
cout<<Retrieve(a,L)<<" ";
a = a->next;
}
cout<<endl;
cout<<"删除L中的第一个元素:"<<endl;
a = L;
Delete(a,L);
while(a->next!=NULL)
{
cout<<Retrieve(a,L)<<" ";
a = a->next;
}
cout<<endl;
cout<<"返回L中的第2个位置对应的元素:"<<endl;
a = L;
a = a->next;
cout<<Retrieve(a,L)<<endl;
return 0;
}