Home  >  Article  >  Java  >  What are the operations of Java one-way linked list?

What are the operations of Java one-way linked list?

WBOY
WBOYforward
2023-05-08 18:37:161267browse

About node data addition:

Tail addition

The most important thing is to define a head pointer and a tail pointer (the tail pointer can not be defined but It will increase the repeatability of the code and increase the running time of the program);

Regarding tail addition: (pay attention to distinguishing between nodes with and without nodes)

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

Header addition

The code idea is basically the same as tail addition. Pay attention to distinguish the links of nodes:

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

Add n x data nodes at one time:

Use loop, directly Call head addition or tail addition:

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

About search:

According to the specified data:

The core is to go down one by one through the head pointer to find the data and all the specified nodes. To find whether the data matches, the most important thing is to use an intermediate variable to record the head pointer, otherwise the head pointer cannot be found (because it is a single-linked list):

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

Search according to the subscript:

Idea It is basically unchanged; the difference is that the specified subscript is passed in; a counter is defined internally, and the subscript and the counter value are equal; it indicates that this node exists in the linked list; you can choose to send it out or remind it; think about it and practice it.

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

Delete the head node:

#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

Delete the tail node:

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

Delete the intermediate node:

Change the idea here: find it based on the data or subscript For the previous node, change the pointing of the pnext pointer of the previous node to directly point to the next node, which is the pnext of this node; a simple demonstration of deleting the specified data in the middle:

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

Delete all nodes:

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

The above is the detailed content of What are the operations of Java one-way linked list?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete