单链表的基本操作(不带头结点)
程序员文章站
2022-03-15 17:31:08
...
- 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
#include "Node.h"
#include <stdio.h>
#include <malloc.h>
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<GetLength(*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);
//ShowList(pHead);
//InsertByTail(&pHead, 1);
//DeleteByHead(&pHead);
//UpdateByValue(pHead, 1, 23);
//UpdateByIndex(pHead, 2, 55);
//InsertByIndex(&pHead, -1, 33);
//DeleteByIndex(&pHead,0);
//Clear(&pHead);
//Reverse(&pHead);
ReverseLink(&pHead);
//ShowList(pHead);
return 0;
}
上一篇: tp 递归查找上下级
下一篇: C++中宏与内联函数的优缺点