数据结构——不带头结点的单链表增删查改的C语言实现

本文详细介绍了如何使用C语言实现不带头结点的单链表,包括初始化、尾插、头插、打印、删除、插入、查找、长度计算、排序、逆置、清空、取头尾元素以及删除指定值节点等操作。
摘要由CSDN通过智能技术生成

1、slist.h

#ifndef _slist_h_
#define _slist_h_

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
#include<memory.h>
#include<vld.h>  
#pragma warning(disable:4996)
#define ElemType int
typedef struct SListNode
{
	ElemType data;
	struct SListNode *next;
}SListNode;
//不带头结点的单链表
typedef SListNode* SList;

void SListInit(SList *phead);
void SListPushBack(SList *phead, ElemType x);
void SListPushFront(SList *phead, ElemType x);
void SListShow(SList *phead);
void SListPopBack(SList *phead);
void SListPopFront(SList *phead);
bool SListInsertPos(SList *phead, SListNode* pos, ElemType x);
bool SListInsertVal(SList *phead, ElemType x);
void SListErasePos(SList *phead, SListNode* pos);
void SListEraseVal(SList *phead, ElemType x);
SListNode* SListFind(SList *phead, ElemType x);
size_t SListLength(SList *phead);
void SListSort(SList *phead);
void SListReverse(SList *phead);
void SListClear(SList *phead);
ElemType SListFront(SList phead);
ElemType SListBack(SList phead);
void SListErase_all(SList *phead, ElemType x);
#endif 

(1) 单链表初始化

void SListInit(SList *phead)
{
	assert(phead != NULL);
	*phead = NULL;
}

(2)单链表尾插、头插

void SListPushBack(SList *phead, ElemType x)
{
	assert(phead != NULL);
	SListNode* s = (SListNode*)malloc(sizeof(SListNode));
	assert(s != NULL);
	s->data = x;
	s->next = NULL;
	SListNode* p = *phead;
	if (p == NULL)
		*phead = s;
	else
	{
		while (p->next != NULL)
			p = p->next;
		p->next = s;
	}
}
void SListPushFront(SList *phead, ElemType x)
{
	assert(phead != NULL);
	SListNode* s = (SListNode*)malloc(sizeof(SListNode));
	assert(s != NULL);
	s->data = x;
	s->next = *phead;
	*phead = s;
}

 (3)单链表打印

void SListShow(SList *phead)
{
	assert(phead != NULL);
	SListNode* p = *phead;
	while (p != NULL)
	{
		printf("%d-&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值