
LinkList.h
#pragma once
#pragma pack(1)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef __cpulspuls
extern "C" {
#endif
typedef struct linkNode
{
int Value;
struct linkNode *Next;
}LINKNODE;
struct linkNode *Init_List();
LINKNODE * Insert_List(LINKNODE *header, int val);
LINKNODE * InsertByPos_List(LINKNODE *header, int pos, int val);
int DeleteByVal_List(LINKNODE *header, int val);
int DeleteByPos_List(LINKNODE *header, int pos);
void Foreach_List(LINKNODE *header);
void Destory_List(LINKNODE *header);
LINKNODE *Empty_List(LINKNODE *header);
#ifdef __cpulspuls
}
#endif
LinkList.c
#include "LinkList.h"
LINKNODE * Init_List()
{
LINKNODE *header = malloc(sizeof(LINKNODE));
header->Next = NULL;
header->Value = -1;
return header;
}
LINKNODE *Insert_List(LINKNODE * header, int val)
{
if (header == NULL)
return NULL;
LINKNODE *New = malloc(sizeof(LINKNODE));
New->Next = NULL;
New->Value = val;
LINKNODE *pCurrent = header;
while (pCurrent->Next != NULL)
{
pCurrent = pCurrent->Next;
}
pCurrent->Next = New;
return header;
}
LINKNODE *InsertByPos_List(LINKNODE * header, int pos, int val)
{
if (header == NULL)
return NULL;
int i = 0;
LINKNODE *New = malloc(sizeof(LINKNODE));
New->Value = val;
New->Next = NULL;
LINKNODE *pCurrent = header->Next;
LINKNODE *pPrev = header;
while (pCurrent->Next != NULL)
{
++i;
if (i == pos)
break;
pPrev = pCurrent;
pCurrent = pCurrent->Next;
}
if (pCurrent->Next == NULL)
{
pCurrent->Next = New;
return header;
}
pPrev->Next = New;
New->Next = pCurrent;
return header;
}
int DeleteByVal_List(LINKNODE * header, int val)
{
if (header == NULL)
return -1;
LINKNODE *pPrev = header;
LINKNODE *pCurrent = header->Next;
while (pCurrent->Next != NULL)
{
if (pCurrent->Value == val)
{
break;
}
pPrev = pCurrent;
pCurrent = pCurrent->Next;
}
if (pCurrent->Next == NULL && pCurrent->Value != val)
return -1;
pPrev->Next = pCurrent->Next;
free(pCurrent);
pCurrent = NULL;
return 0;
}
int DeleteByPos_List(LINKNODE * header, int pos)
{
if (pos <= 0 || header == NULL)
return -1;
LINKNODE *pPrev = header;
LINKNODE *pCurrent = header->Next;
int i = 0;
while (pCurrent != NULL)
{
++i;
if (pos == i)
break;
pPrev = pCurrent;
pCurrent = pCurrent->Next;
}
if (pos > i)
return -1;
pPrev->Next = pCurrent->Next;
free(pCurrent);
pCurrent = NULL;
return 0;
}
void Foreach_List(LINKNODE * header)
{
if (header == NULL)
return;
LINKNODE *pCurrent = header;
while (pCurrent->Next != NULL)
{
pCurrent = pCurrent->Next;
printf("%d ", pCurrent->Value);
}
printf("\n");
}
void Destory_List(LINKNODE * header)
{
if (header == NULL)
return;
LINKNODE *pCurrent = header;
LINKNODE *pRear = header->Next;
while (pCurrent->Next != NULL)
{
free(pCurrent);
pCurrent = pRear;
pRear = pCurrent->Next;
}
}
LINKNODE *Empty_List(LINKNODE * header)
{
LINKNODE *pCurrent = header->Next;
LINKNODE *pRear = pCurrent->Next;
header->Next = NULL;
while (pCurrent->Next != NULL)
{
free(pCurrent);
pCurrent = pRear;
pRear = pCurrent->Next;
}
return header;
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "LinkList.h"
int main()
{
LINKNODE *header = Init_List();
Insert_List(header, 1);
Insert_List(header, 2);
Insert_List(header, 3);
Foreach_List(header);
printf("************************\n");
InsertByPos_List(header, 2, 5);
InsertByPos_List(header, -1, 10);
InsertByPos_List(header, 20, 10);
Foreach_List(header);
printf("************************\n");
DeleteByVal_List(header, 5);
DeleteByVal_List(header, 10);
DeleteByVal_List(header, 10);
DeleteByPos_List(header, 2);
Foreach_List(header);
printf("************************\n");
Empty_List(header);
Foreach_List(header);
printf("************************\n");
Insert_List(header, 1);
Insert_List(header, 2);
Insert_List(header, 3);
Foreach_List(header);
printf("************************\n");
Destory_List(header);
system("pause");
return 0;
}