SHList.h
#pragma once
typedef int DataType;
typedef struct SHListNode
{
DataType data;
struct SHListNode* next;
}Node;
void SHListInit(Node** head);
void SHListPushBack(Node* head, DataType data);
void SHListPopBack(Node* head);
void SHListPushFront(Node* head, DataType data);
void SHListPopFront(Node* head);
void SHListInsert(Node* pos, DataType data);
void SHListErase(Node* pos);
int SHListSize(Node* head);
Node* SHListFind(Node* head, DataType data);
void SHListDestroy(Node** head);
void SHListPrint(Node* head);
SHList.c
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "SHList.h"
Node* NewSHListNode(DataType data)
{
Node* node = (Node*)malloc(sizeof(Node));
if(NULL == node)
{
assert(0);
return NULL;
}
node->next = NULL;
node->data = data;
return node;
}
void SHListInit(Node** head)
{
assert(head);
*head = NewSHListNode(0);
}
void SHListPushBack(Node* head, DataType data)
{
assert(head);
Node* cur = head;
while(cur->next)
{
cur = cur->next;
}
cur->next = NewSHListNode(data);
}
void SHListPopBack(Node* head)
{
assert(head);
Node* cur = NULL;
Node* pre = NULL;
if(NULL == head->next)
return;
cur = head->next;
pre = head;
while(cur->next)
{
pre = cur;
cur = cur->next;
}
free(cur);
pre->next = NULL;
}
void SHListPushFront(Node* head, DataType data)
{
assert(head);
Node* newNode = NewSHListNode(data);
newNode->next = head->next;
head->next = newNode;
}
void SHListPopFront(Node* head)
{
assert(head);
if(NULL == head->next)
return;
Node* cur = head->next;
head->next = cur->next;
free(cur);
}
void SHListInsert(Node* pos, DataType data)
{
if(NULL == pos)
return;
Node* newNode = NewSHListNode(data);
newNode->next = pos->next;
pos->next = newNode;
}
void SHListErase(Node* pos)
{
if(NULL == pos || NULL == pos->next)
return;
Node* cur = pos->next;
pos->next = cur->next;
free(cur);
}
int SHListSize(Node* head)
{
int count = 0;
Node* cur = head->next;
while(cur)
{
++count;
cur = cur->next;
}
return count;
}
Node* SHListFind(Node* head, DataType data)
{
assert(head);
Node* cur = head->next;
while(cur)
{
if(data == cur->data)
return cur;
cur = cur->next;
}
return NULL;
}
void SHListDestroy(Node** head)
{
Node* cur1 = (*head)->next;
Node* cur2 = NULL;
while(NULL != cur1)
{
cur2 = cur1;
free(cur1);
cur1 = cur2->next;
}
free(*head);
*head = NULL;
}
void SHListPrint(Node* head)
{
Node* cur = head->next;
while(cur)
{
printf("%d--->",cur->data);
cur = cur->next;
}
printf("NULL\n");
}
test.c
#include <stdio.h>
#include "SHList.h"
int main()
{
Node* head;
SHListInit(&head);
SHListPushBack(head,1);
SHListPushFront(head,0);
SHListSize(head);
SHListPrint(head);
SHListPopFront(head);
SHListSize(head);
SHListPrint(head);
SHListPushBack(head,2);
SHListPushBack(head,3);
SHListPushBack(head,4);
SHListPushBack(head,4);
SHListPushBack(head,5);
SHListPushBack(head,6);
SHListPopBack(head);
SHListSize(head);
SHListPrint(head);
SHListInsert(SHListFind(head,2),8);
SHListErase(SHListFind(head,4));
SHListSize(head);
SHListPrint(head);
SHListDestroy(&head);
SHListPrint(head);
}