一个线性表的指针实现,还有数组实现和游标实现没有做,不过思想一样.
/* list */
#include<iostream.h>
/*pointer*/
#define null 0
typedef int elementtype;
struct p_list
{
elementtype data;
p_list* next;
};
typedef p_list* List1;
typedef p_list* position;
/*define end*/
position END(List1);
void INSERT(elementtype x,List1 L)
{
List1 temp,last;
temp=new p_list;
temp->data=x;
temp->next=null;
last=END(L);
last->next=temp;
}
position LOCATE(elementtype x,List1 L)
{
while(L!=null)
{
if(x==L->data)return L;
L=L->next;
}
return null;
}
void DELETE(List1 p,List1 L)
{
if(END(L)==null)return;
position temp,tt;
temp=L;
while(temp->next!=null)
{
if(temp->next->data==p->data)
{
tt=temp->next;
temp->next=tt->next;
delete tt;
return;
}
else temp=temp->next;
}
}
/*
List1 NEXT(List1 p,List1 L)
{
}
*/
position END(List1 L)
{
position pp;
pp=L;
if(pp==null)return null;
while(pp->next!=null) pp=pp->next;
return pp;
}
void MAKENULL(List1 &L)
{
L=new p_list;
L->data=111;
L->next=null;
}
void printList(List1 L)
{
cout<<"show the LIST"<<endl;
while(L!=null)
{
cout<<L->data<<",";
L=L->next;
}
}
void main()
{
cout<<endl<<"BEGIN"<<endl;
List1 head;
MAKENULL(head);
INSERT(555,head);
INSERT(666,head);
//printList(head->next);
//position temp=LOCATE(666,head);
//if(temp!=null)cout<<temp->data<<endl;
List1 p=new p_list;
p->data=666;
printList(head->next);
DELETE(p,head);
printList(head->next);
}
#include<iostream.h>
/*pointer*/
#define null 0
typedef int elementtype;
struct p_list
{
elementtype data;
p_list* next;
};
typedef p_list* List1;
typedef p_list* position;
/*define end*/
position END(List1);
void INSERT(elementtype x,List1 L)
{
List1 temp,last;
temp=new p_list;
temp->data=x;
temp->next=null;
last=END(L);
last->next=temp;
}
position LOCATE(elementtype x,List1 L)
{
while(L!=null)
{
if(x==L->data)return L;
L=L->next;
}
return null;
}
void DELETE(List1 p,List1 L)
{
if(END(L)==null)return;
position temp,tt;
temp=L;
while(temp->next!=null)
{
if(temp->next->data==p->data)
{
tt=temp->next;
temp->next=tt->next;
delete tt;
return;
}
else temp=temp->next;
}
}
/*
List1 NEXT(List1 p,List1 L)
{
}
*/
position END(List1 L)
{
position pp;
pp=L;
if(pp==null)return null;
while(pp->next!=null) pp=pp->next;
return pp;
}
void MAKENULL(List1 &L)
{
L=new p_list;
L->data=111;
L->next=null;
}
void printList(List1 L)
{
cout<<"show the LIST"<<endl;
while(L!=null)
{
cout<<L->data<<",";
L=L->next;
}
}
void main()
{
cout<<endl<<"BEGIN"<<endl;
List1 head;
MAKENULL(head);
INSERT(555,head);
INSERT(666,head);
//printList(head->next);
//position temp=LOCATE(666,head);
//if(temp!=null)cout<<temp->data<<endl;
List1 p=new p_list;
p->data=666;
printList(head->next);
DELETE(p,head);
printList(head->next);
}