单链表基本操作练习

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int DataType;
typedef struct SListNode {
	struct SListNode* next;
	DataType data;
}Node;

//申请结点并初始化
extern Node* BuySListNode(DataType data);
//头插
extern void SListPushFront(Node** pplist, DataType x);
//头删
extern void SListPopFront(Node** pplist);
//尾插
extern void SListPushBack(Node** pplist, DataType x);
//尾删
extern void SListPopBack(Node** pplist);
//查找
extern Node* SListFind(Node* plist, DataType x);
//指定位置插入
extern void SListInsertAfter(Node* pos, DataType x);
//指定位置删除
extern void SListEraseAfter(Node* pos);
// 链表长度
extern int SListSize(Node* plist);
//销毁链表
extern void SListDestory(Node** pplist);
//打印链表
void PrintSList(Node* pplist);

#include "SList.h"

//申请结点并初始化
extern Node* BuySListNode(DataType data) {
	Node* newNode = (Node*)malloc(sizeof(Node));
	if (NULL == newNode) {
		assert(0);
		return NULL;
	}
	newNode->data = data;
	newNode->next = NULL;
	return newNode;
}
//头插
void SListPushFront(Node** pplist, DataType x) {
	assert(pplist);
	Node* newNode = BuySListNode(x);
	newNode->next = *pplist;
	*pplist = newNode;
}
//头删
void SListPopFront(Node** pplist) {
	assert(pplist);
	if (NULL == *pplist)
		return;
	Node* delNode = *pplist;
	*pplist = delNode->next;
	free(delNode);
}
//尾插
void SListPushBack(Node** pplist, DataType x) {
	assert(pplist);
	Node* cur = *pplist;
	Node* newNode = BuySListNode(x);
	if (NULL == *pplist) {
		*pplist = newNode;
	}
	else {
		while (cur->next) {
			cur = cur->next;
		}
		cur->next = newNode;
	}
}
//尾删
void SListPopBack(Node** pplist) {
	assert(pplist);
	if (NULL == *pplist) {
		return;
	}
	else if (NULL == (*pplist)->next ){
		free(*pplist);
		*pplist = NULL;
	}
	else {
		Node* cur = *pplist;
		Node* prev = NULL;
		while (cur->next) {
			prev = cur;
			cur = cur->next;
		}
		free(cur);
		prev->next = NULL;
	}
}
//查找
Node* SListFind(Node* plist, DataType x) {
	Node* cur = plist;
	while (cur) {
		if (cur->data == x) {
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
//指定位置插入
void SListInsertAfter(Node* pos, DataType x) {
	if (NULL == pos) {
		return;
	}
	Node* newNode = BuySListNode(x);
	newNode->next = pos->next;
	pos->next = newNode;
}
//指定位置删除
void SListEraseAfter(Node* pos) {
	if (NULL == pos || NULL == pos->next) {
		return;
	}
	Node* delNode = pos->next;
	pos->next = delNode->next;
	free(delNode);
}
// 链表长度
int SListSize(Node* plist) {
	Node* cur = plist;
	int count = 0;
	while (cur) {
		cur = cur->next;
		count++;
	}
	return count;
}
//销毁链表
void SListDestory(Node** pplist) {
	assert(pplist);
	Node* cur = *pplist;
	while (cur) {
		*pplist = cur->next;
		free(cur);
		cur = *pplist;
	}
}
//打印链表
void PrintSList(Node* pplist) {
	Node* cur = pplist;
	while (cur) {
		printf("%d-->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

#include "SList.h"

int main() {
	Node* plist = NULL;
	SListPushBack(&plist, 1);
	SListPushBack(&plist, 2);
	SListPushBack(&plist, 3);
	SListPushBack(&plist, 4);
	SListPushBack(&plist, 5);
	PrintSList(plist);//1-->2--> 3--> 4--> 5--> NULL

	SListPopBack(&plist);
	SListPopBack(&plist);
	PrintSList(plist);//1-->2-->3-->NULL

	SListPushFront(&plist, 0);
	PrintSList(plist);//0-->1-->2-->3-->NULL

	SListPopFront(&plist);
	PrintSList(plist);//1-->2-->3-->NULL

	SListInsertAfter(SListFind(plist, 2), 22);
	PrintSList(plist);//1-->2-->22-->3-->NULL

	SListEraseAfter(SListFind(plist, 2));
	PrintSList(plist);//1-->2-->3-->NULL

	printf("链表长度为%d", SListSize(plist));//3
	SListDestory(&plist);
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值