部分链表+gtest单元测试+CMakeLists.txt

 头文件

#include <stdio.h>

typedef 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 <stdlib.h>
#include <stdio.h>
#include <time.h>

//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 : %d\n",len_);
    PopSortList(head_);
    traverList(head_);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值