如果要看C语言的请转到
https://blog.csdn.net/gs1069405343/article/details/50471736
table.cpp文件
#include "table.h"
table::~table(){;}
// 链表初始化
void table::ListInit(LIST_DATA *head)
{
head->data.num = 0;
head->data.str = "";
}
// 数据包初始化
void table::DataInit(DATA *data)
{
data->num = 0;
data->str = "";
}
// 判断链表是否为空
bool table::ListEmpty(LIST_DATA *head)
{
LIST_DATA *node = head->pNext;
if(NULL == node)
return true;
return false;
}
// 求链表长度
int table::ListLength(LIST_DATA *head)
{
LIST_DATA *node = head->pNext;
int i = 0;
while(NULL != node){
i++;
node = node->pNext;
}
return i;
}
// 返回head链表POS节点的数据
LIST_DATA *table::ListPosData(LIST_DATA *head, int pos)
{
LIST_DATA *node = head->pNext;
while(--pos && (NULL != node)) {
node = node->pNext;
}
return node;
}
// 在尾部删除元素
void table::ListDeleteEnd(LIST_DATA *head)
{
LIST_DATA * p = head;
LIST_DATA * ptemp = NULL;
if (p->pNext != NULL)
{
while (p->pNext != NULL)
{
ptemp = p;
p = p->pNext;
}
delete p;
p = NULL;
ptemp->pNext = NULL;
}
}
// 在头部删除元素
void table::ListDeleteHead(LIST_DATA *head)
{
LIST_DATA * p = head;
if (p != NULL && p->pNext != NULL)
{
LIST_DATA * ptemp = NULL;
p = p->pNext;
ptemp = p->pNext;
delete p;
p = NULL;
head->pNext = ptemp;
}
}
// 删除链表head中第POS个节点的数据
void table::ListDeletePos(LIST_DATA *head, int pos)
{
LIST_DATA *ptemp = ListPosData(head, pos);
if (pos == 1) {
ListDeleteHead(head);
}
else
{
LIST_DATA * p = head;
while (--pos) {
p = p->pNext;
}
p->pNext = ptemp->pNext;
delete ptemp;
ptemp = NULL;
}
}
// 头添加
void table::ListAddHead(LIST_DATA *head, DATA data)
{
LIST_DATA *newNode = new LIST_DATA;
newNode->data = data;
LIST_DATA *p = head;
if (head == NULL) {
head = newNode;
}
newNode->pNext = p->pNext;
p->pNext = newNode;
}
// 尾巴添加
void table::listAddEnd(LIST_DATA *head, DATA data)
{
LIST_DATA *newNode = new LIST_DATA;
newNode->pNext = NULL;
newNode->data = data;
LIST_DATA *p = head;
if (head == NULL) {
head = newNode;
}
else
{
while (p->pNext != NULL) {
p = p->pNext;
}
p->pNext = newNode;
}
}
// 任意改动
void table::ListChangePos(LIST_DATA *head, DATA data, int pos)
{
LIST_DATA *node = ListPosData(head,pos);
node->data = data;
}
// 销毁链表
void table::ListFree(LIST_DATA *head)
{
LIST_DATA *p = head->pNext;
LIST_DATA *ptemp = new LIST_DATA;
while (p != NULL)
{
ptemp = p;
p = p->pNext;
head->pNext = p;
ptemp->pNext = NULL;
delete ptemp;
}
head->pNext = NULL;
}
table.h 文件
#ifndef _TABLE_H_
#define _TABLE_H_
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <stdbool.h>
using namespace std;
typedef unsigned char u8;
typedef struct list{
u8 num;
string str;
}DATA;
typedef struct task {
DATA data;
task *pNext;
}LIST_DATA;
class table
{
public:
~table();
void ListInit(LIST_DATA *head); // 链表初始化
void DataInit(DATA *data); // 数据包初始化
bool ListEmpty(LIST_DATA *head); // 判断链表是否为空
int ListLength(LIST_DATA *head); // 求链表长度
LIST_DATA *ListPosData(LIST_DATA *head, int pos); // 返回head链表POS节点的数据
void ListDeleteEnd(LIST_DATA *head); // 在尾部删除元素
void ListDeleteHead(LIST_DATA *head); // 在头部删除元素
void ListDeletePos(LIST_DATA *head, int pos); // 删除链表head中第POS个节点的数据
void ListAddHead(LIST_DATA *head, DATA data); // 头添加
void listAddEnd(LIST_DATA *head, DATA data); // 尾巴添加
void ListChangePos(LIST_DATA *head, DATA data, int pos); // 任意改动
void ListFree(LIST_DATA *head); // 销毁链表
private:
};
#endif
测试用main.cpp文件
#include "table.h"
LIST_DATA *head = new LIST_DATA;
table list;
void add_head()//往头添加数据
{
int i = 0;
DATA node;
for(i = 0; i < 5; i++)
{
printf("i = %d\n", i);
node.num = i;
list.ListAddHead(head, node);
}
}
void add_end()//往尾部加数据
{
int i = 0;
DATA node;
for(i = 0; i < 5; i++)
{
printf("i = %d\n", i);
node.num = i;
list.listAddEnd(head, node);
}
}
void printf_data(LIST_DATA *head)
{
LIST_DATA *node = head;
while(node->pNext != NULL)
{
node = node->pNext;
printf("num=[%d]\n",node->data.num);
}
printf("\n");
}
int main(int argc, char const *argv[])
{
list.ListInit(head);
if(list.ListEmpty(head)){
printf("链表为空.\n");
} else {
printf("链表不为空\n");
}
printf("==========链表长度=========\n");
printf("len = %d\n",list.ListLength(head));
printf("========往头部添加数据=======\n");
add_head();
printf_data(head);
printf("========往尾部添加数据=======\n");
add_end();
printf_data(head);
printf("==========链表长度=========\n");
printf("len = %d\n",list.ListLength(head));
printf("========任意改动=======\n");
DATA node;
node.num = 10;
list.ListChangePos(head, node, 2);
printf_data(head);
printf("========删除链表head中第2个节点的数据=======\n");
list.ListDeletePos(head,2);
printf_data(head);
printf("===========获取第三个节点数据==========\n");
LIST_DATA *data = list.ListPosData(head,3);
printf("num=[%d]\n",data->data.num);
printf("============尾部删除=========13\n");
list.ListDeleteEnd(head);
printf_data(head);
printf("============头部删除=========13\n");
list.ListDeleteHead(head);
printf_data(head);
printf("============销毁链表=========13\n");
list.ListFree(head);//销毁链表
printf_data(head);
printf("==========链表长度=========\n");
printf("len = %d\n",list.ListLength(head));
}