//=============================================================================
//LinerList.h
//=============================================================================
//初始化隊列的長度
#define LIST_INIT_SIZE 100
//隊列長度不足重新申請空間的默認增長幅度
#define LISTINCREMENT 10
//若干信號量定義
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW 'o'
#define OUTB -2 //越界錯誤
//狀態返回值類型定義
typedef int Status;
//隊列元素定義
typedef struct Elemtyp
{
//不同需要的元素修改此處
int elem;
}ElemType;
//隊列的類型定義
typedef struct Sq
{
ElemType *head;
int length;
int listsize;
}Sqlist;
//==============================================================
//函數聲明部分:
//==============================================================
//線性表操作的工具函數
//比較函數
Status Compare ( ElemType e1, ElemType e2);
//訪問函數
void Visit (Sqlist);
//線性表複製函數,把L1複製到L2中
Status Copy ( Sqlist L1, Sqlist L2);
//線性表的初始化
Status Init_Sq ( Sqlist &L );
//線性表空間的釋放
Status Destory_Sq (Sqlist &L );
//線性表的清空
Status Clear_Sq ( Sqlist &L );
//判斷是否空表
Status IsEmpty_Sq ( Sqlist L );
//獲取當前表長
int Length_Sq ( Sqlist L );
//獲取表中指定元素的值
Status GetElem_Sq ( Sqlist L, int i, ElemType &e );
//確定指定元素在表當中的位置
int LocateElem_Sq ( Sqlist L, ElemType e);
//返回指定位置元素的前驅
Status PriorElem ( Sqlist L, ElemType cur_e, ElemType &pri_e);
//返回指定位置元素的後繼
Status NextElem ( Sqlist L, ElemType cur_e, ElemType &nex_e);
//對線性表的插入操作
Status ListInsert ( Sqlist &L, ElemType e, int pos);
//對線性表的刪除操作
Status ListDelete ( Sqlist &L, ElemType &e, int pos);
//對線性表的遍歷
Status Traverse ( Sqlist L);
//===============================================================================
//LinerList.cpp
//===============================================================================
//線性表加載文件
#include "stdafx.h"
#include "LinerList.h" //線性表定義文件
#include <malloc.h>
//函數實現部分
Status Init_Sq ( Sqlist &L)
{
L.head = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if ( !L.head) return OVERFLOW;
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}//Init_Sq
Status Destory_Sq ( Sqlist &L)
{
if ( !L.head) return ERROR;
free(&L);
L.head=NULL;
return OK;
}//Destory_Sq
Status Clear_Sq ( Sqlist &L)
{
int i;//內循環計數
if ( !L.head) return ERROR;
for(i = 0; i<L.length; i++)
L.head[i].elem = 0;
return OK;
}//Clear_Sq
Status IsEmpty_Sq ( Sqlist L)
{
if( !L.head) return TRUE;
return FALSE;
}//IsEmpty_Sq
int Length_Sq ( Sqlist L)
{
return L.length;
}//Length_Sq
Status GetElme_Sq ( Sqlist L, int pos, ElemType &e)
{
if( !L.head) return ERROR;
else if( pos < 0 || pos >= L.length)
return OVERFLOW;
e = L.head[pos-1];
return OK;
}//GetElem_Sq
Status Compare ( ElemType e1, ElemType e2)
{
if( e1.elem == e2.elem)
return TRUE;
return FALSE;
}//Compare
int LocateElem_Sq ( Sqlist L, ElemType e)
{
int i=0;//計數並記錄位置
Status s;//判斷是否找到
if ( !L.head) return ERROR;
for ( ; i < L.length; i++)
{
s = Compare ( L.head [ i - 1 ], L.head [ i - 2] );
if ( s) break;
}
if( s) return i;
return -1;
}//LocateElem_Sq
Status PriorElem ( Sqlist L, ElemType cur_e, ElemType &pri_e)
{
int _pos = 0;
_pos = LocateElem_Sq ( L, cur_e);
if ( _pos == -1) return ERROR;
else if ( _pos == 0 ) return OUTB;
pri_e = L.head[ _pos-1 ];
return OK;
}//PriorElem
Status NextElem ( Sqlist L, ElemType cur_e, ElemType &nex_e)
{
int _pos = 0;
_pos = LocateElem_Sq ( L, cur_e);
if ( _pos == -1) return ERROR;
else if( _pos == L.length-1 ) return OUTB;
nex_e = L.head [ _pos+1 ];
return OK;
}//NextElem
Status Copy ( Sqlist L1, Sqlist &L2)
{
if ( L1.head || L2.head) return ERROR;
else if ( L1.length <= L2.length ) return ERROR;
for ( int i = 0; i < L1.length; i++)
L2.head [ i ] = L1.head [ i ];
return OK;
}//Copy
Status ListInsert ( Sqlist &L, ElemType e, int pos)
{
ElemType *newbase;
if ( !L.head) return ERROR;
else if ( pos < 1 || pos > L.length+1 ) return OUTB;
if ( L.length == L.listsize)
{
newbase = ( ElemType *)realloc( L.head, ( L.length+LISTINCREMENT)
* sizeof ( ElemType) );
L.head = newbase;
}
for (int i = L.length; i >= pos-1; --i)
L.head [ i + 1 ] = L.head [ i ];
L.head [ i ] = e;
L.length++;
return OK;
}//ListInsert
Status ListDelete ( Sqlist &L, ElemType &e, int pos)
{
if( !L.head) return ERROR;
else if ( pos < 0 || pos >= L.length ) return OUTB;
for ( int i = pos-1; i < L.length-1; i++)
L.head [ i ] = L.head [ i+1 ];
L.length--;
return OK;
}//ListDelete
void Visit ( Sqlist L)
{
for ( int i = 0; i < L.length-1; i++)
printf ( " %d /n", L.head [ i ].elem);
}//Visit
Status Traverse ( Sqlist L)
{
if ( !L.head) return ERROR;
Visit( L);
return OK;
}//Traverse