<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">一、链表</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">一、链表</span>
链表是由一个个的链表节点按一定的顺序连接而成的一种数据结构。节点中包含数据域和指针域,数据域是用来保存数据的,指针域是用来保存下一个节点的地址的指针。链表可以有一个标准结点,即表头,标志结点可以使链表的操作更加方便,表头中不含数据,只有指针域。
#ifndef _List_H
#define _List_H
struct Node;
typedef struct Node* PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(Positon P,List L);
Position Find(ElementType X, List L);
void Insert(Element X, List L, Position P);
void Delete(Element X, List L);
Position FindPrevious(ElementType X, Lkist L);
void DeleteList(List L);
Position Header(List L);
Position First(List L);
Position Advance(Position P);
ElementType Retrieve(Position P);
#endif /* _List_H */
/* Place in the implementation file*/
struct Node
{
ElementType Element;
Position Next;
};
/*Return true if L is empty*/
int
IsEmpty(List L)
{
return L->Next == NULL;
}
/*Return true if P is the last position in the list L.Parameter L is unused in this implementation */
int
IsLast(Position P, List L)
{
return P->Next == NULL;
}
/*Return Position of x in L, NULL if not found*/
Position
Find(ElementType X, List L)
{
Position P;
P = L->Next;
while(p != NULL && P->Element != X)
P = P->Next;
</pre><pre name="code" class="cpp">return P;
}
/*Delete first occurrence of X from a list
Assume use of a header node*/
void
Delete(ElementType X, List L)
{
Position P, TmpCell;
p = FindPrevious(X, L);
if(!IsLast(P,L)
{
TmpCell = P->Next;
P->Next = TmpCell->Next;
free(TmpCell);
}
}
Position
Header(List L)
{
Position header;
header = new Position;
return header;
}
/*If X is not found, then Next field of returned Position is NULL
Assumes a header*/
Position
FindPrevious(ElementType X, List L)
{
Position P;
</pre><pre name="code" class="cpp">P = L;
while(P->Next != NULL && P->Next->Element != X)
P = p->Next;
</pre><pre name="code" class="cpp">return P;
}
/*Insert (after legal position P)
Header implementation assumed
Parameter L is unused in this implementation*/
void
Insert(ElementType X, List L, Position P)
{
Position TmpCell;
</pre><pre name="code" class="cpp">TmpCell = malloc(sizeof(struct Node));
if(TmpCell == NULL)
cout<<"out of space";
</pre><pre name="code" class="cpp">TmpCell->Next = P->Next;
P->Next = TmpCell;
TmpCell->Element = X;
}
void
DeleteList(List L)
{
Position P , Tmp;
</pre><pre name="code" class="cpp">P = L->Next; //Header Assumed
L->Next = NULL:
while(P != NULL)
{
Tmp = P->Next;
free(P);
P = Tmp;
}
}