Chain storage structure of linear table.
The creation of a singly linked list of data structures.
Including, the creation of the linked list, the insertion of the linked list, the display of the linked list, and the deletion of the nodes of the linked list.
#include<stdio.h>
#include<stdlib.h>
#define N 10
typedef struct t_linklist
{
int data;
struct t_linklist *pnext;
}linklist,*plinklist;
linklist *linknodecreate(void)
{
linklist *linknode;
linknode = (linklist *)malloc(sizeof(linklist));
if(NULL == linknode)
{
printf("malloc space fail,please check it!\n");
return NULL;
}
linknode->data = 0;
linknode->pnext = NULL;
return linknode;
}
//this is header node instert
int linknodefrontinsert(plinklist p,int data)
{
linklist *ptemp;
if(p == NULL)
{
printf("the pointer node is NULL,please check it!\n");
return -1;
}
ptemp = (linklist *)malloc(sizeof(linklist));
if(NULL == ptemp)
{
printf("malloc speac fail\n");
return -1;
}
ptemp->data = data;
ptemp->pnext = p->pnext;
p->pnext = ptemp;
//printf("%d\n",ptemp->data);
return 0;
}
//this is trail node instert
int linknodetrailinsert(plinklist p,int nval)
{
linklist *ptemp;
if(NULL == p)
{
printf("the header pointer is NULL,please check it!\n");
return -1;
}
ptemp = (linklist *)malloc(sizeof(linklist));
if(NULL == ptemp)
{
printf("malloc temp node fail\n");
return -1;
}
while(p->pnext != NULL)
{
p = p->pnext;
}
ptemp->data = nval;
ptemp->pnext = p->pnext; // this need to special attendtion
p->pnext = ptemp;
return 0;
}
int linknodeserachshow(plinklist p)
{
if(NULL == p)
{
printf("the pointer node is NULL,please check it!\n");
return -1;
}
while(p != NULL)
{
printf("%d\n",p->data);
p = p->pnext;
}
return 0;
}
int linklistdelete(plinklist p,int nval)
{
linklist *ptemp = NULL;
if(NULL == p)
{
printf("this is NULL header pointer\n");
return 0;
}
while(p->pnext != NULL)
{
if(p->pnext->data == nval)
{
break;
}
p = p->pnext;
}
#if 1
if(NULL == p->pnext) //if not found delete node,will return ,and prompt message or print log
{
printf("delete node not found\n");
return 0;
}
#endif
ptemp = p->pnext;
p->pnext = ptemp->pnext;
free(ptemp);
ptemp = NULL; // this need to special attendtion
return 0;
}
int GetLinkNodeLength(plinklist p)
{
int nVal = 0;
if(NULL == p)
{
printf("the pointer is NULL\n");
return -1;
}
do
{
nVal++;
p = p->pnext;
}while(p->pnext != NULL);
return nVal;
}
int main(void)
{
linklist *pnode;
int i = 10;
int nlength = 0;
pnode = linknodecreate();
printf("%d\n",pnode->data);
#if 0
while(i--)
{
pnode = linknodeinsert(pnode,i);
}
linknodefrontinsert(pnode,1);
linknodefrontinsert(pnode,2);
linknodefrontinsert(pnode,4);
#endif
linknodetrailinsert(pnode,1);
linknodetrailinsert(pnode,2);
linknodetrailinsert(pnode,3);
linknodeserachshow(pnode);
nlength = GetLinkNodeLength(pnode);
if(-1 == nlength)
{
printf("link length get fail\n");
}
printf("%d\n",nlength);
//linklistdelete(pnode,2);
linklistdelete(pnode,6);
linknodeserachshow(pnode);
return 0;
}
1. Singly linked list adopts chain storage structure, with an arbitrary set of storage units to store the elements of linear list.
2. Search: Singly linked list O (n)
3. After finding the pointer at a certain position, the insertion and deletion time is only O (1).
4. Singly linked lists do not require pre-allocated storage space, as long as there are newly added, they can be allocated, and the number of elements is not limited.
note:
Suddenly discovered that there was a loophole in the previous deletion of linked list nodes, and the loophole has now been patched.
If there are loopholes, please predecessors point out