固定长度的顺序表
sqlist头文件
# pragma once
typedef struct SQList
{
int elem[ 10 ] ;
int length;
} SQList, * PSQList;
void InitSqList ( PSQList ps) ;
bool Insert ( PSQList ps, int pos, int val) ;
bool IsEmpty ( PSQList ps) ;
int Search ( PSQList ps, int key) ;
bool DelPos ( PSQList ps, int pos) ;
bool DelVal ( PSQList ps, int val) ;
int GetPrio ( PSQList ps, int key) ;
int GetNext ( PSQList ps, int key) ;
void Show ( PSQList ps) ;
void Clear ( PSQList ps) ;
void Destroy ( PSQList ps) ;
sqlist .cpp文件
# include <stdio.h>
# include <assert.h>
# include "sqlist.h"
void InitSqList ( PSQList ps)
{
assert ( ps != NULL ) ;
if ( ps == NULL )
return ;
ps-> length = 0 ;
}
static bool IsFul ( PSQList ps)
{
return ps-> length == 10 ;
}
bool Insert ( PSQList ps, int pos, int val)
{
if ( pos< 0 || pos> ps-> length || IsFul ( ps) )
{
return false;
}
for ( int i = ps-> length - 1 ; i >= pos; i-- )
{
ps-> elem[ i + 1 ] = ps-> elem[ i] ;
}
ps-> elem[ pos] = val;
ps-> length++ ;
return true;
}
bool IsEmpty ( PSQList ps)
{
return ps-> length == 0 ;
}
int Search ( PSQList ps, int key)
{
for ( int i = 0 ; i < ps-> length; i++ )
{
if ( key == ps-> elem[ i] )
return i;
}
return - 1 ;
}
bool DelPos ( PSQList ps, int pos)
{
if ( pos < 0 || pos >= ps-> length)
return false;
for ( int i = pos; i < ps-> length - 1 ; i++ )
{
ps-> elem[ i] = ps-> elem[ i + 1 ] ;
}
ps-> length-- ;
return true;
}
bool DelVal ( PSQList ps, int val)
{
int i = Search ( ps, val) ;
if ( i < 0 )
return false;
return DelPos ( ps, i) ;
}
int GetPrio ( PSQList ps, int key)
{
int i = Search ( ps, key) ;
if ( i <= 0 )
return - 1 ;
return i - 1 ;
}
int GetNext ( PSQList ps, int key)
{
int i = Search ( ps, key) ;
if ( i < 0 || i == ps-> length - 1 )
return - 1 ;
return i + 1 ;
}
void Show ( PSQList ps)
{
for ( int i = 0 ; i < ps-> length; i++ )
{
printf ( "%d " , ps-> elem[ i] ) ;
}
printf ( "\n" ) ;
}
void Clear ( PSQList ps)
{
ps-> length = 0 ;
}
void Destroy ( PSQList ps)
{
Clear ( ps) ;
}
固定不长度的顺序表
dsqlist头文件
# pragma once
# define INIT_SIZE 10
typedef struct DSQList
{
int * elem;
int length;
int listsize;
} DSQList, * DPSQList;
void InitSqList ( DPSQList ps) ;
bool Insert ( DPSQList ps, int pos, int val) ;
bool IsEmpty ( DPSQList ps) ;
int Search ( DPSQList ps, int key) ;
bool DelPos ( DPSQList ps, int pos) ;
bool DelVal ( DPSQList ps, int val) ;
int GetPrio ( DPSQList ps, int key) ;
int GetNext ( DPSQList ps, int key) ;
void Show ( DPSQList ps) ;
void Clear ( DPSQList ps) ;
void Destroy ( DPSQList ps) ;
dsqlist .cpp文件
# include <assert.h>
# include <stdio.h>
# include <stdlib.h>
# include "dsqlist.h"
void InitSqList ( DPSQList ps)
{
assert ( ps != NULL ) ;
if ( ps == NULL )
{
return ;
}
ps-> elem = ( int * ) malloc ( INIT_SIZE * sizeof ( int ) ) ;
ps-> length = 0 ;
ps-> listsize = INIT_SIZE;
}
static bool IsFull ( DPSQList ps)
{
return ps-> length == ps-> listsize;
}
static bool Inc ( DPSQList ps)
{
ps-> elem = ( int * ) realloc ( ps-> elem, ps-> listsize * 2 * sizeof ( int ) ) ;
assert ( ps-> elem != NULL ) ;
ps-> listsize *= 2 ;
return true;
}
bool Insert ( DPSQList ps, int pos, int val)
{
if ( pos< 0 || pos> ps-> length)
{
return false;
}
if ( IsFull ( ps) )
{
Inc ( ps) ;
}
for ( int i = ps-> length - 1 ; i >= pos; i-- )
{
ps-> elem[ i + 1 ] = ps-> elem[ i] ;
}
ps-> elem[ pos] = val;
ps-> length++ ;
return true;
}
bool IsEmpty ( DPSQList ps)
{
return ps-> length == 0 ;
}
int Search ( DPSQList ps, int key)
{
for ( int i = 0 ; i < ps-> length; i++ )
{
if ( key == ps-> elem[ i] )
return i;
}
return - 1 ;
}
bool DelPos ( DPSQList ps, int pos)
{
if ( pos < 0 || pos >= ps-> length)
return false;
for ( int i = pos; i < ps-> length - 1 ; i++ )
{
ps-> elem[ i] = ps-> elem[ i + 1 ] ;
}
ps-> length-- ;
return true;
}
bool DelVal ( DPSQList ps, int val)
{
int i = Search ( ps, val) ;
if ( i < 0 )
return false;
return DelPos ( ps, i) ;
}
int GetPrio ( DPSQList ps, int key)
{
int i = Search ( ps, key) ;
if ( i <= 0 )
return - 1 ;
return i - 1 ;
}
int GetNext ( DPSQList ps, int key)
{
int i = Search ( ps, key) ;
if ( i < 0 || i == ps-> length - 1 )
return - 1 ;
return i + 1 ;
}
void Show ( DPSQList ps)
{
for ( int i = 0 ; i < ps-> length; i++ )
{
printf ( "%d " , ps-> elem[ i] ) ;
}
printf ( "\n" ) ;
}
void Clear ( DPSQList ps)
{
ps-> length = 0 ;
}
void Destroy ( DPSQList ps)
{
free ( ps-> elem) ;
ps-> elem = NULL ;
ps-> length = 0 ;
ps-> listsize = 0 ;
}