设为首页 加入收藏

TOP

C单链表的实现代码教程(一)
2018-01-03 06:06:37 】 浏览:457
Tags:单链表 实现 代码 教程

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"

//初始化
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("\n");
}

//销毁
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("表空\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 *head,*temp;
	assert(ppHead);
	if((*ppHead)->_next==NULL){
		printf("空表\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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇printf()函数详细了解 下一篇数据结构C语言:单链表

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目