數(shù)據(jù)結(jié)構(gòu)----鏈表相關(guān)操作

定義鏈表結(jié)構(gòu)體(節(jié)點(diǎn))

typedef struct Node{
    int data;
    struct Node * pNext;
}NODE,*PNODE;
NODE 等價于
typedef struct Node
PNODE 等價于
typedef struct * Node

1.創(chuàng)建一個鏈表

/**
 * 創(chuàng)建一個集合,返回頭節(jié)點(diǎn)
 * @return [description]
 */
PNODE createList(){
    PNODE pHead=(PNODE)malloc(sizeof(NODE));
    pHead->pNext=NULL;
    return pHead;
}

2.向鏈表中添加一個節(jié)點(diǎn)

/**
 * 添加一個節(jié)點(diǎn)
 * @param pHead [description]
 * @param node  [description]
 */
void append(PNODE pHead,PNODE node){
    while(pHead->pNext!=NULL){
        pHead=pHead->pNext;
    }
    pHead->pNext=node;
}

3.向指定的位置添加一個節(jié)點(diǎn)

/**
 * 向指定位置添加一個節(jié)點(diǎn)   當(dāng)index超過索引會將元素掛到鏈表的結(jié)尾
 * @param pHead [description]
 * @param node  [description]
 * @param index [description]
 */
void insert(PNODE pHead,PNODE node,int index){
    PNODE temp=pHead;

    while(temp->pNext!=NULL){
        if (index==0&&node)
        {
            /* code */
            node->pNext=temp->pNext;
            temp->pNext=node;
            return;
        }
        index--;
        temp=temp->pNext;
    }
}

4.刪除指定位置的結(jié)點(diǎn)

/**
 * 刪除指定位置的數(shù)據(jù)
 * @param pHead [description]
 * @param index [description]
 */
PNODE delete(PNODE pHead, int index){
    PNODE r;
    PNODE temp=pHead;

    if (index<=0){
        //如果是刪除第一個元素
        r=pHead;
        pHead=pHead->pNext;
        free(r);
        return pHead;
    }

    while(temp->pNext!=NULL){
        if (index==0){
            //刪除
            //首先保存temp所指向的結(jié)點(diǎn)的指針
            r=temp->pNext;
            //將temp->pNext指向下下個結(jié)點(diǎn)
            temp->pNext=temp->pNext->pNext;
            //釋放內(nèi)存
            free(r);

            return pHead;
        }
        index--;
        temp=temp->pNext;
    }
    printf("%s\n", "\n下標(biāo)越界,指定索引超過鏈表長度,刪除失敗");
    return pHead;
}

5.獲取鏈表的長度

/**
 * 獲取鏈表的長度
 * @param  pHead [description]
 * @return       [description]
 */
int size(PNODE pHead){
    int len=0;
    while((pHead=pHead->pNext)!=NULL){
        len++;
    }
    return len;
}

6.對鏈表排序(冒泡排序算法)

/**
 * 對鏈表排序(冒泡排序算法)
 * @param pHead [description]
 */
void sortList(PNODE pHead){
    int i,j,max;
    
    int length=size(pHead);

    PNODE r,q;

    r=pHead;
    for (int i = 0; i < length-1; ++i)
    {
        //向下移動
        r=r->pNext;
        
        //假定在外層的本次循環(huán)中的第一個節(jié)點(diǎn)的數(shù)據(jù)是最大值
        max=r->data;
        
        //把本次循環(huán)的r當(dāng)前節(jié)點(diǎn)賦值給q
        q=r;
        
        //從i的下一個結(jié)點(diǎn)開始,往下找,是否有比max打的結(jié)點(diǎn)
        for (int j = i+1; j < length; ++j)
        {
            //向下移動一次
            q=q->pNext;
            
            if (q->data>max)
            {
                //找到比max大的數(shù),交換
                max=q->data;
                q->data=r->data;
                r->data=max;
            }
        }
    }
}

7.遍歷

/**
 * 遍歷鏈表
 * @param pHead [description]
 */
void traverse(PNODE pHead){
    while((pHead=pHead->pNext)!=NULL){
        printf("%d\t", pHead->data);
    }
}

測試

int main(){
    int i;

    PNODE pHead=NULL;

    //創(chuàng)建
    pHead=createList();

    //添加
    for(i=0;i<10;i++){
        //創(chuàng)建結(jié)點(diǎn)
        PNODE pNew=(PNODE)malloc(sizeof(NODE));
        pNew->data=i+1;
        pNew->pNext=NULL;

        //追加結(jié)點(diǎn)
        append(pHead,pNew);
    }

    printf("鏈表長度是length=%d\n",size(pHead));
    traverse(pHead);

    pHead=delete(pHead, 0);
    printf("\n");
    printf("鏈表長度是length=%d\n",size(pHead));
    traverse(pHead);

    pHead=delete(pHead, 1);
    printf("\n");
    traverse(pHead);
    

    PNODE pNew=(PNODE)malloc(sizeof(NODE));
    pNew->data=100;
    printf("鏈表長度是length=%d\n",size(pHead));
    pNew->pNext=NULL;

    insert(pHead,pNew,3);
    printf("\n");
    printf("鏈表長度是length=%d\n",size(pHead));
    traverse(pHead);

    sortList(pHead);
    printf("\n");
    traverse(pHead);

    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容