您的位置:首页 > 理论基础 > 数据结构算法

【数据结构】C语言实现无头节点单链表

2018-01-13 22:43 429 查看
LinkList.h

#pragma once

#include<stdio.h>
#include<stddef.h>

typedef char LinkType;

typedef struct LinkNode{
LinkType data;
struct LinkNode* next;
}LinkNode;

//初始化
void LinkListInit(LinkNode** head);
//创建一个新结点
LinkNode* CreatNewNode(LinkType value);
//尾插
LinkNode* LinkListPushBack(LinkNode** head, LinkType value);
//尾删
void LinkListPopBack(LinkNode** head);
//头插
void LinkListPushFront(LinkNode** head, LinkType value);
//头删
void LinkListPopFront(LinkNode** head);
//查找元素在链表中的位置
LinkNode* LinkListFind(LinkNode* head, LinkType to_find);
//在pos之前插入元素
void LinkListInsert(LinkNode** head, LinkNode* pos, LinkType value);
//在pos之后插入元素
void LinkListInsertAfter(LinkNode** head, LinkNode* pos, LinkType value);
//删除指定位置的元素
void LinkListErase(LinkNode** head, LinkNode* pos);
//删除指定值的元素
void LinkListRemove(LinkNode** head, LinkType to_delete);
//删除指定值的所有元素
void LinkListRemoveAll(LinkNode** head, LinkType to_delete);
//判断链表为空
int LinkListEmpty(LinkNode* head);
//求链表元素个数
size_t LinkListSize(LinkNode* head);
//逆序打印单链表
void LinkListReversePrint(LinkNode** head);
//不允许遍历链表,在pos位置之前插入一个元素
void LinkListInsertBefore(LinkNode* head, LinkNode* pos, LinkType value);

LinkList.c
#define _CRT_SECURE_NO_WARNINGS 1

#include"LinkList.h"
#include<stdlib.h>

LinkNode* CreatNewNode(LinkType value) {

LinkNode* NewNode = (LinkNode*)malloc(sizeof(LinkNode));
NewNode->data = value;
NewNode->next = NULL;
return NewNode;

}

void DestoryNode(LinkNode** Delete) {
if (Delete == NULL) {
return;
}
free(*Delete);
}

void LinkListInit(LinkNode** head) {
if (head == NULL) {
//非法输入
return;
}
if ((*head) == NULL) {
//空链表
return;
}
(*head) = NULL;
}
LinkNode* LinkListPushBack(LinkNode** head, LinkType value) {
if (head == NULL) {
//非法输入
return;
}
if ((*head) == NULL) {
//空链表
(*head) = CreatNewNode(value);
}
//链表不为空链表,直接遍历一遍
else {
LinkNode* begin = *head;
while (begin->next) {
begin = begin->next;
}
begin->next = CreatNewNode(value);
}
}
void LinkListPopBack(LinkNode** head) {
if (head == NULL) {
return;
}
if (*head == NULL) {
//空链表
return;
}
LinkNode* pop = *head;
while (pop->next->next){
pop = pop->next;
}
LinkNode* Delete = pop->next;
DestoryNode(&Delete);
pop->next = NULL;
}

void LinkListPushFront(LinkNode** head, LinkType value) {
if (head == NULL) {
return;
}
if (*head == NULL) {
//空链表
LinkNode* NewNode = CreatNewNode(value);
*(head) = NewNode;
}
else{
LinkNode* NewNode = CreatNewNode(value);
LinkNode* NextNode = (*head);
(*head) = NewNode;
NewNode->next = NextNode;
}
}

void LinkListPopFront(LinkNode** head) {
if (head == NULL) {
return;
}
if (*head == NULL) {
return;
}
LinkNode* Delete = (*head);
(*head) = (*head)->next;
DestoryNode(&Delete);
}

LinkNode* LinkListFind(LinkNode* head, LinkType to_find) {
if (head == NULL) {
//空链表
return;
}
LinkNode* cur = head;
while (cur) {
if (cur->data == to_find) {
return cur;
}
cur = cur->next;
}
return 0;
}

