头文件
#includetypedef struct _Node { int data; struct _Node* next; }Node; //add create list Node* CreateList(); //insert list Node, void InsertList(Node* Head,int Data); //traverList List Node void traverList(Node* Head); //Get List Length int LengthList(Node* Head); //search List Node* SearchList(Node* Head, int Find); //delete List Node void DeleteList(Node* Head, Node* FindNode); void DestoryList(Node* head);
#include "list.h" #include#include #include //add create list ,head List; Node* CreateList() { Node* head = (Node*)malloc(sizeof(Node)); head->next = NULL; return head; } //insert list Node, void InsertList(Node* Head,int Data) { Node* CurrentNode = (Node*)malloc(sizeof(Node)); //current InsertList function CurrentNode->data = Data CurrentNode->data = Data; CurrentNode->next = Head->next; Head->next = CurrentNode; } //traverList List Node void traverList(Node* Head) { Head = Head->next; while(Head) { printf("%2d ", Head->data); Head = Head->next; } } //Get List Length int LengthList(Node* Head) { int Value = 0; Head = Head->next; while(Head) { // printf("%2d ", Head->data); Value++; Head = Head->next; } return Value; } //search List Node* SearchList(Node* Head, int Find) { Head = Head->next; while(Head) { if (Head->data == Find) break; Head = Head->next; } return Head; // while(Head) { // if (Head->data == Find) // return Find; // // Head = Head->next; // } // return NULL; } //delete List Node void DeleteList(Node* Head,Node* FindNode) { if (Head->next == NULL) { while (Head->next != FindNode) { Head = Head->next; } Head->next = FindNode->next; free(FindNode->next); } else { FindNode->data = FindNode->next->data; Node* pTest = FindNode->next; FindNode->next = pTest->next; free(pTest); } } void PopSortList(Node* Head) { Node*p,*q; int len_ = LengthList(Head); for (int i = 0; i < len_;i++) { p = Head->next; q = p->next; for (int j = 1; j < len_ - 1; j++) { if (p->data > q->data) { p->data ^= q->data; q->data ^= p->data; p->data ^= q->data; } p = p->next; q = p->next; } } } void DestoryList(Node* head) { Node* pTest; while (head) { pTest = head->next; free(head); head = pTest; } }
cmake_minimum_required(VERSION 3.12)
PROJECT(List)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} Test)
SET(
utest
${CMAKE_CURRENT_SOURCE_DIR}/list.cc
${CMAKE_CURRENT_SOURCE_DIR}/utest_list.cc
)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/gtest/googletest/include)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
# ADD_EXECUTABLE(aaa ${Test})
ADD_EXECUTABLE(UtList ${utest})
target_link_libraries(
UtList
-pthread
${CMAKE_CURRENT_SOURCE_DIR}/gtest/lib/libgtest.a
${CMAKE_CURRENT_SOURCE_DIR}/gtest/lib/libgtest_main.a
)
#include "gtest/gtest.h"
#include "list.h"
TEST(Case, Case_1) {
Node* head_ = CreateList();
traverList(head_);
srand(time(NULL));
for(int i = 0; i < 15; i++) {
InsertList(head_,rand()%50);
}
traverList(head_);
int len_ = LengthList(head_);
printf("List Length : %dn",len_);
PopSortList(head_);
traverList(head_);
}



