線性表的構造

線性表的邏輯定義

     線性表(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

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