不带头结点的单链表的基本操作


1、头文件(nlist.h)

#pragma once

//不带头结点的单链表
typedef struct NNode
{
	int data;
	struct NNode *next;
}NNode,*NList;

//链表初始化
void InitList(NList *pplist);

//头插
bool Insert_head(NList *pplist,int val);

//尾插
bool Insert_tail(NList *pplist,int val);

//查找
NNode *Search(NList plist,int key);

//删除
bool Delete(NList *pplist,int key);

//获取长度
int GetLength(NList plist);

//判空
bool IsEmpty(NList plist); 

//清空
void Clear(NList *pplist);

//摧毁
void Destroy(NList *pplist);

//打印
void Show(NList plist);#pragma once

//不带头结点的单链表
typedef struct NNode
{
	int data;
	struct NNode *next;
}NNode,*NList;

//链表初始化
void InitList(NList *pplist);

//头插
bool Insert_head(NList *pplist,int val);

//尾插
bool Insert_tail(NList *pplist,int val);

//查找
NNode *Search(NList plist,int key);

//删除
bool Delete(NList *pplist,int key);

//获取长度
int GetLength(NList plist);

//判空
bool IsEmpty(NList plist); 

//清空
void Clear(NList *pplist);

//摧毁
void Destroy(NList *pplist);

//打印
void Show(NList plist);


2、源文件(nlist.cpp)

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"nlist.h"

//链表初始化
void InitList(NList *pplist)
{
	assert(pplist != NULL);

	*pplist = NULL;
}

//头插
bool Insert_head(NList *pplist,int val)
{
	NNode *p = (NNode *)malloc(sizeof(NNode));
	p->data = val;

	p->next = *pplist;
	*pplist = p;

	return true;
}

//尾插
bool Insert_tail(NList *pplist,int val)
{
	NNode *p = (NNode *)malloc(sizeof(NNode));
	p->data = val;
	p->next = NULL;

	if(*pplist == NULL)//如果为空的情况下
	{
		*pplist = p;
		return true;
	}

	NNode *q;
	for(q = *pplist;q->next != NULL;q = q->next);//找尾巴
	q->next = p;

	return true;
}

//查找
NNode *Search(NList plist,int key)
{
	NNode *p;
	for(p = plist->next;p != NULL;p = p->next)
	{
		if(p->data == key)
		{
			return p;
		}
	}
	return NULL;
}

//删除
bool Delete(NList *pplist,int key)
{
	if(*pplist == NULL)//if(IsEmpty(*pplist))链表为空
	{
		return false;
	}

	NNode *p = *pplist;
	if(p->data == key)//删除第一个点
	{
		*pplist = p->next;
		free(p);
		return true;
	}

	for(p = *pplist;p->next != NULL;p = p->next)
	{
		if(p->next->data == key)
		{
			NNode *q = p->next;
			p->next = q->next;
			free(q);
			return true;	
		}
	}
	return false;
}

//获取长度
int GetLength(NList plist)
{
	int count = 0;

	for(NNode *p = plist->next;p != NULL;p = p->next)
	{
		count++;
	}
	return count;
}

//判空
bool IsEmpty(NList plist)
{
	return plist == NULL;
}

//清空
void Clear(NList *pplist)
{
	Destroy(pplist);
}

//摧毁
void Destroy(NList *pplist)
{
	NNode *p;
	while(*pplist != NULL)
	{
		p = *pplist;
		*pplist = p->next;
		free(p);
	}
	*pplist = NULL;		
}

//打印
void Show(NList plist)
{
	NNode *p;
	for(p = plist->next;p != NULL;p = p->next)
	{
		printf("%d ",p->data);
	}
	printf("\n");
}


3、测试源文件(test.cpp)

#include<stdio.h>
#include<stdlib.h>
#include<vld.h>
#include"nlist.h"

int main()
{
	NList list;
	InitList(&list);
	
	for(int i=0;i<15;i++)
	{
		//Insert_head(&list,i);
		Insert_tail(&list,i);
	}
	
	//查找测试用例
	/*NNode *s1 = Search(list,3);
	NNode *s2 = Search(list,-1);
	NNode *s3 = Search(list,15);
	printf("%d\n",s1->data);
	printf("%d\n",s2->data);
	printf("%d\n",s3->data);*/
	
	//删除测试用例
	/*Delete(&list,-1);
	Delete(&list,18);
	Delete(&list,0);
	Delete(&list,5);
	Delete(&list,14);
	Show(list);*/

	//获取长度测试
	printf("%d\n",GetLength(list));

	Show(list);

	Destroy(&list);
	Destroy(&list);

	return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值