数据结构之单链表——带头节点和不带头节点(C/C++)

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define BLINKHEAD 1 // 0表示不带头节点,1表示带头节点

typedef int DataType;

typedef struct LNode{	
	DataType data;
	struct LNode *next;
}LNode,*LinkList;

#if BLINKHEAD
// 带头节点的单链表的创建、插入及删除 开始**********************************************************

/******************************************************************************
 * 函数: CreatListF
 * 描述: 创建带头节点的单链表
 * 输入: 
 * 输出: 
 * 返回: 
 * 备注: 这里的函数参数必须是链表的指针的地址,否则为局部变量无法将链表指针带回主程序
******************************************************************************/
void CreatListF( LinkList *head)
{
	*head=(LNode*)malloc(sizeof(LNode));
	(*head)->next=NULL;

	if(!*head)
		printf("debug\n");
}

/******************************************************************************
 * 函数: InsertList
 * 描述: 从小到大插入
 * 输入: 
 * 输出: 
 * 返回: 
 * 备注: 
 	参数 LinkList *head可以是LinkList head,届时将改LinkList p=head,由于带头节
节点的时候不会操作第一个节点(第一个节点为头节点)
******************************************************************************/
void InsertList(LinkList *head,DataType data)
{
	LinkList p=*head,q;
	LNode *s;
	
	q=p;
	p=p->next;
	while(p && p->data<data)
	{
		q=p;
		p=p->next;
	}
	s=(LNode*)malloc(sizeof(LNode));
	s->data=data;
	
	s->next=p;
	q->next=s;
	
}

/******************************************************************************
 * 函数: DeleteList
 * 描述: 删除,注意函数查不到的情况
 * 输入: 
 * 输出: 
 * 返回: 
 * 备注: 
 	参数 LinkList *head可以是LinkList head,届时将改LinkList p=head,由于带头节
节点的时候不会操作第一个节点(第一个节点为头节点)
******************************************************************************/
int DeleteList(LinkList *head,DataType data)
{
	LinkList p=*head,q;
	q=p;
	p=p->next;
	while(p && p->data!=data)
	{
		q=p;
		p=p->next;
	}
	
	if(!p)
		return 0;// 为找不到到
	
	q->next=p->next;
	
	free(p);// 释放指针
	
	p=NULL;// 释放后指针指向NULL
	
	return 1;
}
void PrintList(LinkList head)
{
	LinkList p=head;
	while(p->next != NULL)
	{		
		p=p->next;
		printf("%d->",p->data);
	}
	printf("end\n");
}
// 带头节点的单链表的创建、插入及删除 结束**********************************************************
#else
// 不带头节点的单链表的创建、插入及删除 开始--------------------------------------------------------

/******************************************************************************
 * 函数: CreatListF
 * 描述: 创建不带头节点的单链表
 * 输入: 
 * 输出: 
 * 返回: 
 * 备注: 这里的函数参数必须是链表的指针的地址,否则为局部变量无法将链表指针带回主程序
******************************************************************************/
void CreatListF( LinkList *head)
{
	*head=NULL;
}

/******************************************************************************
 * 函数: InsertList
 * 描述: 从小到大插入
 * 输入: 
 * 输出: 
 * 返回: 
 * 备注: 单链表的参数必须为二级指针,由于会对第一个节点进行处理
******************************************************************************/
void InsertList(LinkList *head,DataType data)
{
	LinkList p=*head,q;
	LNode *s;
	
	s=(LNode*)malloc(sizeof(LNode));
	s->data=data;
	
	if(p==NULL)
	{
		// 空表的情况
		s->next=NULL;
		*head=s;
	}
	else 
	{
		q=p;
		
		while(p&&p->data<data)
		{
			q=p;
			p=p->next;
		}
		
		if(p==*head)
		{
			// 在头部插入
			s->next=p;
			*head=s;
		}
		else 
		{
			// 在中部或尾部插入
			s->next=p;
			q->next=s;
		}
	}
	
}

/******************************************************************************
 * 函数: DeleteList
 * 描述: 删除,注意函数查不到的情况
 * 输入: 
 * 输出: 
 * 返回: 
 * 备注: 函数参数必须是二级指针,可能会删除第一个节点
******************************************************************************/
int DeleteList(LinkList *head,DataType data)
{
	LinkList p=*head,q;
	q=p;
	
	while(p && p->data!=data)
	{
		q=p;
		p=p->next;
	}
	
	if(!p)
		return 0;// 未找到
	
	q->next=p->next;
	
	free(p);// 释放指针
	
	p=NULL;// 释放后指针指向NULL
	
	return 1;
}
void PrintList(LinkList head)
{
	LinkList p=head;
	while(p!= NULL)
	{	
		printf("%d->",p->data);	
		p=p->next;
	}
	printf("end\n");
}
// 不带头节点的单链表的创建、插入及删除 结束--------------------------------------------------------
#endif


// 主程序
void main()
{
	LinkList head=NULL;
	CreatListF(&head);// 这里很重要
	
	PrintList(head);

	InsertList(&head,10);
	PrintList(head);	
	InsertList(&head,20);
	PrintList(head);
	InsertList(&head,5);
	PrintList(head);
	InsertList(&head,15);
	PrintList(head);
	
	DeleteList(&head,15);
	PrintList(head);
	
	DeleteList(&head,15);
	PrintList(head);
}


已在在vc6.0实践过


特别注意的是:

带头节点的单链表和不带头节点的单链表的区别:

带头节点的单链表:

         初始化时一定返回是指向头节点的地址,必须要用二维指针,否则会导致内存访问失败或异常。届时插入,删除只需带入头节点就行了,不是特别复杂。

不带头节点的单链表:

         不带头节点的单链表在初始化、插入、删除时对于第一个节点的操作与其他节点不一样需要特别处理,添加了程序的复杂度。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值