数据结构与算法分析 C语言描述 单链表的实现

/*头文件*/
#ifndef _List_H
#define _List_H

/* Place in the interface file */
typedef int ElementType;

typedef struct LNode
{
	ElementType Element;
	LNode    *Next;
}LNode, *pLinkList;

typedef pLinkList List;
typedef LNode* Node;

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

#endif    /* _List_H */
/* END */
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">/*源文件*/
#include "list.h"
#include <stdlib.h>
#include <stdio.h>

List MakeEmpty( List L )
{
	if( L != NULL )
		DeleteList( L );
	L = (List)malloc(sizeof(LNode));
	if( L == NULL )
		printf( "Out of memory!\n" );
	L->Next = NULL;

	return L;
}

/* START: fig3_8.txt */
/* Return true if L is empty */

bool IsEmpty( List L )
{
	return L->Next == NULL;
}
/* END */

/* START: fig3_9.txt */
/* Return true if P is the last position in list L */
/* Parameter L is unused in this implementation */

int IsLast( Node P, List L )
{
	return P->Next == NULL;
}
/* END */

/* START: fig3_10.txt */
/* Return Position of X in L; NULL if not found */

Node Find( ElementType X, List L )
{
	Node P;

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

	return P;
}
/* END */

/* START: fig3_11.txt */
/* Delete from a list */
/* Cell pointed to by P->Next is wiped out */
/* Assume that the position is legal */
/* Assume use of a header node */

void Delete( ElementType X, List L )
{
	Node P, TmpCell;

	P = FindPrevious( X, L );

	if( !IsLast( P, L ) )  /* Assumption of header use */
	{                      /* X is found; delete it */
		TmpCell = P->Next;
		P->Next = TmpCell->Next;  /* Bypass deleted cell */
		free( TmpCell );
	}
}
/* END */

/* START: fig3_12.txt */
/* If X is not found, then Next field of returned value is NULL */
/* Assumes a header */

Node FindPrevious( ElementType X, List L )
{
	Node P;

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

	return P;
}
/* END */

/* START: fig3_13.txt */
/* Insert (after legal position P) */
/* Header implementation assumed */
/* Parameter L is unused in this implementation */

void Insert( ElementType X, List L, Node P )
{
	Node TmpCell;

	TmpCell = (Node)malloc(sizeof(LNode));
	if( TmpCell == NULL )
	        printf( "Out of space!\n" );

	TmpCell->Element = X;
	TmpCell->Next = P->Next;
	P->Next = TmpCell;
}
/* END */

#if 0
/* START: fig3_14.txt */
/* Incorrect DeleteList algorithm */

void DeleteList( List L )
{
	Position P;

	/* 1*/      P = L->Next;  /* Header assumed */
	/* 2*/      L->Next = NULL;
	/* 3*/      while( P != NULL )
	{
		/* 4*/          free( P );
		/* 5*/          P = P->Next;
	}
}
/* END */
#endif

/* START: fig3_15.txt */
/* Correct DeleteList algorithm */

void DeleteList( List L )
{
	Node P, Tmp;

	P = L->Next;  /* Header assumed */
	L->Next = NULL;
	while( P != NULL )
	{
		Tmp = P->Next;
		free( P );
		P = Tmp;
	}
}
/* END */

Node Header( List L )
{
	return L;
}

Node First( List L )
{
	return L->Next;
}

Node Advance( Node P )
{
	return P->Next;
}

ElementType Retrieve( Node P )
{
	return P->Element;
}

int main()
{
	return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值