【C】单链表的实现

SListNode.h

#ifndef _SLISTNODE_H_
#define _SLISTNODE_H_
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<malloc.h>
#include<windows.h>

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"

//初始化
SListNode* Init(){
	SListNode* head = (SListNode *)malloc(sizeof(SListNode));
	head->_next=NULL;
	return head;
}

//创建一个结点
SListNode* BuySListNode(DataType x){
	SListNode *node = (SListNode *)malloc(sizeof(SListNode));
	node->_data=x;
	node->_next=NULL;
	return node;
}

//打印单链表
void SListPrint(SListNode* pHead){

	SListNode* cur;
	assert(pHead);
	cur = pHead->_next;
	while(cur){
		printf("%d ",cur->_data);
		cur=cur->_next;
	}
	printf("
");
}

//销毁
void SListDestory(SListNode** ppHead){
	SListNode *cur,*t;
	assert(ppHead);
	cur = *ppHead;
	while(cur){
		t = cur->_next;
		free(cur);
		cur = t;
	}
}

//尾插;
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){
	SListNode *cur,*pr;
	if((*ppHead)->_next==NULL){
		printf("表空
");
		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 *head,*temp;
	assert(ppHead);
	if((*ppHead)->_next==NULL){
		printf("空表
");
		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
");
			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);
	SListPopBack(&list);
	SListPopBack(&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);
	SListPopBack(&list);
	SListPopBack(&list);
	SListPrint(list);
	node=SListFind(list,5);
	if(node){
		printf("%d 
",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);
	SListPopFront(&list);
	SListPopFront(&list);
	SListPopFront(&list);
	SListPrint(list);

}


main.c

#include"SListNode.h"

int main(){
	test1();
	system("pause");
	return 0;
}



原文地址:https://www.cnblogs.com/yongtaochang/p/13615378.html