數據結構--線性結構

順序存儲結構

typedef int Position; 
typedef struct LNode *List; 
struct LNode { 
    ElementType Data[MAXSIZE]; 
    Position Last; 
}; 

/* 初始化 */ 
List MakeEmpty() 
{ 
    List L; 

    L = (List)malloc(sizeof(struct LNode)); 
    L->Last = -1; 

    return L; 
} 

/* 查找 */ 
#define ERROR -1 

Position Find( List L, ElementType X ) 
{ 
    Position i = 0; 

    while( i <= L->Last && L->Data[i]!= X ) 
        i++; 
    if ( i > L->Last )  return ERROR; /* 如果沒找到,返回錯誤信息 */ 
    else  return i;  /* 找到後返回的是存儲位置 */ 
} 

/* 插入 */ 
/*注意:在插入位置參數P上與課程視頻有所不同,課程視頻中i是序列位序(從1開始),這裏P是存儲下標位置(從0開始),兩者差1*/ 
bool Insert( List L, ElementType X, Position P )  
{ /* 在L的指定位置P前插入一個新元素X */ 
    Position i; 

    if ( L->Last == MAXSIZE-1) { 
        /* 表空間已滿,不能插入 */ 
        printf("表滿");  
        return false;  
    }   
    if ( P<0 || P>L->Last+1 ) { /* 檢查插入位置的合法性 */ 
        printf("位置不合法"); 
        return false;  
    }  
    for( i=L->Last; i>=P; i-- ) 
        L->Data[i+1] = L->Data[i]; /* 將位置P及以後的元素順序向後移動 */ 
    L->Data[P] = X;  /* 新元素插入 */ 
    L->Last++;       /* Last仍指向最後元素 */ 
    return true;  
}  

/* 刪除 */ 
/*注意:在刪除位置參數P上與課程視頻有所不同,課程視頻中i是序列位序(從1開始),這裏P是存儲下標位置(從0開始),兩者差1*/ 
bool Delete( List L, Position P ) 
{ /* 從L中刪除指定位置P的元素 */ 
    Position i; 

    if( P<0 || P>L->Last ) { /* 檢查空表及刪除位置的合法性 */ 
        printf("位置%d不存在元素", P );  
        return false;  
    } 
    for( i=P+1; i<=L->Last; i++ ) 
        L->Data[i-1] = L->Data[i]; /* 將位置P+1及以後的元素順序向前移動 */ 
    L->Last--; /* Last仍指向最後元素 */ 
    return true;    
} 

以上爲順序存儲結構的初始化查找插入刪除。

線性表基本操作
1、List MakeEmpty():初始化一個空線性表L;
2、ElementType FindKth( int K, List L ):根據位序K,返回相應元素 ;
3、int Find( ElementType X, List L ):在線性表L中查找X的第一次出現位置;
4、void Insert( ElementType X, int i, List L):在位序i前插入一個新元素X;
5、void Delete( int i, List L ):刪除指定位序i的元素;
6、int Length( List L ):返回線性表L的長度n。
這裏寫圖片描述

鏈式存儲結構

 typedef struct LNode *PtrToLNode; 
struct LNode { 
    ElementType Data; 
    PtrToLNode Next; 
}; 
typedef PtrToLNode Position; 
typedef PtrToLNode List; 

/* 查找 */ 
#define ERROR NULL 

Position Find( List L, ElementType X ) 
{ 
    Position p = L; /* p指向L的第1個結點 */ 

    while ( p && p->Data!=X ) 
        p = p->Next; 

    /* 下列語句可以用 return p; 替換 */ 
    if ( p ) 
        return p; 
    else 
        return ERROR; 
} 

/* 帶頭結點的插入 */ 
/*注意:在插入位置參數P上與課程視頻有所不同,課程視頻中i是序列位序(從1開始),這裏P是鏈表結點指針,在P之前插入新結點 */ 
bool Insert( List L, ElementType X, Position P ) 
{ /* 這裏默認L有頭結點 */ 
    Position tmp, pre; 

    /* 查找P的前一個結點 */         
    for ( pre=L; pre&&pre->Next!=P; pre=pre->Next ) ;             
    if ( pre==NULL ) { /* P所指的結點不在L中 */ 
        printf("插入位置參數錯誤\n"); 
        return false; 
    } 
    else { /* 找到了P的前一個結點pre */ 
        /* 在P前插入新結點 */ 
        tmp = (Position)malloc(sizeof(struct LNode)); /* 申請、填裝結點 */ 
        tmp->Data = X;  
        tmp->Next = P; 
        pre->Next = tmp; 
        return true; 
    } 
} 

/* 帶頭結點的刪除 */ 
/*注意:在刪除位置參數P上與課程視頻有所不同,課程視頻中i是序列位序(從1開始),這裏P是擬刪除結點指針 */ 
bool Delete( List L, Position P ) 
{ /* 這裏默認L有頭結點 */ 
    Position tmp, pre; 

    /* 查找P的前一個結點 */         
    for ( pre=L; pre&&pre->Next!=P; pre=pre->Next ) ;             
    if ( pre==NULL || P==NULL) { /* P所指的結點不在L中 */ 
        printf("刪除位置參數錯誤\n"); 
        return false; 
    } 
    else { /* 找到了P的前一個結點pre */ 
        /* 將P位置的結點刪除 */ 
        pre->Next = P->Next; 
        free(P); 
        return true; 
    } 
} 

鏈式存儲結構的初始化查找插入刪除。
這裏寫圖片描述

這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

堆棧

棧的順序存儲實現

