【数据结构】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;
}
#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;
}
相关文章推荐
- C语言实现无头节点的单链表
- C语言实现无头节点链表
- 【数据结构】C语言实现链表节点的插入和删除
- 数据结构之C++实现无头节点链表(List)(无主函数)
- 【数据结构】C语言实现链表节点的插入和删除
- C语言实现无头节点单链表
- 重温数据结构之链表二——C语言实现
- 算法与数据结构-单向链表的基本操作C语言实现
- C语言实现数据结构的链表
- 数据结构(严蔚敏)之二——链表的c语言实现
- 【数据结构】C语言实现链表
- 【小镇的技术天梯】从头写数据结构,C语言实现双向链表
- 数据结构_队列_用链表动态建立释放节点实现队列各种操作_C++实现
- C语言数据结构中链表的具体实现
- C语言实现链表节点的插入
- [数据结构]C语言链表实现
- 单向链表之删除节点(C语言实现)
- C语言实现链表节点的插入和删除
- 数据结构模版----单链表SimpleLinkList[不带头结点&&伪OO](C语言实现)
- 链表问题之无头单链表删节点<Java实现>