diff options
| author | Steve Lee <me@xiangyangli.com> | 2017-04-19 22:43:18 +0800 |
|---|---|---|
| committer | Steve Lee <me@xiangyangli.com> | 2017-04-19 22:43:18 +0800 |
| commit | 9ee91c759c5a87030cebb6b79adc94230f23da4a (patch) | |
| tree | 4184793354ac57cdbf7002e6821c301bc24ad44a /DSAA/chap3_lists_stacks_queues/list.c | |
| parent | ce07c94e0ec62a829e0d0c447ec4c932f0f78c3d (diff) | |
| download | Personal-9ee91c759c5a87030cebb6b79adc94230f23da4a.tar.xz Personal-9ee91c759c5a87030cebb6b79adc94230f23da4a.zip | |
remove
Diffstat (limited to 'DSAA/chap3_lists_stacks_queues/list.c')
| -rw-r--r-- | DSAA/chap3_lists_stacks_queues/list.c | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/DSAA/chap3_lists_stacks_queues/list.c b/DSAA/chap3_lists_stacks_queues/list.c new file mode 100644 index 0000000..23848bc --- /dev/null +++ b/DSAA/chap3_lists_stacks_queues/list.c @@ -0,0 +1,145 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "list.h" + +/* Return true if L is empty */ + +int +IsEmpty( List L ) +{ + return L->Next == NULL; +} + +/* Return true if P is the last position in 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 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 ); + } +} + +/* 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. + * Return the position of the inserted node */ + +Position +Insert( ElementType X, List L, Position P ) +{ + Position TmpCell; + + TmpCell = malloc( sizeof( struct Node ) ); + if( TmpCell == NULL ) + printf( "Out of space!" ); + + TmpCell->Element = X; + TmpCell->Next = P->Next; + P->Next = TmpCell; + + return TmpCell; +} + +void +DeleteList( List L ) +{ + Position P, Tmp; + + P = L->Next; + L->Next = NULL; + + while( P != NULL ) + { + Tmp = P->Next; + free( P ); + P = Tmp; + } +} + +void ConstructList( List L, int Elements[], int Num ) +{ + int i; + + for( i = 0; i < Num; i++ ) + { + Insert(Elements[i], L, L); + } +} +void +PrintList( List L ) +{ + Position P; + P = L->Next; + + while( P != NULL ) + { + printf( "%d->", P->Element ); + P = P->Next; + } + + printf( "\n" ); +} + +/*int*/ +/*main()*/ +/*{*/ + /*Position P;*/ + /*List L;*/ + + /*L = malloc( sizeof (struct Node) );*/ + /*Insert(3,L, L);*/ + /*Insert(5,L, L);*/ + /*Insert(8, L, L);*/ + /*P = FindPrevious(3, L);*/ + /*PrintList(L);*/ +/*}*/ |
