Rumah >Java >javaTutorial >Apakah operasi senarai terpaut sehala Java?
Titik teras adalah untuk mentakrifkan penuding kepala dan penuding ekor (penunjuk ekor boleh undefined tetapi Ia akan meningkatkan pertindihan kod dan meningkatkan masa berjalan program); >#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;
}
Penambahan pengepala
Idea kod pada asasnya sama dengan penambahan ekor Beri perhatian untuk membezakan pautan nod: #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;
}
Tambah n x nod data pada satu masa:
#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; }Cari mengikut subskrip: Idea Pada dasarnya tidak berubah; subskrip dan nilai kaunter adalah sama;
#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 MyPadamkan nod ekor:
#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; } } } }
#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; }
Atas ialah kandungan terperinci Apakah operasi senarai terpaut sehala Java?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!