·什么是链表
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
如下图:
·代码及其实现
·Linklist.h
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>
typedef int DataType;
typedef struct Node Node;
typedef struct Node* pNode;
typedef struct Node List;
typedef struct Node* pList;
struct Node
{
DataType data;
struct Node* next;
};
void InitLinkList(pList* pplist);
pNode BuyNode(DataType d);
void DestroyLinkList(pList* pplist);
void PushBack(pList plist, DataType d);
void PopBack(pList* pplist);
void PushFront(pList* pplist, DataType d);
void PopFront(pList* pplist);
pNode Find(pList* pplist, DataType d);
void Print(pList* pplist);
//在指定位置之前插入一个值
void Insert(pList* pplist, pNode pos, DataType d);
//指定位置删除
void Erase(pList* pplist, pNode pos);
void Remove(pList* pplist, DataType d);
void RemoveAll(pList* pplist, DataType d);
void EraseNotTailNode(pNode pos);
void PrintLinkList(pList plist);
int GetListLength(pList plist);
//链表面试题
//1. 逆序打印单项链表
void PrintReverse(pList plist);
void Reverse(pList plist, pList *newplist);
#endif //__LINKLIST_H__
·Linklist.c
#include "linklist.h"
void test()
{
pList* pplist = NULL;
pList* newpplist = NULL;
InitLinkList(&pplist);
PushFront(&pplist, 1);
PushFront(&pplist, 2);
PushFront(&pplist, 3);
PushFront(&pplist, 4);
Erase(&pplist, Find(&pplist, 3));
Print(&pplist);
}
int main()
{
test();
system("pause");
return 0;
}
int GetListLength(pList plist)
{
assert(plist);
pNode cur = plist;
int count = 0;
while (cur)
{
cur = cur->next;
count++;
}
return count;
}
void Reverse(pList plist,pList *newplist)
{
pNode cur = plist;
while (cur)
{
PushFront(newplist, cur->data);
cur = cur->next;
}
Print(newplist);
}
void PrintReverse(pList plist)
{
pNode cur = plist;
if (cur->next)
{
PrintReverse(cur->next);
printf("%d->", cur->data);
}
else if (cur->next == NULL)
{
printf("%d->", cur->data);
}
}
pNode BuyNode(DataType d)
{
pNode newNode = (pNode)malloc(sizeof(Node));
if (NULL == newNode)
{
perror("malloc");
exit(EXIT_FAILURE);
}
newNode->data = d;
newNode->next = NULL;
return newNode;
}
void InitLinkList(pList* pplist)
{
assert(pplist);
*pplist = NULL;
}
void Print(pList* pplist)
{
pNode cur = *pplist;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
void PushBack(pList* pplist, DataType d)
{
assert(pplist);
pNode newNode = BuyNode(d);
pNode cur = *pplist;
if (NULL == *pplist)
{
*pplist = newNode;
return;
}
else
{
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newNode;
}
}
void PopBack(pList* pplist)
{
assert(pplist);
pNode cur = *pplist;
pNode pre = NULL;
while (cur->next != NULL)
{
pre = cur;
cur = cur->next;
}
pre->next = NULL;
free(cur);
}
void PushFront(pList* pplist, DataType d)
{
assert(pplist);
pNode newNode = BuyNode(d);
if (NULL == *pplist)
{
*pplist = newNode;
return;
}
else
{
newNode->next = *pplist;
*pplist = newNode;
}
}
void PopFront(pList* pplist)
{
assert(pplist);
pNode cur = *pplist;
*pplist=cur->next;
free(cur);
}
pNode Find(pList *pplist, DataType d)
{
pNode cur = *pplist;
while (cur)
{
if (cur->data == d)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void Insert(pList* pplist, pNode pos, DataType d)
{
assert(pplist);
pNode newNode = BuyNode(d);
newNode->next = pos->next;
pos->next = newNode;
}
void Erase(pList* pplist, pNode pos)
{
assert(pplist);
pNode cur = pos->next;
pos->next = cur->next;
free(cur);
cur = NULL;
}