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