線性表之鏈表

單鏈表也是一種鏈式存取的線性表,用一組地址任意的存儲單元存放線性表中的數據元素。鏈表中的數據是以結點來表示的,以next指針指向下一個節點而鏈接起來,相比於順序表,鏈表有着快速增加,刪除節點的優勢,其節點的隨機訪問效率較低。

頭文件:

/***************************************************************************************************** 
 *Copyright:Yue Workstation 
 * 
 *FileName:LineTable.h 
 * 
 *Function:單鏈表相關數據定義和函數聲明 
 * 
 *Author:Abel Lee 
 * 
 *CreateOn:2011-5-3 
 * 
 *Log:2011-5-3 由Abel Lee創建 
 *****************************************************************************************************/ 

#ifndef SINGLE_LIST_H 
#define SINGLE_LIST_H 

#include "global.h" 

typedef struct LNode 
{ 
    ElemType data; 
    struct LNode *next; 
}LNode,*LinkList; 

int CreateSingleList(LinkList *L,int n); 
void  PrintSingleList(LinkList L); 
int InsertSingleList(LinkList *L,int i,ElemType e); 
int DeleteSingleList(LinkList *L,int i,ElemType *e); 
int GetSingleListLength(LinkList L); 
void DestroySingleList(LinkList *L); 

#endif

源文件:

/***************************************************************************************************** 
 *Copyright:Yue Workstation 
 * 
 *FileName:SingleList.c 
 * 
 *Function:單鏈表基本操作 
 * 
 *Author:Abel Lee 
 * 
 *CreateOn:2011-5-3 
 * 
 *Log:2011-5-3 由Abel Lee創建 
 *****************************************************************************************************/ 

#include "../inc/SingleList.h" 

/**************************************************************************************************** 
 *Function Name:CreateSingleList 
 * 
 *Function:創建一個單鏈表 
 * 
 *Parameter:     L:單鏈表表頭, 
 *                 n:單鏈表長度 
 * 
 *Return Value:成功返回0,失敗返回-1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
int CreateSingleList(LinkList *L,int n) 
{ 
    int i = 0; 
    LinkList p1,p2; 

    *L = (LinkList)malloc(sizeof(LNode)); 
    if(*L == NULL) 
    { 
        perror("malooc error\n"); 
        return -1; 
    } 
    (*L)->data = 0; 
    (*L)->next = NULL; 
    p1 = *L; 
    p2 = *L; 

    for(i=1; i<=n; i++) 
    { 
        p1 = (LinkList)malloc(sizeof(LNode)); 
        if(p1 == NULL) 
        { 
            perror("malooc error\n"); 
            return -1; 
        } 
        p1->data = i; 
        p2->next = p1; 
        p2 = p1; 
    } 

    p2->next = NULL; 
    (*L)->data = n; 

    return 0; 
} 

/**************************************************************************************************** 
 *Function Name:PrintSingleList 
 * 
 *Function:打印單鏈表表中的元素 
 * 
 *Parameter:     L:單鏈表表頭 
 * 
 *Return Value:無 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
void  PrintSingleList(LinkList L) 
{ 
    L = L->next; 
    while(L) 
    { 
        printf("%d---",L->data); 
        L = L->next; 
    } 
    putchar('\n'); 

    return ; 
} 

/**************************************************************************************************** 
 *Function Name:InsertSingleList 
 * 
 *Function:在i位置插入一個元素e 
 * 
 *Parameter:     L:單鏈表表頭, 
 *               i:元素位置 
 *               e:要插入的元素 
 * 
 *Return Value:成功返回0,失敗返回-1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
int InsertSingleList(LinkList *L,int i,ElemType e) 
{ 
    LinkList p1 = (*L)->next; 
    LinkList p2 = (*L)->next; 
    int j = 1; 

    while(p1 && j<i-1) 
    { 
        p1 = p1->next; 
        ++j; 
    } 

    if(!p1 || j>i-1) 
    { 
        perror("Insert position error,the parameter i is error!\n"); 
        return -1; 
    } 

    p2 = (LinkList)malloc(sizeof(LNode)); 
    p2->data = e; 
    p2->next = p1->next; 
    p1->next = p2; 
    (*L)->data += 1; 

    return 0; 
} 

/**************************************************************************************************** 
 *Function Name:DeleteSingleList 
 * 
 *Function:在制定位置刪除單鏈表中的元素 
 * 
 *Parameter:     L:單鏈表表頭, 
 *               i:元素位置 
 *               e:要插入的元素 
 * 
 *Return Value: 成功返回0,失敗返回-1 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
int DeleteSingleList(LinkList *L,int i,ElemType *e) 
{ 
    LinkList p1 = (*L)->next; 
    LinkList p2 = NULL; 
    int j = 1; 

    while(p1 && j<i-1) 
    { 
        p1 = p1->next; 
        ++j; 
    } 

    if(!p1 || j>=i) 
    { 
        perror("Delete position error,the parameter i is error!\n"); 
        return -1; 
    } 

    p2 = p1->next; 
    p1->next = p2->next; 
    *e = p2->data; 
    free(p2); 
    (*L)->data -= 1; 

    return 0; 
} 

/**************************************************************************************************** 
 *Function Name:GetSingleListLength 
 * 
 *Function:獲取單鏈表長度 
 * 
 *Parameter:     L:單鏈表表頭 
 * 
 *Return Value:單鏈表長度 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
int GetSingleListLength(LinkList L) 
{ 
    if(L == NULL) 
    { 
        return -1; 
    } 

    return L->data; 
} 

/**************************************************************************************************** 
 *Function Name:DestroySingleList 
 * 
 *Function:銷燬一個單鏈表 
 * 
 *Parameter:     L:單鏈表表頭 
 * 
 *Return Value:無 
 * 
 *Author:Abel Lee 
 * 
 *Log:2011-5-24 
 ***************************************************************************************************/ 
void DestroySingleList(LinkList *L) 
{ 
    LinkList p1 = *L; 
    LinkList p2 = *L; 

    while(p1 != NULL) 
    { 

        p2 = p1; 
        p1 = p1->next; 
        free(p2); 
    } 
    *L = NULL; 

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