说明:
这是不带头结点的功能实现程序
不带头结点的程序,在进行插入、删除、查值等函数功能时需要对表首进行特殊处理
故,不带头结点函数较为麻烦,考虑情况过多。
如果看不懂不带头结点的代码的话,强烈建议先学会带头结点的代码
注释已更新完毕
代码如下:
/*
带头结点单链表 C语言指针实现
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#define ElemType int
#define MaxSize 10
typedef struct LNode {
ElemType data;
struct LNode *next;
} LNode, *LinkList;
LNode * InitList( LinkList L );//定义初始化单链表 函数
bool LinkListEmpty( LinkList L );//定义判断单链表是否为空 函数
bool isEquality( ElemType e1, ElemType e2 );//定义 判断两元素值是否相等 函数
LinkList HeadInsert( LinkList L );//定义 头插法新建单链表 函数
LinkList TailInsert( LinkList L );//定义 尾插法新建单链表 函数
LNode *GetElem( LinkList L, int i );//定义 按序号查找节点 函数
LNode *LocateElem( LinkList L, ElemType e );//定义 按值查找节点 函数
bool InsertPriorNode( LinkList *L, int i, ElemType e );//定义 在第i个位置上插入值为e的新节点(二级指针带回表头,并且能返回bool)
bool InsertPriorNodePro( LNode *p, ElemType e );//实现 在节点p 前 插入新节点(偷天换日)
bool DeleteNode( LinkList *L, int i , ElemType *e );//定义 删除第i个结点 并且返回该结点值 函数
bool DeleteLNodePro( LNode *p );//定义 偷天换日法删除第i个结点 函数
void PrintLinkList( LinkList L );//定义 打印单链表各元素的值 函数
int ReturnListLength( LinkList L );//定义 返回链表长度 函数
LinkList Test( LinkList L, int a, int x );//定义 老师出的那个题的函数
/* 题目:
在第一个值为a的节点后面插入一个 值为x的节点
若没有找到值为a的节点或者L为空表则在表尾插入一个 值为x的节点。
*/
bool DeleteNode( LinkList *L, int i , ElemType *e );
int main() {
srand( ( unsigned )time( NULL ) );
LNode *L;
L = InitList( L );
printf( "%d\n", LinkListEmpty( L ) );
//测试头插法
// L = HeadInsert( L );
// PrintLinkList( L );
//测试尾插法
L = TailInsert( L );
PrintLinkList( L );
// 测试GetElem函数
// printf( "GetElem( L, 7 )->data = %d\n", GetElem( L, 7 )->data );
//测试LocateElem函数
// printf( "LocateElem( L, 710 )->data = %d", LocateElem( L, 710 )->data );
//测试InsertPriorNode函数
// printf( "InsertPriorNode( &L, 3, 710 ) = %d\n", InsertPriorNode( &L, 3, 710 ) );
// PrintLinkList( L );
//测试InsertPriorNodePro函数
// printf( "InsertPriorNodePro( GetElem( L, 10 ), 710 ) ) = %d\n", InsertPriorNodePro( GetElem( L, 10 ), 710 ) );
// PrintLinkList( L );
//测试DeleteNode函数
// ElemType e = 0;
// print