链表的创建,头插法,尾插法,遍历显示,节点删除

线性表之链式存储结构。
数据结构之单链表的创建。
包括,链表的创建,链表的插入,链表的显示,链表的节点删除。

#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.单链表采用链式存储结构,用一组任意的存储单元存放线性表的元素。
2.查找:单链表O(n)
3.单链表在找出某位置的指针后,插入和删除时间仅为O(1)。
4.单链表不需要预分分配存储空间,只要有新添加的就可以分配,元素个数不受限制。

note:
突然发现之前些的链表节点的删除有一个漏洞,现在已经修补漏洞。
如还有漏洞,请前辈们指出

发布了53 篇原创文章 · 获赞 16 · 访问量 2213

猜你喜欢

转载自blog.csdn.net/m0_37757533/article/details/101002532