list.h
#pragma once
//头文件 ".h",存放结构或者函数的声明
//带头结点单链表,逻辑相邻,物理不一定相邻,为了找到下一个节点,就必须增加下一个节点的地址
//尾节点:最后一个节点,在单链表中,尾节点的next为NULL,NULL是单链表的结尾标志
//头结点:其数据域不使用或者存放链表长度.其作用,相对于一个标杆,简化操作
typedef int ElemType;
typedef struct Node
{
ElemType data;//数据
struct Node* next;//后继指针
}Node,*List;//List == Node *
//typedef Node* List;//List == Node *//List本质是Node*,但含义不同,List表示一条链表,Node*表示一个节点的地址
//初始化plist
void InitList(List plist);
//往plist中头部插入数字val
bool Insert_head(List plist, ElemType val);
//往plist中的尾部插入数字val
bool Insert_tail(List plist , ElemType val);
//在plist中查找val值,找到返回该节点地址,失败返回NULL
Node* Search(List plist, ElemType val);
//删除plist中的第一个val
bool DeleteVal(List plist, ElemType val);
//判断plist是否为空链表(没有数据节点)
bool IsEmpty(List plist);
//获取plist长度,数据节点的个数
int GetLength(List plist);
//获取plist链表的pos位置的值
int GetElem(List plist, int pos);
//获取val的前驱
Node* Prior(List plist, ElemType val);
//获取val的后继
Node* Next(List plist, ElemType val);
//输出plist的所有数据
void Show(List plist);
//清空数据
void Clear(List plist);
//销毁
void Destroy(List plist);
list.cpp
//".cpp,.c",源文件,存放函数的实现
//每一个.h文件都有一个同名的源文件
#include <stdio.h>//库函数,编译工具自带的函数
#include <stdlib.h>
#include <assert.h>
#include "list.h"//引用自己的头文件
//初始化plist
void InitList(List plist)
{
assert(plist != NULL);
//头结点的数据不使用 plist->data可以不处理
plist->next = NULL;
}
//往plist中头部插入数字val
bool Insert_head(List plist, ElemType val)//O(1)
{
//1.动态创建一个新节点
Node* p = (Node*)malloc(sizeof(Node));
assert(p != NULL);
if (p == NULL)
return false;
//2.把数据val存放到新节点
p->data = val;
//3.把新节点插入在头结点的后面
p->next = plist->next;
plist->next = p;
return true;
}
//往plist中的尾部插入数字val
bool Insert_tail(List plist, ElemType val)//O(n)
{
//1.动态创建新节点
Node* p = (Node*)malloc(sizeof(Node));
assert(p != NULL);
if (p == NULL)
return false;
// 2.把值存放到新节点
p->data = val;//p->next = NULL;
// 3.找到链表尾巴
Node* q;
for (q = plist; q->next != NULL; q = q->next)
;
// 4.把新节点插在尾节点的后面,把p插入在q的后面
p->next = q->next;//p->next = NULL;
q->next = p;
return true;
}
//在plist中查找val值,找到返回该节点地址,失败返回NULL
Node* Search(List plist, ElemType val)
{
for (Node* p = plist->next; p != NULL; p = p->next)
{
if (p->data == val)//找到了
return p;
}
return NULL;//没有找到
}
//删除plist中的第一个val
bool DeleteVal(List plist, ElemType val)
{
//1.找val的前驱
Node* p = Prior(plist,val);//指向前驱节点
if (p == NULL)//没有val
return false;
//free(p->next);//错误
//p->next = p->next->next;
Node* q = p->next;//标记需要删除的节点
p->next = q->next;//p->next = p->next->next;将q从链表中剔除
free(q);//释放节点
return true;
}
//判断plist是否为空链表(没有数据节点)
bool IsEmpty(List plist);
{
return plist->next == NULL;//等同下面的if else
/*if (plist->next == NULL)
return true;
else
return false;*/
}
//获取plist长度,数据节点的个数
int GetLength(List plist);
{
int count = 0;//计数器
for (Node* p = plist->next; p != NULL; p = p->next)
count++;
return count;
}
//获取plist链表的pos(下标)位置的值
bool GetElem(List plist, int pos, int* rtval)//rtval:输出参数
{
if (pos < 0 || pos >= GetLength(plist))//出错
return false;
Node* p=plist->next;
for (int i=0; i < pos; p = p->next, i++)
{
;
}
*rtval = p->data;
return true;
}
//int GetElem(List plist, int pos);
//{
// if (pos < 0 || pos >= GetLength(plist))//下标不存在
// return -1;//不可以,可能和正常的值冲突
//}
//获取val的前驱
Node* Prior(List plist, ElemType val)
{
for (Node* p = plist; p != NULL; p = p->next)
{
if (p->next->data == val)//找到了
return p;
}
return NULL;//失败了
}
//获取val的后继
Node* Next(List plist, ElemType val);
{
for (Node* p = plist->next; p != NULL; p = p->next)
{
if (p->data == val)
return p->next;
}
return NULL;
}
//输出plist的所有数据
void Show(List plist)
{
for (Node* p = plist->next; p != NULL; p = p->next)//遍历所有的数据节点
{
printf("%d ",p->data);
}
printf("\n");
}
//清空数据
void Clear(List plist);
{
Destroy(plist);
}
//销毁
void Destroy(List plist);
{
Node* p;//指向第一个数据节点
while (plist->next != NULL)//还有数据结构
{
p = plist->next;
plist->next = p->next;//剔除p
free(p);
}
//销毁,内存回收
void Destroy1(List plist)
{
if (plist == NULL || plist->next == NULL)
return;
Node* p = plist->next;
Node* q;
plist->next = NULL;
while (p != NULL)
{
q = p->next;
free(p);
p = q;
}
}