Code tutorial for implementing a single-chain table C and code tutorial for a single-chain table c
SListNode. h
#ifndef _SLISTNODE_H_#define _SLISTNODE_H_#include
#include#include
#include
#include
typedef int DataType;typedef struct SListNode { struct SListNode* _next; DataType _data; }SListNode;SListNode* Init();SListNode* BuySListNode(DataType x); void SListPrint(SListNode* pHead); void SListDestory(SListNode** ppHead); void SListPushBack(SListNode** ppHead, DataType x); void SListPopBack(SListNode** ppHead); void SListPushFront(SListNode** ppHead, DataType x); void SListPopFront(SListNode** ppHead); SListNode* SListFind(SListNode* pHead, DataType x); void SListInsert(SListNode** ppHead, SListNode* pos, DataType x); void SListErase(SListNode** ppHead, SListNode* pos); void test1();void test2();#endif
SListNode. c
# Include "SListNode. h "// initialize SListNode * Init () {SListNode * head = (SListNode *) malloc (sizeof (SListNode); head-> _ next = NULL; return head ;} // create a node SListNode * BuySListNode (DataType x) {SListNode * node = (SListNode *) malloc (sizeof (SListNode); node-> _ data = x; node-> _ next = NULL; return node;} // print the void SListPrint (SListNode * pHead) {SListNode * cur; assert (pHead ); cur = pHead-> _ next; while (cur) {printf ("% d", cur-> _ data ); Cur = cur-> _ next;} printf ("\ n");} // destroy void SListDestory (SListNode ** ppHead) {SListNode * cur, * t; assert (ppHead); cur = * ppHead; while (cur) {t = cur-> _ next; free (cur); cur = t ;}/// end plug; void SListPushBack (SListNode ** ppHead, DataType x) {SListNode * newNode = BuySListNode (x); SListNode * cur; assert (ppHead); cur = * ppHead; while (cur-> _ next) {cur = cur-> _ next;} cur-> _ next = newNode;} // void SListPopBack (SListNode ** ppHead) {SLis TNode * cur, * pr; if (* ppHead)-> _ next = NULL) {printf ("Table blank \ n"); return ;} pr = cur = * ppHead; assert (ppHead); while (cur-> _ next) {pr = cur; cur = cur-> _ next;} free (cur ); pr-> _ next = NULL;} void SListPushFront (SListNode ** ppHead, DataType x) {SListNode * head, * newNode; assert (ppHead); head = * ppHead; newNode = BuySListNode (x); newNode-> _ next = head-> _ next; head-> _ next = newNode;} void SListPopFront (SListNode ** ppHead) {SListNode * hea D, * temp; assert (ppHead); if (* ppHead)-> _ next = NULL) {printf ("empty table \ n"); return ;} temp = head = * ppHead; temp = temp-> _ next; head-> _ next = temp-> _ next; free (temp );} SListNode * SListFind (SListNode * pHead, DataType x) {SListNode * cur; assert (pHead); cur = pHead; while (cur) {if (cur-> _ data = x) {printf ("zhaodaole \ n"); return cur ;}cur = cur-> _ next;} return NULL ;} void SListInsert (SListNode ** ppHead, SListNode * pos, DataType X) {SListNode * cur, * pr, * newNode; newNode = BuySListNode (x); assert (ppHead); assert (pos); pr = cur = * ppHead; while (cur & cur! = Pos) {pr = cur; cur = cur-> _ next;} if (cur) {newNode-> _ next = cur; pr-> _ next = newNode ;}} void SListErase (SListNode ** ppHead, SListNode * pos) {SListNode * cur, * pr; assert (ppHead); assert (pos); pr = cur = * ppHead; while (cur & cur! = Pos) {pr = cur; cur = cur-> _ next;} pr-> _ next = cur-> _ next; free (cur);} void test1 () {SListNode * list = Init (); SListPushBack (& list, 1); SListPushBack (& list, 2); SListPushBack (& list, 3); SListPushBack (& list, 4); SListPushBack (& list, 5); SListPrint (list); SListPopBack (& list ); SListPopBack (& list); SListPrint (list); SListDestory (& list);} void test2 () {SListNode * node; SListNode * list = Init (); SListPushFront (& list, 1); SListPushFront (& list, 2); SListPushFront (& list, 3); SListPushFront (& list, 4 ); SListPushFront (& list, 5); SListPrint (list); SListPopBack (& list); SListPrint (list ); node = SListFind (list, 5); if (node) {printf ("% d \ n", node-> _ data);} SListInsert (& list, node, 6); SListInsert (& list, node, 7); SListInsert (& list, node, 8); SListPrint (list); node = SListFind (list, 6 ); SListErase (& list, node); SListPrint (list); SListPopFront (& list ); SListPopFront (& list); SListPrint (list );}
Main. c
#include"SListNode.h"int main(){test1();system("pause");return 0;}