读书人

有关了链表的实现请大家帮忙看一下,

发布时间: 2012-03-03 15:33:04 作者: rapoo

有关了链表的实现,请大家帮忙看一下
#ifndef _List_H

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(List L);
Position Find(int X,List L);
void Delete(int X,List L);
Position FindPrevious(int X,List L);
void DeleteList(List L);
Position Header(List L);
Position First(List L);
Position Advance(Position P);
int Retrieve(Position P);

#endif

struct Node
{
int Element;
Position Next;
};

#include "list.h "
#include <stdio.h>
#include <stdlib.h>


void main()
{
Position Insert(int X,List L,Position P);
int PrintList(List L);
Position P,L,Head;
int C;
Head=(Position)malloc(sizeof(struct Node));
Head-> Next =NULL;
L=Head;
P=L;
while((C=getchar())!=EOF)
{
Insert(C,L,P);
}
PrintList(Head);
}


//return true if L is empty
int IsEmpty(List L)
{
return L-> Next==NULL;
}

//return true if P is thr last position in list L

int IsLast(Position P,List L)
{
return P-> Next==NULL;
}

//return position of X in L;NULL if not found

Position Find(int X,List L)
{
Position P;
P=L-> Next;
while(P!=NULL&&P-> Element!=X)
P=P-> Next;
return P;
}


//delete first occurrence 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))
{
TmpCell=P-> Next;
P-> Next=TmpCell-> Next;
free(TmpCell);
}
}


//if X is not found ,then next field of returned
//postion is NULL
//assumes a header

Position FindPrevious(int X,List L)
{
Position P;

P=L;
while(P-> Next!=NULL&&P-> Next-> Element!=X)
P=P-> Next;
return P;
}


//insert (after legal position P)
//header implementation assued
//parameter L is unused in this implementation

void Insert(int X,List L,Position P)
{
Position TmpCell;
TmpCell=(Position)malloc(sizeof(struct Node));
if(TmpCell==NULL)
printf( "out of space!!!!! ");
TmpCell-> Element=X;
TmpCell-> Next=P-> Next;
P-> Next=TmpCell;
}


//delete all list


void DeleteList(List L)
{
Position P,Tmp;
P=L-> Next;
L-> Next=NULL;
while(P!=NULL)
{
Tmp=P-> Next;
free(P);
P=Tmp;
}
}


//print a list
//header implementtation assued

void PrintList(List Head)
{
Position P;
P=Head;
if(P!=NULL)
do
{
printf( "%d ",P-> Element);
P=P-> Next;
}while(P!=NULL);

}


[解决办法]
定义,声明形式不一致

读书人网 >软件架构设计

热点推荐