typedef int Position; 
struct SNode { 
    ElementType *Data; /* 存儲元素的數組 */ 
    Position Top;      /* 棧頂指針 */ 
    int MaxSize;       /* 堆棧最大容量 */ 
}; 
typedef struct SNode *Stack; 

Stack CreateStack( int MaxSize ) 
{ 
    Stack S = (Stack)malloc(sizeof(struct SNode)); 
    S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType)); 
    S->Top = -1; 
    S->MaxSize = MaxSize; 
    return S; 
} 

bool IsFull( Stack S ) 
{ 
    return (S->Top == S->MaxSize-1); 
} 

bool Push( Stack S, ElementType X ) 
{ 
    if ( IsFull(S) ) { 
        printf("堆棧滿"); 
        return false; 
    } 
    else { 
        S->Data[++(S->Top)] = X; 
        return true; 
    } 
} 

bool IsEmpty( Stack S ) 
{ 
    return (S->Top == -1); 
} 

ElementType Pop( Stack S ) 
{ 
    if ( IsEmpty(S) ) { 
        printf("堆棧空"); 
        return ERROR; /* ERROR是ElementType的特殊值,標誌錯誤 */ 
    } 
    else  
        return ( S->Data[(S->Top)--] ); 
} 

棧的鏈式存儲實現

typedef struct SNode *PtrToSNode; 
struct SNode { 
    ElementType Data; 
    PtrToSNode Next; 
}; 
typedef PtrToSNode Stack; 

Stack CreateStack( )  
{ /* 構建一個堆棧的頭結點,返回該結點指針 */ 
    Stack S; 

    S = (Stack)malloc(sizeof(struct SNode)); 
    S->Next = NULL; 
    return S; 
} 

bool IsEmpty ( Stack S ) 
{ /* 判斷堆棧S是否爲空,若是返回true;否則返回false */ 
    return ( S->Next == NULL ); 
} 

bool Push( Stack S, ElementType X ) 
{ /* 將元素X壓入堆棧S */ 
    PtrToSNode TmpCell; 

    TmpCell = (PtrToSNode)malloc(sizeof(struct SNode)); 
    TmpCell->Data = X; 
    TmpCell->Next = S->Next; 
    S->Next = TmpCell; 
    return true; 
} 

ElementType Pop( Stack S )   
{ /* 刪除並返回堆棧S的棧頂元素 */ 
    PtrToSNode FirstCell; 
    ElementType TopElem; 

    if( IsEmpty(S) ) { 
        printf("堆棧空");  
        return ERROR; 
    } 
    else { 
        FirstCell = S->Next;  
        TopElem = FirstCell->Data; 
        S->Next = FirstCell->Next; 
        free(FirstCell); 
        return TopElem; 
    } 
} 

這裏寫圖片描述
這裏寫圖片描述

隊列

隊列的順序存儲實現

typedef int Position; 
struct QNode { 
    ElementType *Data;     /* 存儲元素的數組 */ 
    Position Front, Rear;  /* 隊列的頭、尾指針 */ 
    int MaxSize;           /* 隊列最大容量 */ 
}; 
typedef struct QNode *Queue; 

Queue CreateQueue( int MaxSize ) 
{ 
    Queue Q = (Queue)malloc(sizeof(struct QNode)); 
    Q->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType)); 
    Q->Front = Q->Rear = 0; 
    Q->MaxSize = MaxSize; 
    return Q; 
} 

bool IsFull( Queue Q ) 
{ 
    return ((Q->Rear+1)%Q->MaxSize == Q->Front); 
} 

bool AddQ( Queue Q, ElementType X ) 
{ 
    if ( IsFull(Q) ) { 
        printf("隊列滿"); 
        return false; 
    } 
    else { 
        Q->Rear = (Q->Rear+1)%Q->MaxSize; 
        Q->Data[Q->Rear] = X; 
        return true; 
    } 
} 

bool IsEmpty( Queue Q ) 
{ 
    return (Q->Front == Q->Rear); 
} 

ElementType DeleteQ( Queue Q ) 
{ 
    if ( IsEmpty(Q) ) {  
        printf("隊列空"); 
        return ERROR; 
    } 
    else  { 
        Q->Front =(Q->Front+1)%Q->MaxSize; 
        return  Q->Data[Q->Front]; 
    } 
} 

隊列的鏈式存儲實現

typedef struct Node *PtrToNode; 
struct Node { /* 隊列中的結點 */ 
    ElementType Data; 
    PtrToNode Next; 
}; 
typedef PtrToNode Position; 

struct QNode { 
    Position Front, Rear;  /* 隊列的頭、尾指針 */ 
    int MaxSize;           /* 隊列最大容量 */ 
}; 
typedef struct QNode *Queue; 

bool IsEmpty( Queue Q ) 
{ 
    return ( Q->Front == NULL); 
} 

ElementType DeleteQ( Queue Q ) 
{ 
    Position FrontCell;  
    ElementType FrontElem; 

    if  ( IsEmpty(Q) ) { 
        printf("隊列空"); 
        return ERROR; 
    } 
    else { 
        FrontCell = Q->Front; 
        if ( Q->Front == Q->Rear ) /* 若隊列只有一個元素 */ 
            Q->Front = Q->Rear = NULL; /* 刪除後隊列置爲空 */ 
        else                      
            Q->Front = Q->Front->Next; 
        FrontElem = FrontCell->Data; 

        free( FrontCell );  /* 釋放被刪除結點空間  */ 
        return  FrontElem; 
    } 
} 

這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述
這裏寫圖片描述

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