> 文章列表 > 单链表(C语言)

单链表(C语言)

单链表(C语言)

SList.h文件

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>typedef int SLTDateType;typedef struct SListNode
{SLTDateType data;struct SListNode* next;
}SLTNode;void Print(SLTNode* phead);
void Push_Back(SLTNode** phead, SLTDateType x);
void Push_Front(SLTNode** phead, SLTDateType x);
void Pop_Back(SLTNode** phead);
void Pop_Front(SLTNode** phead);
void Pop_Pos(SLTNode** phead, SLTNode* pos);
void Push_Pos(SLTNode** phead, SLTNode* pos, SLTDateType x);
SLTNode* Buy_Node(SLTDateType x);
SLTNode* Find(SLTNode* phead, SLTDateType x);

SList.c

#include"SList.h"void Print(SLTNode* phead)
{SLTNode* cur=phead;while (cur != NULL){printf("%d->", cur->data);cur = cur->next;}printf("NULL\\n");
}SLTNode* Buy_Node(SLTDateType x)
{SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));if (newnode == NULL){perror("malloc fail");return NULL;}newnode->data = x;newnode->next = NULL;return newnode;
}void Push_Back(SLTNode** phead, SLTDateType x)
{SLTNode* newnode= Buy_Node(x);if (*phead == NULL){*phead = newnode;}else{SLTNode* cur = *phead;while (cur->next != NULL){cur = cur->next;}cur->next = newnode;}
}void Push_Front(SLTNode** phead, SLTDateType x)
{assert(phead);SLTNode* newnode = Buy_Node(x);newnode->next = *phead;*phead = newnode; 
}void Pop_Back(SLTNode** phead)
{//如果没有节点if ((*phead) == NULL){return;}//assert(*phead!=NULL);//如果只有一个节点if ((*phead)->next == NULL){free(*phead);*phead = NULL;}else{SLTNode* tail = *phead;while (tail->next->next != NULL){tail = tail->next;}free(tail->next);tail->next = NULL;}
}void Pop_Front(SLTNode** phead)
{if (*phead == NULL){return;}SLTNode* head = *phead;*phead = (*phead)->next;free(head);head = NULL;
}//给定位置
//void Push_Pos(SLTNode** phead, SLTDateType x, SLTDateType pos)
//{
//	assert(phead);
//	if (pos == 0)
//	{
//		Push_Front(*phead,x);
//		return;
//	}
//	SLTNode* first = *phead;
//	SLTNode* second = *phead;
//	SLTDateType cnt = 0;//计数
//	while (cnt != pos-1)
//	{
//		first = second;
//		second = second->next;
//		cnt++;
//	}
//	SLTNode* newnode = Buy_Node(x);
//	first->next = newnode;
//	newnode->next = second;
//
//}SLTNode* Find(SLTNode* phead, SLTDateType x)
{SLTNode* cur = phead;while (cur != NULL){if (cur->data == x){return cur;}cur = cur->next;}return NULL;
}在pos位置之前插入
//void Push_Pos(SLTNode** phead, SLTNode* pos, SLTDateType x)
//{
//	assert(phead);
//	assert(pos);
//	if (pos == *phead)
//	{
//		Push_Front(phead, x);
//	}
//	else
//	{
//		SLTNode* prev = *phead;
//		while (prev->next != pos)
//		{
//			prev = prev->next;
//		}
//		SLTNode* newnode = Buy_Node(x);
//		prev->next = newnode;
//		newnode->next = pos;
//	}
//}//在pos位置之后插入
void Push_Pos(SLTNode** phead, SLTNode* pos, SLTDateType x)
{assert(phead);assert(pos);if (pos == *phead){Push_Front(phead, x);}else{SLTNode* tail = pos->next;SLTNode* newnode = Buy_Node(x);pos->next = newnode;newnode->next = tail;}
}void Pop_Pos(SLTNode** phead, SLTNode* pos)
{assert(phead);assert(pos);if (pos == *phead){Pop_Front(phead);}else{SLTNode* prev = *phead;while (prev->next != pos){prev = prev->next;}prev->next = pos->next;free(pos);//pos = NULL;//为什么不需要让pos=NULL}
}

test.c

#include"SList.h"void TestSList1()
{SLTNode* plist=NULL;Push_Back(&plist, 1);Push_Back(&plist, 2);Push_Back(&plist, 3);Print(plist);SLTNode* ret = Find(plist, 1);SLTNode* ret1 = Find(plist, 2);Push_Pos(&plist, ret, 100);Print(plist);Pop_Pos(&plist, ret1);Print(plist);
}int main(void)
{TestSList1();return 0;
}

单链表(C语言)