SeqList.h
# pragma once # include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <assert.h>
typedef int SLDataType;
typedef struct SeqList { SLDataType* a; int size; int capacity;
} SL;
void SeqListPrint ( SL* ps) ;
void SeqListInit ( SL* ps) ;
void SeqListDestory ( SL* ps) ;
void SeqListCheckCapacity ( SL* ps) ;
void SeqListPushBack ( SL* ps, SLDataType x) ;
void SeqListPopBack ( SL* ps) ;
void SeqListPushFront ( SL* ps, SLDataType x) ;
void SeqListPopFront ( SL* ps) ;
int SeqListFind ( SL* ps, SLDataType x) ;
void SeqListInsert ( SL* ps, int pos, SLDataType x) ;
void SeqListErase ( SL* ps, int pos) ;
void SeqListModify ( SL* ps, int pos, SLDataType x) ;
SeqList.c
# define _CRT_SECURE_NO_WARNINGS 1
# include "SeqList.h"
void SeqListPrint ( SL* ps)
{ for ( int i = 0 ; i < ps-> size; i++ ) printf ( "%d " , ps-> a[ i] ) ; printf ( "\n" ) ;
}
void SeqListInit ( SL* ps)
{ ps-> a = NULL ; ps-> size = ps-> capacity = 0 ;
}
void SeqListDestory ( SL* ps)
{ free ( ps-> a) ; ps-> a = NULL ; ps-> capacity = ps-> size = 0 ;
}
void SeqListCheckCapacity ( SL* ps)
{ if ( ps-> size == ps-> capacity) { int newcapacity = ps-> capacity == 0 ? 4 : ps-> capacity * 2 ; SLDataType* tmp = ( SLDataType* ) realloc ( ps-> a, newcapacity * sizeof ( SLDataType) ) ; if ( tmp == NULL ) { printf ( "AddSeqList::%s\n" , strerror ( errno) ) ; printf ( "realloc fail\n" ) ; exit ( - 1 ) ; } ps-> a = tmp; ps-> capacity = newcapacity; }
}
void SeqListPushBack ( SL* ps, SLDataType x)
{ SeqListCheckCapacity ( ps) ; ps-> a[ ps-> size] = x; ps-> size++ ;
}
void SeqListPopBack ( SL* ps)
{ assert ( ps-> size > 0 ) ; ps-> size-- ; }
void SeqListPushFront ( SL* ps, SLDataType x)
{ SeqListCheckCapacity ( ps) ; int end = ps-> size - 1 ; while ( end >= 0 ) { ps-> a[ end + 1 ] = ps-> a[ end] ; end-- ; } ps-> a[ 0 ] = x; ps-> size++ ;
}
void SeqListPopFront ( SL* ps)
{ assert ( ps-> size > 0 ) ; int begin = 1 ; while ( begin < ps-> size) { ps-> a[ begin - 1 ] = ps-> a[ begin] ; begin++ ; } ps-> size-- ;
}
int SeqListFind ( SL* ps, SLDataType x)
{ for ( int i = 0 ; i < ps-> size; i++ ) { if ( ps-> a[ i] == x) { printf ( "%d\n" , i) ; return 0 ; } } return - 1 ;
}
void SeqListInsert ( SL* ps, int pos, SLDataType x)
{ assert ( pos < ps-> size) ; SeqListCheckCapacity ( ps) ; int end = ps-> size - 1 ; while ( end >= pos) { ps-> a[ end + 1 ] = ps-> a[ end] ; end-- ; } ps-> a[ pos] = x; ps-> size++ ;
}
void SeqListErase ( SL* ps, int pos)
{ assert ( pos < ps-> size) ; int start = pos + 1 ; while ( start < ps-> size) { ps-> a[ start - 1 ] = ps-> a[ start] ; start++ ; } ps-> size-- ;
}
void SeqListModify ( SL* ps, int pos, SLDataType x)
{ assert ( pos < ps-> size) ; ps-> a[ pos] = x;
}
test.c
# define _CRT_SECURE_NO_WARNINGS 1 # include "SeqList.h" void TestSeqList1 ( )
{ SL s1; SeqListInit ( & s1) ; SeqListPushBack ( & s1, 1 ) ; SeqListPushBack ( & s1, 2 ) ; SeqListPushBack ( & s1, 3 ) ; SeqListPushBack ( & s1, 4 ) ; SeqListPushBack ( & s1, 5 ) ; SeqListPopBack ( & s1) ; SeqListPopBack ( & s1) ; SeqListPopBack ( & s1) ; SeqListPopBack ( & s1) ; SeqListPushBack ( & s1, 4 ) ; SeqListPushBack ( & s1, 5 ) ; SeqListPrint ( & s1) ; SeqListDestory ( & s1) ;
} void TestSeqList2 ( )
{ SL s1; SeqListInit ( & s1) ; SeqListPushBack ( & s1, 1 ) ; SeqListPushBack ( & s1, 2 ) ; SeqListPushBack ( & s1, 3 ) ; SeqListPushBack ( & s1, 4 ) ; SeqListPushBack ( & s1, 5 ) ; SeqListPrint ( & s1) ; SeqListPushFront ( & s1, 10 ) ; SeqListPushFront ( & s1, 20 ) ; SeqListPushFront ( & s1, 30 ) ; SeqListPushFront ( & s1, 40 ) ; SeqListPushFront ( & s1, 50 ) ; SeqListPrint ( & s1) ; SeqListPopFront ( & s1) ; SeqListPopFront ( & s1) ; SeqListPrint ( & s1) ; SeqListDestory ( & s1) ;
}
void TestSeqList3 ( )
{ SL s1; SeqListInit ( & s1) ; SeqListPushBack ( & s1, 1 ) ; SeqListPushBack ( & s1, 2 ) ; SeqListPushBack ( & s1, 3 ) ; SeqListPushBack ( & s1, 4 ) ; SeqListPushBack ( & s1, 5 ) ; SeqListPrint ( & s1) ; SeqListFind ( & s1, 4 ) ; SeqListModify ( & s1, 1 , 30 ) ; SeqListPrint ( & s1) ; SeqListDestory ( & s1) ; } int main ( )
{ TestSeqList3 ( ) ; return 0 ;
}