首頁 > Java > java教程 > 主體

Java單向鍊錶的操作有哪些?

WBOY
發布: 2023-05-08 18:37:16
轉載
1305 人瀏覽過

關於節點資料新增:

尾新增

最核心的是定義一個頭指標和一個尾指標(尾指標可以不定義但是會增加程式碼的重複性,增加程式運行時間);

關於尾添加:(注意區分有節點和無節點的情況)

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    endadd(&phead,&pend,4);
    ......
 
 
    return 0;
}
void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata)
 
{
  
    struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct));
    if(NULL == pt)
        return;
    pt->data = adddata;
    pt->pnext = NULL;
    if(NULL == *phead)
    {
       *phead = pt;
 
    }
    else
 
    {
        (*pend)->pnext = pt;
    }
    *pend= pt;
}
登入後複製

頭添加

關於程式碼思路與尾添加基本一致,注意區分節點的連結:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    head_add(&phead,&pend,4);
    ......
 
 
    return 0;
}
void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata)
 
{
    
    struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct));
    if(NULL == pt)
        return;
    pt->data = adddata;
    pt->pnext = NULL;
    if(NULL == *phead)
    {
       *pend = pt;
 
    }
    else
 
    {
        pt->pnext = (*phead);
    }
    *phead= pt;
}
登入後複製

一次新增n個x資料節點:

利用循壞,直接呼叫頭新增或尾新增:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    circulate_add(&phead,&pend,4,5);
    ......
 
 
    return 0;
}
void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int count, int adddata);
 
{
    for(int i = 0;i<count;i++)
    {
        endadd(phead, pend, adddata);
       
    }
 
 
 
}
登入後複製

關於查找:

根據指定資料:

核心就是透過頭指標一個一個往下走找到指定節點的資料與所找資料是否匹配,最重要的是要使用中間變數記錄頭指針,否則就無法找到頭指針了(因為是單項鍊錶):

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void data_find(struct Mystruct *phead, int designated_data);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    middle_data_find(phead,4);
    ......
 
 
    return 0;
}
void data_find(struct Mystruct* phead, int designated_data)
{
    if (NULL == phead)
        return;
    struct Mystruct* ptemp = phead;
    while (ptemp != NULL)
    {
        if (ptemp->data == designated_data)
        {
            printf("找到了");
            break;
 
        }
        ptemp = ptemp->pnext;
    }
    return;
 
}
登入後複製

根據下標查找:

思路基本上不變;差異傳入指定下標;內部定義一個計數器,當下標和計數器數值相等;表示鍊錶存在這個節點;可以選擇傳出或提醒;大家思考一下,動手實作一下。

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
struct Mystruct *index_find(struct Mystruct *phead, int index);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    middle_data_find(phead,4);
    ......
 
 
    return 0;
}
struct Mystruct* index_find(struct Mystruct* phead, int index)
 
{
    if (NULL == phead||index<0)
        return NULL;
 
    struct Mystruct* ptemp = phead;
    int i = 0;
    for (i = 0; i < index; i++)
    {
        ptemp = ptemp->pnext;
    }
    return ptemp;
 
}
登入後複製

刪除頭節點:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_head(struct Mystruct **phead,struct Mystruct **pend);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_head(&phead)
    ......
 
 
    return 0;
}
void deleat_head(struct Mystruct** phead, struct Mystruct** pend)
{
    if (NULL == *phead)
        return;
    struct Mystruct* pt = *phead;
    if ((*phead)->pnext == NULL)
    {
        free(pt);
        *phead = NULL;
        *pend = NULL;
    }
    else
    {
        *phead = (*phead)->pnext;
        free(pt);
    }
 
 
 
 
}
void deleat_end(struct My
登入後複製

刪除尾節點:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_end(struct Mystruct**phead,struct Mystruct**pend);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_head(&phead)
    ......
 
 
    return 0;
}
void deleat_end(struct Mystruct** phead, struct Mystruct** pend)
{
    if (NULL == *phead)
        return;
    struct Mystruct* pt = *phead;
    if (pt->pnext == NULL)
    {
        free(pt);
        *phead = NULL;
        *pend = NULL;
    }
    else
    {
        while (pt->pnext != (*pend))
        {
            if (pt->pnext == (*pend))
            {
                free(*pend);
                *pend = pt;
                pt->pnext = NULL;
                pt = pt->pnext;
            }
           
 
        }
 
    }
    
  
 
 
}
登入後複製

刪除中間節點:

這裡思路改變一下:根據資料或下標找到前一個節點,改變前一個節點的pnext指標的指向,直接指向下一個節點,也就是這個節點的pnext;簡單示範一下刪除中間指定資料:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata);
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_head(&phead)
    ......
 
 
    return 0;
}
void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata)
{
    if (NULL == *phead)
        return;
    struct Mystruct* pt = *phead;
    if (pt->pnext == NULL)
    {
        free(pt);
        *phead = NULL;
        *pend = NULL;
    }
}
登入後複製

刪除全部節點:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct 
{
    int data;
    struct Mystruct *pnext;
 
};
void deleat_all(struct Mystruct** phead, struct Mystruct** pend)
int main(void)
 
{
    
    struct Mystruct *phead = NULL;
    struct Mystruct *pend= NULL;
    deleat_all(&phead,&pend)
    ......
 
 
    return 0;
}
void deleat_all(struct Mystruct** phead, struct Mystruct** pend)
{
 
 
    while (*phead!= NULL)
    {
        struct Mystruct* pt = *phead;
        *phead = (*phead)->pnext;
        free(pt);
 
 
    }
    *phead = NULL;
    *pend = NULL;
 
 
 
}
登入後複製

以上是Java單向鍊錶的操作有哪些?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!