- Node.h
#ifndef NODE_H
#define NODE_H
typedef struct NODE
{
int value;
struct NODE* next;
}Node,*pNode;
#endif // NODE_H
- List.h
#ifndef LIST_H
#define LIST_H
#include "Node.h"
void InsertByHead(pNode *pHead, int value);
void InsertByTail(pNode *pHead, int value);
void DeleteByHead(pNode *pHead);
void DeleteByTail(pNode *pHead);
pNode FindByValue(pNode pHead, int value);
pNode FindByIndex(pNode pHead, int index);
void UpdateByValue(pNode pHead, int oldval, int newval);
int GetLength(pNode pHead);
void UpdateByIndex(pNode pHead, int index, int value);
void InsertByIndex(pNode *pHead, int index, int value);
void DeleteByValue(pNode *pHead, int value);
void DeleteByIndex(pNode *pHead, int index);
void Reverse(pNode *pHead);
void ReverseLink(pNode *pHead);
void Clear(pNode *pHead);
void ShowList(pNode pHead);
#endif // !LIST_H
- List.c
void InsertByHead(pNode *pHead, int value)
{
pNode pNew = (pNode)malloc(sizeof(Node));
if (NULL == pNew)
{
printf("malloc free failed!\n ");
return;
}
pNew->value = value;
pNew->next = *pHead;
*pHead = pNew;
}
void InsertByTail(pNode *pHead, int value)
{
if (NULL ==*pHead)
{
InsertByHead(pHead, value);
}
else
{
pNode pNew = (pNode)malloc(sizeof(Node));
if (NULL == pNew)
{
printf("malloc free failed!\n ");
return;
}
pNew->value = value;
pNew->next = NULL;
pNode pos = *pHead;
while (pos->next != NULL)
{
pos = pos->next;
}
pos->next = pNew;
}
}
void DeleteByHead(pNode *pHead)
{
if (*pHead != NULL)
{
pNode pos = *pHead;
*pHead = pos->next;
free(pos);
pos = NULL;
}
}
void DeleteByTail(pNode *pHead)
{
if (NULL ==(*pHead)->next || NULL == *pHead)
{
DeleteByHead(pHead);
}
else
{
pNode pos = *pHead;
while (pos->next->next != NULL)
{
pos = pos->next;
}
free(pos->next);
pos->next = NULL;
}
}
pNode FindByValue(pNode pHead, int value)
{
pNode pos = pHead;
while (pos != NULL)
{
if (pos->value != value)
{
pos = pos->next;
}
else
break;
}
return pos;
}
void UpdateByValue(pNode pHead, int oldval, int newval)
{
pNode pos = FindByValue(pHead, oldval);
while (pos != NULL&&pos->value == oldval)
{
pos->value = newval;
pos = FindByValue(pHead, oldval);
}
}
int GetLength(pNode pHead)
{
pNode pos = pHead;
int count = 0;
while (pos != NULL)
{
pos = pos->next;
count++;
}
return count;
}
pNode FindByIndex(pNode pHead, int index)
{
pNode pos = pHead;
if (index > 0 && index < GetLength(pHead) - 1)
{
for (int i = 0; i < index&&pos != NULL; i++)
{
pos = pos->next;
}
}
return pos;
}
void UpdateByIndex(pNode pHead, int index, int value)
{
pNode pos = FindByIndex(pHead, index);
if (pos != NULL)
{
pos->value = value;
}
}
void InsertByIndex(pNode *pHead, int index, int value)
{
pNode pos = *pHead;
if (0 == index )
{
InsertByHead(pHead, value);
}
else if (index > 0 && index <= GetLength(*pHead))
{
for (int i = 1; i < index && pos->next != NULL; i++)
{
pos = pos->next;
}
pNode pNew = (pNode)malloc(sizeof(Node));
if (NULL == pNew)
{
printf("malloc free failed!\n ");
return;
}
pNew->value = value;
pNew->next = pos->next;
pos->next = pNew;
}
}
void Reverse(pNode *pHead)//非递归(迭代)
{
if (NULL == *pHead)
{
return;
}
else
{
pNode p1 = *pHead;
pNode p2 = (*pHead)->next;
pNode p3 = NULL;
while (p2!=NULL)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;//p1指向p2结点
p2 = p3;//p2指向p3结点
}
(*pHead)->next = NULL;
*pHead = p1;
}
}
void ReverseLink(pNode *pHead)//递归
{
if (NULL == *pHead)
{
return ;
}
else
{
ReverseLink(&(*pHead)->next);//回溯部分
printf("%d\t", (*pHead)->value);
//(*pHead)->next = NULL;
}
}
void DeleteByValue(pNode *pHead, int value)
{
pNode pos = *pHead;
while (pos->next != NULL)
{
if (pos->next->value != value)
{
pos = pos->next;
}
else
{
pNode temp = pos->next;
pos->next = temp->next;
free(temp);
temp = NULL;
}
}
if ((*pHead)->value == value)
{
DeleteByHead(pHead);
}
}
void DeleteByIndex(pNode *pHead, int index)
{
if (0 == index)
{
DeleteByHead(pHead);
}
else if (index > 0 && index*pHead))
{
pNode pos = *pHead;
for (int i = 0; i < index - 1 && pos->next != NULL; i++)
{
pos = pos->next;
}
pNode temp = pos->next;
pos->next = temp->next;
free(temp);
temp = NULL;
}
}
void Clear(pNode *pHead)
{
while ((*pHead) != NULL)
{
DeleteByHead(pHead);
}
}
void ShowList(pNode pHead)
{
pNode pos = pHead;
while (pos != NULL)
{
printf("%d\t", pos->value);
pos = pos->next;
}
printf("\n");
}
- main.c
int main(void)
{
pNode pHead = NULL;
InsertByTail(&pHead, 1);
InsertByTail(&pHead, 4);
InsertByTail(&pHead, 2);
InsertByTail(&pHead, 8);
ReverseLink(&pHead);
return 0;
}