码迷,mamicode.com
首页 > 其他好文 > 详细

[BS]线性表之链表基本操作

时间:2015-03-11 17:13:44      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

线性表分为顺序表和链表。


链表的基本操作如下:


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

#define Error( Str )        FatalError( Str )
#define FatalError( Str )   fprintf( stderr, "%s\n", Str ), exit( 1 )


struct Node
{
    ElementType Element;
    Position    Next;
};
/* Make a list empty */
List
MakeEmpty( List L )
{
    if( L != NULL )
        DeleteList( L );
    L = malloc( sizeof( struct Node ) );
    if( L == NULL )
        FatalError( "Out of memory!" );
    L->Next = NULL;
    return L;
}

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

/* Return true if P is the last position int 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;
    return P;
}

/* Delete first occurence 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 ) )/* Assumption of header use */
	{					 /* X is found ; delete it*/
		TmpCell = P->Next;
		P->Next = TmpCell->Next;
		free( TmpCell);
	}
}

/* If X is not found, then Next field of returned */
/* Position is NULL */
/* Assumes a header */

Position
FindPrevious(ElementType 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 assumed */
/* Parameter L is unused in this implementation */
void
Insert( ElementType X, List L, Position P )
{
	Position TmpCell;

	TmpCell = malloc( sizeof( struct Node ) );
	if( TmpCell == NULL )
		FatalError("Out of space!!!");
	TmpCell->Element = X;
	TmpCell->Next = P->Next;
	P->Next = TmpCell;
}

/* Incorrect DeleteList algorithm */
/*void
DeleteList( List L )
{
    Position P;

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

/* Correct DeleteList algorithm */
void
DeleteList( List L )
{
    Position P,Tmp;

    P = L->Next;
    L->Next = NULL;
    while( P != NULL )
    {
        Tmp = P->Next;
        free(P);
        P = Tmp;
    }
}

/* Position Header */
Position
Header(List L)
{
    return L;
}

/* Return the First Position Node of list L */
Position
First(List L)
{
    return L->Next;
}

/* Return the Advance of P */
Position
Advance(Position P)
{
    return P->Next;
}

/* Retrive the element of the position P */
ElementType
Retrieve(Position P)
{
    return P->Element;
}

/* Output all elements of list */
void
OutputAllElements( List L )
{
    Position P;
    P = L;
    while( P->Next != NULL )
    {
        printf( "%c ",Retrieve( P->Next ) );
        P = P->Next;
    }
}

/* Insert initial elements of list */
void
InsertInitialElements( ElementType *X, List L ,int n )
{
    while( n-- )
    {
        Insert( *X, L, L );
        *X = *X - 1;
    }
}
/* Main */
int
main(void)
{
    ElementType i = 'z';
    List La = malloc( sizeof( struct Node ) );
    La->Next = NULL;
    MakeEmpty( La );
    InsertInitialElements( &i, La, 26 );
    OutputAllElements( La );
    return 0;
}

Result :

技术分享

part of list.h

struct Node;/* define ? */
typedef struct Node *PtrToNode;/* 用PtrToNode 代替 struct Node * */
typedef PtrToNode List;/* 用List 代替 PtrToNode */
typedef PtrToNode Position;/* 用Position 代替 PtrToNode */
typedef char    ElementType;/* 用ELementType 代替 char */

[BS]线性表之链表基本操作

标签:

原文地址:http://blog.csdn.net/oimchuan/article/details/44200485

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!