链表
链表实现C语言
- LinkedList.h文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
//头节点
struct Header
{
int length;
struct Node *next;//指向第一个元素节点
};
//节点
struct Node
{
int data;
struct Node *next; //struct Node就是一种数据类型。
};
//为了方便使用为struct Header和struct Node重命名
typedef struct Node List;
typedef struct Header pHead;
//创建列表
pHead *createList()
{
pHead *ph = (pHead*)malloc(sizeof(pHead));//分配空间并把地址赋值给ph
ph->length = 0;
ph->next = NULL;
return ph;
}
//获取链表大小
int Size(pHead *ph)
{
if (ph == NULL)
{
printf("Size() error:");
return 0;
}
return ph->length;
}
//插入元素
int Insert(pHead *ph, int pos, int val)
{
if (ph == NULL || pos<0 || pos>ph->length)
{
printf("传参有误!");
return 0;
}
//分配一个节点的内存
List *pval = (List *)malloc(sizeof(List));
pval->data = val;
List *pCur = ph->next;//第0个元素的地址
if (pos == 0)//插入在下表为0的位置
{
ph->next = pval;
pval->next = pCur;
}
else
{
for (int i = 1; i < pos; i++)
pCur = pCur->next;//从第0位置一直后移,直到pos-1位置。
pval->next = pCur->next;//特别“pCur为插入点前的节点,pCur->next为插入点之后的节点,pval为新节点”
pCur->next = pval;
}
ph->length++;
return 1;
}
//查找元素
List *find(pHead *ph, int val)
{
if (ph == NULL) {
printf("传参有误!");
return NULL; //返回地址为NULL
}
List *pTmp = ph->next;
do
{
if (pTmp->data == val)
{
return pTmp;
}
pTmp = pTmp->next;
} while (pTmp->next != NULL);//有缺陷,无法找最后一个元素
if (pTmp->data == val)
{
return pTmp;
}
printf("没有值为%d的元素!", val);
return NULL;
}
//删除元素
List *Delete(pHead *ph, int val)
{
if (ph == NULL)
{
printf("链表传入有错");
return NULL;
}
List *pval = find(ph, val);
if (pval == NULL)
{
printf("没有值为%d的元素",val);
return NULL;
}
List *pRe = ph->next;//首节点
List *pCur = NULL;
if (pRe->data == val) {
ph->next = pRe->next;
ph->length--;
return pRe;
}
else
{
for (int i = 0; i < ph->length; i++) {
pCur = pRe->next;
if (pCur->data == val)
{
pRe->next = pCur->next;
ph->length--;
return pCur;
}
pRe = pRe->next;
}
}
}
//销毁链表
void Destory(pHead *ph)
{
List *pCur = ph->next;
List *pTmp;
if (ph == NULL)
printf("传入参数有误!");
while (pCur->next != NULL)
{
pTmp = pCur->next;
free(pCur);
pCur = pTmp;
}
ph->length = 0;
ph->next = NULL;
}
- LinkedListmain.cpp文件
#include"LinkedList.h"
int main()
{
int m, n;
pHead *ph;
List *fin;
ph=createList();
Insert(ph, 0, 0);
Insert(ph, 1, 1);
Insert(ph, 2, 2);
Insert(ph, 3, 3);
m = Size(ph);
fin = find(ph, 2);
printf("size is %d\nit can find %d\n", m,fin->data);
fin = Delete(ph, 1);
m = Size(ph);
printf("size is %d\nit can find %d\n", m, fin->data);
Destory(ph);
system("pause");
}
- 双链表
dlist.h文件
#pragma once
#include<stdio.h>
#include<stdlib.h>
typedef struct Head *pHead;
typedef struct Node *pNode;
struct Head {
int length;
pNode next;
};
struct Node {
int data;
pNode pre;
pNode next;
};
//创建双链表
pHead DlistCreat()
{
pHead ph = (pHead)malloc(sizeof(struct Head));
if (ph == NULL)
printf("\n分配头节点失败!");
ph->length = 0;
ph->next = NULL;
return ph;
}
//取双链表长度
int getLength(pHead ph)
{
if (ph == NULL)
printf("\n传入双链表有误!");
return ph->length;
}
//判断链表是否为空
int IsEmpty(pHead ph)
{
if (ph == NULL)
printf("\n传入参数有误!");
if (ph->length == 0)
return 1;
else
return 0;
}
//在pos位置插入元素val
int DlistInsert(pHead ph, int pos, int val)
{
pNode pval = NULL;
if (ph == NULL || pos<0 || pos>ph->length)
printf("\n插入元素时,参数传递有误!");
pval = (pNode)malloc(sizeof(struct Node));
pval->data = val;
if (IsEmpty(ph))
{
ph->next = pval;
pval->next = NULL;
pval->pre = NULL;
}
else {
pNode pCur = ph->next;//pCur为第一个元素
if (pos == 0)
{
ph->next = pval;
pval->pre = NULL;
pval->next = pCur;
pCur->pre = pval;
}
else
{
for (int i = 1; i < pos; i++)
{
pCur = pCur->next;
}
if (pos == ph->length) {//在链表尾部插入
pval->pre = pCur;
pval->next = NULL;
pCur->next = pval;
}
else {//在链表中间插入时
pval->next = pCur->next;
pCur->next->pre = pval;
pval->pre = pCur;
pCur->next = pval;
}
}
}
ph->length++;
return 1;
}
//查找元素
pNode DlistFind(pHead ph, int val)
{
if (ph == NULL)
printf("\n参数传入有误!");
pNode pTmp = ph->next;
do {
if (pTmp->data == val)
{
printf("\n有此%d元素", val);
return pTmp;
}
pTmp = pTmp->next;
} while (pTmp->next != NULL);
if (pTmp->data == val)
{
printf("\n有此%d元素", val);
return pTmp;
}
printf("\n无此元素");
return NULL;
}
//删除ph中val元素
pNode DlistDelete(pHead ph, int val)
{
if (ph == NULL || ph->length == 0)
printf("\n参数输入有误");
pNode pval = DlistFind(ph, val);
if (pval == NULL)
return NULL;
printf("\n将其删除");
pNode pRe = pval->pre;//第一个元素的pre为NULL
pNode pNext = pval->next;//最后一个元素的next为NULL
if (pRe == NULL) {//如果为第一个元素
ph->next = pNext;
pNext->pre = pRe;
}
else if (pNext == NULL) {
pRe->next = NULL;
}
else {
pRe->next = pNext;
pNext->pre = pRe;
}
ph->length--;
}
//销毁链表
void DlistDestory(pHead ph)
{
pNode pCur = ph->next;
pNode pTmp;
if (ph == NULL||pCur==NULL)
printf("/n传参有误!");
while (pCur->next != NULL)
{
pTmp = pCur->next;
free(pCur);
pCur = pTmp;
}
free(pCur);//释放最后一个节点
ph->length = 0;
ph->next = NULL;
}
//打印链表元素,从前往后
void printFront(pHead ph)
{
if (ph == NULL)
printf("参数传递有误!");
pNode pTmp = ph->next;
while (pTmp != NULL)
{
printf("%d\t", pTmp->data);
pTmp = pTmp->next;
}
printf("\n");
}
dlistmain.cpp文件
#include"dlist.h"
int main() {
pHead ph=DlistCreat();
int m, n;
DlistInsert(ph, 0, 0);
DlistInsert(ph, 1, 1);
DlistInsert(ph, 2, 2);
DlistInsert(ph, 1, 3);
//DlistDelete(ph, 0);
//DlistDelete(ph, 2);
//DlistDelete(ph, 3);
printFront(ph);
DlistDestory(ph);
m = getLength(ph);
printf("\nThe length is %d", m);
system("pause");
return 0;
}