void LinkListInsert(LinkNode** head, LinkNode* pos, LinkType value){
if (head == NULL) {
return;
}
if (*head == NULL) {
//空链表
return;
}
if (pos == NULL) {
return;
}
LinkNode* cur = (*head);
while (cur) {
if (pos == (*head)) {
LinkNode* NewNode = CreatNewNode(value);
LinkNode* AfterInsert = (*head);
(*head) = NewNode;
NewNode->next = AfterInsert;
}
if (cur->next == pos) {
LinkNode* NewNode = CreatNewNode(value);
LinkNode* AfterInsert = cur->next;
cur->next = NewNode;
NewNode->next = AfterInsert;
break;
}
cur = cur->next;
}
}

void LinkListInsertAfter(LinkNode** head, LinkNode* pos, LinkType value) {
if (head == NULL) {
//非法输入
return;
}
if (*head == NULL) {
//空链表
return;
}
if (pos == NULL) {
return;
}
LinkNode* cur = *head;
while (cur) {
if (pos == cur) {
LinkNode* NewNode = CreatNewNode(value);
LinkNode* After = cur->next;
cur->next = NewNode;
NewNode->next = After;
break;
}
cur = cur->next;
}
}

void LinkListErase(LinkNode** head, LinkNode* pos){
if (head == NULL) {
return;
}
if (*head == NULL) {
//空链表
return;
}
if (pos == NULL) {
return;
}
LinkNode* cur = (*head);
while (cur) {
if (pos == (*head)) {
(*head) = (*head)->next;
DestoryNode(&cur);
break;
}
if (cur->next == pos) {
LinkNode* Delete = cur->next;
cur->next = cur->next->next;
DestoryNode(&Delete);
break;
}
cur = cur->next;
}
}

void LinkListRemove(LinkNode** head, LinkType to_delete) {
if (head == NULL) {
return;
}
if (*head == NULL) {
return;
}
LinkNode* begin = *head;
LinkNode* prev = NULL;
while (begin) {
if ((*head)->data == to_delete) {
LinkNode* Delete = begin;
(*head) = (*head)->next;
DestoryNode(&Delete);
break;
}
if (begin->data == to_delete) {
LinkNode* Delete = begin;
prev->next = begin->next;
DestoryNode(&Delete);
break;
}
prev = begin;
begin = begin->next;
}
}

size_t LinkListSize(LinkNode* head) {
if (head == NULL) {
return 0;
}
size_t count = 0;
LinkNode* cur = head;
while (cur) {
count++;
cur = cur->next;
}
return count;
}

void LinkListRemoveAll(LinkNode** head, LinkType to_delete) {
if (head == NULL) {
return;
}
if (*head == NULL) {
return;
}
size_t size = LinkListSize(*head);
while (size) {
LinkListRemove(&(*head), to_delete);
size--;
}
}

int LinkListEmpty(LinkNode* head) {
if (head == NULL) {
//空链表
return 1;
}
else {
return 0;
}
}

void LinkListReversePrint(LinkNode** head) {
if (head == NULL) {
//空链表
return;
}
LinkNode* NewHead = NULL;
LinkNode* cur = (*head);
while (cur) {

//拆结点
LinkNode* Node = cur;
cur = cur->next;

//装结点
Node->next = NewHead;
NewHead = Node;
}
LinkNode* cur2 = NewHead;
while (cur2) {
printf("[%c]->", cur2->data);
cur2 = cur2->next;
}
printf("NULL\n");
}

void LinkListInsertBefore(LinkNode* head, LinkNode* pos, LinkType value) {
if (head == NULL) {
//空链表
return;
}
if (pos == NULL) {
//非法
return;
}
LinkNode* NewNode = CreatNewNode(pos->data);
LinkNode* cur = pos->next;
pos->next = NewNode;
NewNode->next = cur;
pos->data = value;
}


test.c
#define _CRT_SECURE_NO_WARNINGS 1

#include"LinkList.h"
#include<stdlib.h>
#include<stddef.h>

#define FUNCTION printf("-----------------------%s-----------------------\n",__FUNCTION__);

void PrintChar(LinkNode* head, char* msg) {
if (head == NULL) {
return;
}
printf("%s\n\n", msg);
while (head) {
printf("[%c] ", head->data);
head = head->next;
}
printf("\n\n");
}

