代码的主体来自于《Data Structures and Algorithm Analysis in C (Second Edition)》,我在书中代码的基础上进行了一些修改与补充。
修改 补充 说明:
1. 添加了Last变量,代表链表的最后一个元素的下标,使IsEmpty( )等函数的实现更为直观,快捷。
2. 增加了FindPrevious,Print,Out三个函数,使ADT更为完善。
3. 修改了Insert()函数,使元素自动按递增插入。
4. LIst L代表链表的第一个节点,即头节点。
5. main()函数中对Insert,IsEmpty,Delete,Print等函数均进行了测试。
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int list_size = 15;
typedef int PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node{
int ele;
Position Next;
};
struct Node CursorSpace[ list_size ];
Position Last;
void initialize()
{
int i;
for(i = 0; i < list_size - 1; i++)
CursorSpace[ i ].Next = i + 1;
CursorSpace[ i ].Next = 0;
for(int i = 0; i < list_size; i++)
CursorSpace[ i ].ele = -100;
}
Position Cursoralloc( void )
{
Position P;
P = CursorSpace[ 0 ].Next;
CursorSpace[ 0 ].Next = CursorSpace[P].Next;
return P;
}
void CursorFree( Position P )
{
CursorSpace[ P ].Next = CursorSpace[ 0 ].Next;
CursorSpace[ 0 ].Next = P;
}
/* Return true if L is empty */
bool IsEmpty( List L )
{
return L == Last;
}
/* Return true if P is the last position in list L */
/* Parameter L is unused in this implementation */
bool IsLast( Position P, List L )
{
return CursorSpace[ P ].Next == 0;
}
/* Return Position of X in L; 0 if not found */
/* Uses a header node */
Position Find( int X, List L )
{
Position P;
P = CursorSpace[ L ].Next;
while( P && CursorSpace[ P ].ele != X )
P = CursorSpace[ P ].Next;
return P;
}
int FindPrevious( int X, List L )
{
Position P = L;
while(CursorSpace[CursorSpace[ P ].Next].Next)
{
if(CursorSpace[CursorSpace[ P ].Next].ele == X)
break;
P = CursorSpace[ P ].Next;
}
return P;
}
/* Delete first occurence of X from a list */
/* Assume use of a header node */
void Delete( int X, List L )
{
Position P, TmpCell;
P = FindPrevious( X, L );
if( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found, delete it */
TmpCell = CursorSpace[ P ].Next;
if(TmpCell == Last)
Last = P;
CursorSpace[ P ].Next = CursorSpace[ TmpCell ].Next;
CursorFree( TmpCell );
}
}
/* Insert ( after a legal position P ) */
/* Header implementation assumed */
/* Parameter L is unused in this implementation */
void Insert( int X, List L )
{
Position TmpCell;
TmpCell = Cursoralloc( );
if( TmpCell == 0 )
printf(" Out of space !!!");
Position P = L;
while(CursorSpace[CursorSpace[ P ].Next].ele < X && P != Last)
{
P = CursorSpace[ P ].Next;
}
CursorSpace[ TmpCell ].ele = X;
if(P == Last)
Last = TmpCell;
else
CursorSpace[ TmpCell ].Next = CursorSpace[ P ].Next;
CursorSpace[ P ].Next = TmpCell;
}
/* print the linked list */
void print(List L)
{
Position P = CursorSpace[ L ].Next;
while( P!= Last )
{
printf("%d ",CursorSpace[ P ].ele);
P = CursorSpace[ P ].Next;
}
printf("%d ",CursorSpace[ P ].ele);
printf("\n");
}
/* print the whole list */
void out(List L)
{
for(int i = 0; i < list_size; i++)
printf("%d\t",i);
for(int i = 0; i < list_size; i++)
{
if (i == L)
{
printf("head\t");
continue;
}
printf("%d\t",CursorSpace[ i ].ele);
}
for(int i = 0; i < list_size; i++)
printf("%d\t",CursorSpace[ i ].Next);
printf("\n");
}
int main()
{
initialize();
List L = Cursoralloc();
Last = L;
if(IsEmpty( L ))
printf("empty\n");
Insert(5, L);
Insert(9, L);
Insert(6, L);
Insert(15, L);
Insert(7, L);
print(L);
out(L);
Delete(5, L);
Delete(7, L);
print(L);
return 0;
}