线性表的构造

线性表的逻辑定义

     线性表(Linear List)是由n(n≥0)个数据元素(结点)a1,a2,…,an组成的有限序列。
     ① 数据元素的个数n定义为表的长度(n=0时称为空表)。
     ② 将非空的线性表(n>0)记作:(a1,a2,…,an
     ③ 数据元素ai(1≤i≤n)只是个抽象符号,其具体含义在不同情况下可以不同。
  
线性表的逻辑结构特征
    
  对于非空的线性表:
     ① 有且仅有一个开始结点a1,没有直接前趋,有且仅有一个直接后继a2
     ② 有且仅有一个终结结点an,没有直接后继,有且仅有一个直接前趋an-1
     ③ 其余的内部结点ai(2≤i≤n-1)都有且仅有一个直接前趋ai-1和一个ai+1

常见的线性表的基本运算

1. InitList(L)

     构造一个空的线性表L,即表的初始化。
2. DestroyList(L)
     消除一个线性表L。
3. ClearList(L)
     清空一个线性表L。
4.     ListEmpty( L)
        判继线性表L是否为空表。

5. ListLength(L)

     求线性表L中的结点个数,即求表长。
6. GetElem(L,i)

     取线性表L中的第i个结点,这里要求1≤i≤ListLength(L)
7. LocateElem(L,x)

     在L中查找值为x 的结点,并返回该结点在L中的位置。若L中有多个结点的值和x 相同,则返回首次找到的结点位置;若L中没有结点的值为x ,则返回一个特殊值表示查找失败。
8. PriorElem(L,cur_e,*pre_e)
         返回L中的当前结点的上一个结点。
9. NextElem(L,cur_e,*pre_e)
         返回L中的当前结点的下一个结点。
10. ListInsert(L,x,i)

     在线性表L的第i个位置上插入一个值为x 的新结点,使得原编号为i,i+1,…,n的结点变为编号为i+1,i+2,…,n+1的结点。这里1≤i≤n+1,而n是原表L的长度。插入后,表L的长度加1。
11. ListDelete(L,i)

     删除线性表L的第i个结点,使得原编号为i+1,i+2,…,n的结点变成编号为i,i+1,…,n-1的结点。这里1≤i≤n,而n是原表L的长度。删除后表L的长度减1。
12.     ListTraverse( L,visit())
         遍历线性表L。


<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

/*************************line.h****************/
 #include <typedef.h>
#include <stdio.h>
#include <string.h>

typedef int Status;
typedef int ElemType;

 

struct LIST{
  ElemType *elem;
  int length;
  int listsize;
};
typedef struct LIST List;


Status InitList(List *L)
{
  L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  if(!L->elem)exit(OVERFLOW);
  L->length=0;
  L->listsize=LIST_INIT_SIZE;
  return OK;
}


Status DestroyList(List *L)
{
  free(L);
  L=NULL;
  return OK;
}


Status ClearList(List *L)

  List R
  InitList(&R);
  strcpy(L,R); 
  return OK;
}


Status ListEmpty(List L)
{
  if(L.elem[0]==NULL) return TRUE;
  else  return FALSE;
}


Status ListLength(List L)
{
  return L.length;
}


Status GetElem(List L,int i,ElemType *e)
{
  if(1<=i&&i<=L.length)
    *e=L.elem[i];
}


Status LocateElem(List L,ElemType e,ElemType compare())
{
  int i;
  for(i=1;i<=L.length;i++){
    if(compare(L.elem[i],e))
      return i;
    else return FALSE;
  }
}


Status PriorElem(List L,ElemType cur_e,int *pre_e)
{
  int i;
  for(i=2;i<=L.length;i++)
    if(L.elem[i]==cur_e)
      *pre_e=i-1;
}

 

Status NextElem(List L,ElemType cur_e,int *next_e)
{
  int i;
  for(i=1;i<=L.length;i++)
    if(L.elem[i]==cur_e)
      *next_e=i+1;
}


Status ListInsert(List *L,int i,ElemType e)
{

  ElemType *p,*q,*newbase;
  if(i<1||i>L->length+1)
    return ERROR;
  if(L->length>=L->listsize){
    newbase=(ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
    if(!newbase)exit(OVERFLOW);
    L->elem=newbase;
    L->listsize+=LISTINCREMENT;
  }
  q=&(L->elem[i-1]);
  for(p=&(L->elem[L->length-1]);p>=q;--p)
    *(p+1)=*p;
  *q=e;
  ++L->length;
  return OK;
}


Status ListDelete(List *L,int i,ElemType *e)
{
  ElemType *p,*q;
  if((i<1)||(i>L->length))
    return ERROR;
  p=&(L->elem[i-1]);
  *e=*p;
  q=L->elem+L->length-1;
  for(++p;p<=q;++p)
    *(p-1)=*p;
  --L->length;
  return OK;
}


Status ListTraverse(List L,ElemType visit())
{
  int i;
  for(i=1;i<=L.length;i++)
    visit(L.elem[i]);
}


/*************************typedef.h*****************/
#define TRUE                                           1
#define FALSE                                         0
#define OK                                                1
#define ERROR                                        0
#define INFEASIVLE                             -1
#define OVERFLOW                             -2
#define LIST_INIT_SIZE                   100
#define LISTINCREMENT                  10
#define EQUAL                                       1
#define STACK_INIT_SIZE             100
#define STACKINCREMENT            10
#define MAX                                      100
#define MAXSIZE                             100

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章