void TestPushBack() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
}
void TestPopBack() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
LinkListPopBack(&pnode);
LinkListPopBack(&pnode);
PrintChar(pnode, "尾删两个元素");
}
void TestPushFront() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
LinkListPushFront(&pnode, 'a');
LinkListPushFront(&pnode, 'b');
LinkListPushFront(&pnode, 'c');
LinkListPushFront(&pnode, 'd');
PrintChar(pnode, "头插四个元素");
}

void TestPopFront() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkListPushFront(&pnode, 'a');
LinkListPushFront(&pnode, 'b');
LinkListPushFront(&pnode, 'c');
LinkListPushFront(&pnode, 'd');
PrintChar(pnode, "头插四个元素");
LinkListPopFront(&pnode);
LinkListPopFront(&pnode);
PrintChar(pnode, "头删两个元素");
}

void TestFind() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkListPushFront(&pnode, 'a');
LinkListPushFront(&pnode, 'b');
LinkListPushFront(&pnode, 'c');
Li
4000
nkListPushFront(&pnode, 'd');
PrintChar(pnode, "头插四个元素");

if (LinkListFind(pnode, 'b')) {
printf("find sucess!!!\n");
}
if (LinkListFind(pnode, 'f') == 0) {
printf("lose!!!\n");
}
}

void TestInsert() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkNode* pos_a = LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkNode* pos_d = LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
LinkListInsert(&pnode, pos_d, 'e');
PrintChar(pnode, "在d之前插入一个元素e:");
LinkListInsert(&pnode, pos_a, 'f');
PrintChar(pnode, "在a之前插入一个元素f:");

}

void TestAfterInsert() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkNode* pos_a = LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkNode* pos_d = LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
LinkListInsertAfter(&pnode, pos_a, 'f');
PrintChar(pnode, "在a之后插入一个元素f:");
LinkListInsertAfter(&pnode, pos_d, 'e');
PrintChar(pnode, "在d之后插入一个元素e:");
}

void TestErase() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkNode* pos_a = LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkNode* pos_d = LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
LinkListErase(&pnode, pos_a);
PrintChar(pnode, "删除元素'a'");
LinkListErase(&pnode, pos_d);
PrintChar(pnode, "删除元素'd'");
}

void TestRemove() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
LinkListRemove(&pnode, 'a');
PrintChar(pnode, "删除元素a");
LinkListRemove(&pnode, 'd');
PrintChar(pnode, "删除元素d");
}

void TestRemoveAll() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkListPushBack(&pnode, 'd');
LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
LinkListRemoveAll(&pnode, 'a');
PrintChar(pnode, "删除元素'a'");
LinkListRemoveAll(&pnode, 'd');
PrintChar(pnode, "删除元素'd'");
}

void TestEmpty() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
int ret;
ret = LinkListEmpty(pnode);
printf("expect 1,actual %d: \n", ret);
LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
ret = LinkListEmpty(pnode);
printf("expect 0, actual %d\n: ", ret);
}

void TestListSize() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
size_t size = LinkListSize(pnode);
printf("except 4,actual : %d\n", size);
}

void TestReversePrint() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkListPushBack(&pnode, 'a');
LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
LinkListReversePrint(&pnode);
}

void TestInsertBefore() {
LinkNode* pnode;
FUNCTION;
LinkListInit(&pnode);
LinkNode* pos_a = LinkListPushBack(&pnode, 'a');
LinkNode* pos_b = LinkListPushBack(&pnode, 'b');
LinkListPushBack(&pnode, 'c');
LinkNode* pos_d = LinkListPushBack(&pnode, 'd');
PrintChar(pnode, "尾插四个元素");
LinkListInsertBefore(pnode, pos_b, 'e');
PrintChar(pnode, "在b之前插入e");
LinkListInsertBefore(pnode, pos_d, 'f');
PrintChar(pnode, "在d之前插入f");
LinkListInsertBefore(pnode, pos_a, 'g');
PrintChar(pnode, "在a之前插入g");
}

int main() {
TestPushBack();
TestPopBack();
TestPushFront();
TestPopFront();
TestFind();
TestInsert();
TestAfterInsert();
TestErase();
TestRemove();
TestRemoveAll();
TestEmpty();
TestListSize();
TestReversePrint();
TestInsertBefore();
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: