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.pswp.cn/news/270366.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/270366.shtml
英文地址,請注明出處:http://en.pswp.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 表示最大優先級