ホームページ  >  記事  >  Java  >  Javaの一方向リンクリストの操作は何ですか?

Javaの一方向リンクリストの操作は何ですか?

WBOY
WBOY転載
2023-05-08 18:37:161267ブラウズ

ノード データの追加について:

テールの追加

最も重要なことは、ヘッド ポインターとテール ポインターを定義することです (テール ポインターは次のように定義できます)。定義されていませんが、コードの再現性が向上し、プログラムの実行時間が増加します);

末尾の追加について: (ノードがあるノードとないノードの区別に注意してください)

#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 個のデータ ノードを次の場所に追加します。 1 回:

ループを直接使用ヘッド追加または末尾追加を呼び出す:

#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);
       
    }
 
 
 
}

検索について:

指定されたデータに従って:

コアヘッド ポインタを 1 つずつ下に移動して、データと指定されたすべてのノードを検索します。データが一致するかどうかを確認するには、中間変数を使用してヘッド ポインタを記録することが最も重要です。そうでない場合、ヘッド ポインタを記録することはできません。見つかった (単一リンクリストであるため):

#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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。