c语言不循环链表,无头单向不循环链表相关接口实现(C语言)

单链表相关接口介绍List.h

#define _CRT_SECURE_NO_WARNINGS

#ifndef __LIST_H__

#define __LIST_H__

#include

#include

#include

#include

typedef int SLTDataType;

typedef struct SListNode

{

SLTDataType _data;

struct SListNode* _next;

}SListNode;

typedef struct SList

{

SListNode* _head;

}SList;

void SListInit(SList* plist);

void SListDstory(SList* plist);

SListNode* BuySListNode(SLTDataType x);

void SListPushFront(SList* plist, SLTDataType x);

void SListPushBack(SList* plist,SLTDataType x);

void SListPopFront(SList* plist);

void SListPopBack(SList* plist);

SListNode* SListFind(SList* plist, SLTDataType x);

// 在pos的后面进行插入

void SListInsertAfter(SListNode* pos, SLTDataType x);

// 在pos的前面进行插入

void SListEraseAfter(SListNode* pos);

void SListRemove(SList* plist, SLTDataType x);

void SListPrint(SList* plist);

void TestSList();

void SListPopFront(SList* plist);

SListNode* SListFind(SList* plist, SLTDataType x);

// 在pos的后面进行插入

void SListInsertAfter(SListNode* pos, SLTDataType x);

// 在pos的前面进行插入

void SListEraseAfter(SListNode* pos);

void SListRemove(SList* plist, SLTDataType x);

void SListPrint(SList* plist);

void Listtest1();

#endif //__LIST_H__

单链表相关接口的实现List.c

#include"List.h"

void SListInit(SList* plist)

{

assert(plist);

plist->_head = NULL;

}

void SListDstory(SList* plist)

{

assert(plist);

SListNode* cur = plist->_head;

while (cur != NULL)

{

SListNode* next = cur->_next;

free(cur);

cur = next;

}

plist->_head = NULL;

}

void SListPrint(SList* plist)

{

assert(plist);

SListNode* cur = plist->_head;

while (cur != NULL)

{

printf("%d->", cur->_data);

cur = cur->_next;

}

printf("NULL");

}

SListNode* BuySListNode(SLTDataType x)

{

SListNode* pnode;

pnode = (SListNode*)malloc(sizeof(SListNode));

pnode->_data = x;

pnode->_next = NULL;

return pnode;

}

void SListPushFront(SList* plist, SLTDataType x)

{

assert(plist);

SListNode* newnode = BuySListNode(x);

newnode->_next = plist->_head;

plist->_head = newnode;

}

void SListPushBack(SList* plist, SLTDataType x)

{

assert(plist);

if (plist->_head == NULL)

{

SListNode* newnode = BuySListNode(x);

}

else

{

SListNode* tail = plist->_head;

while (tail->_next != NULL)

{

tail = tail->_next;

}

SListNode* newtail = BuySListNode(x);

tail->_next = newtail;

}

}

void SListPopFront(SList* plist)

{

assert(plist);

SListNode* next = plist->_head->_next;

free(plist->_head);

plist->_head = next;

}

void SListPopBack(SList* plist)

{

if (plist->_head->_next == NULL)

{

free(plist->_head);

plist->_head = NULL;

}

else

{

SListNode* prev = NULL;

SListNode* tail = plist->_head;

while (tail->_next != NULL)

{

prev = tail;

tail = tail->_next;

}

free(tail);

prev->_next = NULL;

}

}

SListNode* SListFind(SList* plist, SLTDataType x)

{

assert(plist);

SListNode* cur = plist->_head;

while (cur->_next != NULL)

{

if (cur->_data == x)

return cur;

else

cur = cur->_next;

}

return cur;

}

// 在pos的后面进行插入

void SListInsertAfter(SListNode* pos, SLTDataType x)

{

assert(pos);

SListNode* newnode = BuySListNode(x);

assert(newnode);

newnode->_next = pos->_next;

pos->_next = newnode;

}

// 在pos的前面进行插入

void SListEraseAfter(SListNode* pos)

{

assert(pos && pos->_next);

SListNode* next = pos->_next;

SListNode* nextnext = next->_next;

pos->_next = nextnext;

free(next);

}

void SListRemove(SList* plist, SLTDataType x)

{

assert(plist);

if (plist->_head->_next==NULL)

{

if (plist->_head->_data == x)

SListPopFront(plist);

else

printf("所删除的数不存在!\n");

}

else

{

SListNode* cur = plist->_head;

SListNode* prev = plist->_head;

while (cur->_next != NULL)

{

if (cur->_data == x)

{

prev->_next = cur->_next;

}

else

{

cur = cur->_next;

prev = prev->_next;

}

}

}

}

void Listtest1()

{

SList list;

SListInit(&list);

//SListPrint(&list);

SListPushFront(&list, 1);

SListPushFront(&list, 2);

SListPushFront(&list, 3);

SListPushFront(&list, 4);

//SListPushBack(&list, 0);

//SListPopFront(&list);

//SListPopBack(&list);

/*SListNode* pos = SListFind(&list, 3);

printf("%d\n", pos->_data);

SListInsertAfter(pos, 6);

SListEraseAfter(pos);*/

SListRemove(&list, 2);

SListPrint(&list);

SListDstory(&list);

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/270366.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hashmap中的hash扰动函数

https://www.zhihu.com/question/20733617转载于:https://www.cnblogs.com/lushilin/p/6142597.html

线程NEW状态和RUNNABLE状态

新建一个线程的时候是NEW状态 启动线程后是RUNNABLE状态

电脑知识:Win10系统优化的7个设置技巧

今天小编给大家介绍一些Win10系统优化的7个设置技巧,希望对大家能有所帮助!1、卸载Win10自带的软件Win10默认会自带很多的内置应用(地图、游戏、画图3D、Groove音乐、Skye、Xbox),大部分大家都用不到,建议卸…

c语言已知加速度求位移速度,知道初速度知道加速度求位移的公式

知道初速度知道加速度求位移的公式以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!知道初速度知道加速度求位移的公式高一物理公式总结一、质点的运动(1)------直线运动1)匀变速直线运动1.平…

IllegalThreadStateException

IllegalThreadStateException

js日期显示效果

<!DOCTYPE html><html> <head> <meta charset"UTF-8"> <title></title> </head> <body> <div id"div1"> </div> <script type"text/javascript"> window.οnlοadfunction s…

操作系统:操作系统装进U盘的图解教程

使用U盘安装操作系统的相信大家都比较熟悉了&#xff0c;如果把操作系统安装在U盘中你尝试过吗&#xff1f;操作系统安装电脑的时候是写入硬盘当中&#xff0c;U盘属于移动硬盘&#xff0c;自然也在安装范围内&#xff0c;过去只是U盘空间小无法安装&#xff0c;现在随着科技发…

c语言图片效果,c语言能显示图片吗

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼/* Svga64k.bgi 测试文件 */#include "graphics.h"#include "Svga256.h"#include "stdio.h"#include "fcntl.h"#include "malloc.h"#include "io.h"int huge Return_…

互斥量和信号量的区别

1. 互斥量用于线程的互斥&#xff0c;信号量用于线程的同步。 这是互斥量和信号量的根本区别&#xff0c;也就是互斥和同步之间的区别。 互斥&#xff1a;是指某一资源同时只允许一个访问者对其进行访问&#xff0c;具有唯一性和排它性。但互斥无法限制访问者对资源的访问顺序…

网络知识:WiFi越用越慢,到底是什么原因

WiFi越用越慢&#xff0c;到底是什么原因&#xff1f; 有人认为是WiFi盒子有问题&#xff0c;但其实和路由器的错误摆放也有关系。 今天&#xff0c;小编特地百度了一下&#xff0c;列出了几个路由器正确摆放的小常识&#xff0c;而且不用花一分钱&#xff0c;就可以测试出家中…

转: vim 的编辑格式设置

http://www.cnblogs.com/freewater/archive/2011/08/26/2154602.html :set encodingutf-8:set fileencodingsucs-bom,utf-8,cp936:set fileencodinggb2312:set termencodingutf-8转载于:https://www.cnblogs.com/jhj117/p/6149545.html

linux 连接数 限制,linux设置最大连接数

1.最大文件打开数限制查看前用户进程打开的文件数限制&#xff0c;命令行执行&#xff1a;ulimit -n默认1024.这表示当前用户的每个进程最多允许同时打开1024个文件&#xff0c;这1024个文件中还得除去每个进程必然打开的标准输入&#xff0c;标准输出&#xff0c;标准错误&…

什么叫网络抖动

网络抖动&#xff1a; 网上说法是指网络中的延迟是指信息从发送到接收经过的延迟时间&#xff0c;一般由传输延迟及处理延迟组成&#xff1b; 而抖动是指最大延迟与最小延迟的时间差&#xff0c;如最大延迟是20毫秒&#xff0c;最小延迟为5毫秒&#xff0c;那么网络抖动就是15毫…

硬件知识:电脑组装机必备的知识梳理

今天小编给大家分享电脑组装机必备的知识&#xff0c;希望对大家能有所帮助&#xff01; 确实机箱内部的硬件连接中&#xff0c;大多数线材的插头和插孔都是独特的&#xff0c;比如主板的204pin&#xff0c;CPU的44pin都不能通用&#xff0c;多接口中方口和圆口的搭配也不会导致…

多态、抽象类

多态、抽象类 多态&#xff1a; 多态&#xff1a;多种形式&#xff1b; 多态是指一个对象有多种形式的能力&#xff1b; 多态描述&#xff1a;相同的行为&#xff1b;不同的实现&#xff1b; 多态分类&#xff1a; 静态多态&#xff1a;程序在编译时&#xff0c;系统就能决定调…

android 首页布局变换,Android XML布局与View之间的转换

Android的布局方式有两种&#xff0c;一种是通过xml布局&#xff0c;一种是通过java代码布局&#xff0c;两种布局方式各有各的好处&#xff0c;当然也可以相互混合使用。很多人都习惯用xml布局&#xff0c;那xml布局是如何转换成view的呢&#xff1f;本文从源码的角度来简单分…

C++的ORM工具比较

用过Java的都知道SSH框架&#xff0c;特别对于数据库开发&#xff0c;Java领域有无数的ORM框架&#xff0c;供数据持久层调用&#xff0c;如Hibernate&#xff0c;iBatis(现在改名叫MyBatis)&#xff0c;TopLink&#xff0c;JDO&#xff0c;JPA……非常方便实用。用过C#的同学们…

电脑技巧:Win10自带存储感知功能给电脑磁盘瘦身

今天给大家分享Win10自带存储感知功能给电脑磁盘瘦身功能&#xff0c;希望对大家能有所帮助&#xff01;1、什么是存储感知Win10存储感知功能属于Win10操作系统的一大亮点&#xff0c;自带有AI的存储感知功能发挥其磁盘清理功能&#xff0c;它可以在操作系统需要的情况下清理不…

线程的优先级

setPriority(); 设置线程的优先级Thread类里面的 MIN_PRIORITY 1 表示最小优先级 NORM_PRIORITY 5 表示默认优先级 MAX_PRIORITY 10 表示最大优先级