C中数据结构单链表的基本操作

  1. #ifndef SList__h
    #define SList__h

    #define _CRT_SECURE_NO_WARNINGS 1
    
    #include 
    #include 
    
    typedef struct SListNode
    {
    	int data;
    	struct SListNode *nest;
    }SListNode;
    
    typedef struct SList
    {
    	SListNode *first;
    }SList;
    
    void SListInit(SList *list);
    SListNode *BuySListNode(int data);
    void SListPushFront(SList *list, int data);
    void SListDelFront(SList *list);
    void SListPushBuck(SList *list, int data);
    void SListDelBack(SList *list);
    void SListNodePrint(SList *list);
    SListNode *SListNodeFind(SList *list, int data);
    void SListInsertAfter(SListNode *pos, int data);
    void SListEarseAfter(SListNode *pos);
    void SListRemove(SList *list, int data);
    
    
    #endif //__SList__h__
    
    #include "SList.h"
    
    void SListInit(SList *list)   //坑B浪费了爸爸十几分钟,测试前一定加上
    {
    	assert(list != NULL);    
    	list->first = NULL;      //初始化
    }
    
    SListNode *BuySListNode(int data)    //创建一个节点
    {
    	SListNode *node = (SListNode*)malloc(sizeof(SListNode));
    	assert(node != NULL);
    	node->data = data;
    	node->nest = NULL;
    
    	return node;
    }
    
    void SListPushFront(SList *list, int data)    //头插
    {
    	assert(list != NULL);
    	SListNode *node = BuySListNode(data);    
    	node->nest = list->first;                   
    	list->first = node;
    }
    
    void SListDelFront(SList *list)
    {
    	assert(list != NULL);
    	assert(list->first != NULL);
    	SListNode *OldFirst = list->first;
    	list->first = list->first->nest;
    	free(OldFirst);
    }
    
    void SListPushBuck(SList *list, int data)
    {
    	assert(list != NULL);
    	if (list->first == NULL)
    	{
    		SListPushFront(list, data);
    	}
    	SListNode *node = BuySListNode(data);
    	SListNode *lastone = list->first;
    	for (; lastone->nest != NULL; lastone = lastone->nest)
    	{
    		;
    	}
    
    	lastone->nest = node;
    }
    
    void SListDelBack(SList *list)
    {
    	assert(list != NULL);
    	assert(list->first != NULL);
    	SListNode *cur = list->first;
    	for (; cur->nest->nest != NULL; cur = cur->nest)
    	{
    		;
    	}
    
    	free(cur->nest);
    	cur->nest = NULL;
    }
    
    void SListNodePrint(SList *list)
    {
    	assert(list != NULL);
    	assert(list->first != NULL);
    	SListNode *cur = list->first;
    	for (; cur != NULL; cur = cur->nest)
    	{
    		printf("%d--->", cur->data);
    	}
    	printf("NULL\n");
    }
    
    SListNode *SListNodeFind(SList *list, int data)
    {
    	assert(list != NULL);
    	assert(list->first != NULL);
    	SListNode *cur = list->first;
    	for (; cur != NULL; cur = cur->nest)
    	{
    		if (cur->data == data)
    		{
    			return cur;
    		}
    	}
    	return NULL;
    }
    
    void SListInsertAfter(SListNode *pos, int data)
    {
    	assert(pos != NULL);
    	SListNode *node = BuySListNode(data);
    	node->nest = pos->nest;
    	pos->nest = node;
    }
    
    void SListEarseAfter(SListNode *pos)
    {
    	assert(pos != NULL);
    	SListNode *nest = pos->nest->nest;
    	free(pos->nest);
    	pos->nest = nest;
    }
    
    void SListRemove(SList *list, int data)
    {
    	assert(list != NULL);
    	assert(list->first != NULL);
    	
    	SListNode *prev = NULL;
    	SListNode *cur = list->first;
    	while (cur != NULL && cur->data != data)
    	{
    		prev = cur;
    		cur = cur->nest;
    	}
    
    	if (cur == NULL)
    	{
    		return 0;
    	}
    
    	if (prev == NULL)
    	{
    		SListDelFront(list);
    	}
    	prev->nest = cur->nest;
    	free(cur);
    }
    
    #include "SList.h"
    
    int main()
    {
    	SList list;
    	SListNode *cur;
    	SListInit(&list);
    	SListPushFront(&list, 1);
    	SListPushFront(&list, 3);
    	SListPushFront(&list, 2);
    	SListNodePrint(&list);
    	SListPushBuck(&list, 4);
    	SListPushBuck(&list, 6);
    	SListPushBuck(&list, 8);
    	SListNodePrint(&list);
    	/*SListDelFront(&list);
    	SListDelFront(&list);
    	SListNodePrint(&list);
    	SListDelBack(&list);
    	SListDelBack(&list);
    	SListNodePrint(&list);*/
    	cur = SListNodeFind(&list, 3);
    	SListInsertAfter(cur, 9);
    	SListNodePrint(&list);
    	SListEarseAfter(cur);
    	SListNodePrint(&list);
    	SListRemove(&list, 6);
    	SListNodePrint(&list);
    
    
    
    
    	system("pause");
    	return 0;
    }
    

你可能感兴趣的:(C中数据结构单链表的基本操作)