// LinkTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct
{
int data;
}UsrCommData;
typedef struct NODE
{
UsrCommData usr_info;
struct NODE *next;
}CommNode;
CommNode *headNode = NULL;
/*******************************************************************************************************************/
//创建节点
CommNode *CreatNode(UsrCommData *p_info)
{
CommNode *node = (CommNode *)malloc(sizeof(CommNode));
if (node == NULL)
return NULL;
memcpy(&node->usr_info, p_info, sizeof(UsrCommData));
node->next = NULL;
return node;
}
//增加节点到结尾
int AddNodeToTail(CommNode **p_head, UsrCommData *p_info)
{
CommNode *temp_node = NULL;
CommNode *node = CreatNode(p_info);
if (node == NULL)
{
printf("%s: Create node fail!", __FUNCTION__);
return -1;
}
if (*p_head == NULL)//链表为空
{
*p_head = node;
}
else
{
temp_node = *p_head;
while (temp_node->next != NULL)
{
temp_node = temp_node->next;
}
temp_node->next = node;
}
return 0;
}
//从头增加节点
int AddNodeToHead(CommNode **p_head, UsrCommData *p_info)
{
CommNode *node = CreatNode(p_info);
if (node == NULL)
{
printf("%s: Create node fail!", __FUNCTION__);
return -1;
}
if (*p_head == NULL)//链表为空
{
*p_head = node;
}
else//链表已存在节点
{
node->next = *p_head;
}
*p_head = node;
return 0;
}
//获取大小
int GetListSize(CommNode **p_head)
{
CommNode *node = *p_head;
int size = 0;
while (node != 0)
{
node = node->next;
++size;
}
return size;
}
//查找节点
CommNode *FindNode(CommNode **p_head, int index)
{
CommNode *node = *p_head;
int curIndex = 0;
if (node == NULL)
{
printf("Link is null!");
return NULL;
}
while (node != NULL)
{
if (index == curIndex)
{
return node;
}
node = node->next;
++curIndex;
}
return NULL;
}
//读取节点
UsrCommData *ReadNode(CommNode **p_head, int index)
{
CommNode *node = FindNode(p_head, index);
if (node == NULL)
{
printf("No this node!");
}
else
{
return &node->usr_info;
}
return NULL;
}
//读取链表
void ReadList(CommNode **p_head)
{
UsrCommData *temp_info;
CommNode *node = *p_head;
if (node == NULL)
{
printf("Link is null!");
}
else
{
for (int index = 0; index < GetListSize(p_head); index++)
{
temp_info = ReadNode(p_head, index);
printf("%s: CommNode %d's data = %d\n", __FUNCTION__, index, temp_info->data);
//printf("%s: CommNode %d's data = %d\n", __FUNCTION__, index, node->usr_info.data);
//node = node->next;
}
printf("\n");
}
}
//更新节点
int UpdateNode(CommNode **p_head, int index, UsrCommData *p_info)
{
CommNode *node = FindNode(p_head, index);
if (node == NULL)
{
printf("Link is null!");
return -1;
}
memcpy(&node->usr_info, p_info, sizeof(UsrCommData));
return 0;
}
//删除链表
void DeleteList(CommNode **p_head)
{
CommNode *node = *p_head;
if (node == NULL)
{
printf("Link is null!");
return;
}
while (node != NULL)
{
CommNode *temp = node;
node = node->next;
free(temp);
}
*p_head = NULL;
printf("%s: The LINKED LIST has cleaned.\n", __FUNCTION__);
}
//删除节点
int DeleteNode(CommNode **p_head, int index)
{
CommNode *foundNode = FindNode(p_head, index);
if (foundNode == NULL)
{
return -1;
}
else
{
CommNode *node = *p_head;
if (GetListSize(p_head) == 1)// 只有一个节点
{
free(node);
*p_head = NULL;
}
else// 大于一个节点
{
if (index == 0)// 删除头节点
{
CommNode *oldHeadNode = node;// 暂存旧节点
node = node->next;// 将第二个节点设置为新头节点
*p_head = node;
free(oldHeadNode);// 释放头旧节点的内存
oldHeadNode = NULL;
}
else if (index == (GetListSize(p_head)-1))// 删除尾节点
{
while (node->next != foundNode)
{
node = node->next;
}
node->next = NULL;// 将倒数第二个节点的next置为NULL后就相当于设置为尾节点
free(foundNode);//释放被删节点的内存
foundNode = NULL;
}
else// 删除中间个某节点
{
while (node->next != foundNode)// 找到要删除的节点
{
node = node->next;
}
node->next = foundNode->next;// 将 被删节点 的 上一个节点的next 设置为 被删节点 的 下一个节点
free(foundNode);//释放被删节点的内存
foundNode = NULL;
}
}
}
printf("CommNode %d has deleted!\n\n", index);
return 0;
}
//插入节点
void InsertNode(CommNode **p_head, int index, UsrCommData *p_info)
{
CommNode *newNode = CreatNode(p_info);
CommNode *insertNode = FindNode(p_head, index);
if (*p_head == NULL)
{
printf("Link is null!");
return;
}
else if (insertNode == NULL)
{
printf("No this node!");
return;
}
else
{
newNode->next = insertNode->next;
insertNode->next = newNode;
}
}
void NodeTest(void)
{
UsrCommData temp_info;
UsrCommData *r_info;
int r_index;
int del_index;
int del_res;
//节点为空时删除头节点,看结果。
del_index = 0;
del_res = DeleteNode(&headNode, del_index);
if (del_res == -1)
{
printf("%s: CommNode %d delete fail!\r\n", __FUNCTION__, del_index);
}
else
{
printf("%s: CommNode %d delete success!\r\n", __FUNCTION__, del_index);
}
//增加一个节点
temp_info.data = 55;
AddNodeToHead(&headNode, &temp_info);
//删除头节点,看结果。
del_index = 0;
del_res = DeleteNode(&headNode, del_index);
if (del_res == -1)
{
printf("%s: CommNode %d delete fail!\r\n", __FUNCTION__, del_index);
}
else
{
printf("%s: CommNode %d delete success!\r\n", __FUNCTION__, del_index);
}
//增加两个节点
temp_info.data = 55;
AddNodeToHead(&headNode, &temp_info);
temp_info.data = 11;
AddNodeToTail(&headNode, &temp_info);
//删除头节点,看结果
del_index = 0;
del_res = DeleteNode(&headNode, del_index);
if (del_res == -1)
{
printf("%s: CommNode %d delete fail!\r\n", __FUNCTION__, del_index);
}
else
{
printf("%s: CommNode %d delete success!\r\n", __FUNCTION__, del_index);
}
//增加一个节点到开头
temp_info.data = 55;
AddNodeToHead(&headNode, &temp_info);
//删除尾节点,看结果。
del_index = GetListSize(&headNode)-1;
del_res = DeleteNode(&headNode, del_index);
if (del_res == -1)
{
printf("%s: CommNode %d delete fail!\r\n", __FUNCTION__, del_index);
}
else
{
printf("%s: CommNode %d delete success!\r\n", __FUNCTION__, del_index);
}
//增加一个节点到结尾
temp_info.data = 11;
AddNodeToTail(&headNode, &temp_info);
//再增加三个节点。
temp_info.data = 22;
AddNodeToTail(&headNode, &temp_info);
temp_info.data = 33;
AddNodeToTail(&headNode, &temp_info);
temp_info.data = 44;
AddNodeToHead(&headNode, &temp_info);
//读取列表
ReadList(&headNode);
//删除中间节点,看结果。
del_index = 2;
del_res = DeleteNode(&headNode, del_index);
if (del_res == -1)
{
printf("%s: CommNode %d delete fail!\r\n", __FUNCTION__, del_index);
}
else
{
printf("%s: CommNode %d delete success!\r\n", __FUNCTION__, del_index);
}
//读取列表
ReadList(&headNode);
//读取节点
r_index = 0;
r_info = ReadNode(&headNode, r_index);
printf("%s: CommNode %d's data = %d!\r\n", __FUNCTION__, r_index, r_info->data);
r_index = 2;
r_info = ReadNode(&headNode, r_index);
printf("%s: CommNode %d's data = %d!\r\n", __FUNCTION__, r_index, r_info->data);
r_index = GetListSize(&headNode)-1;
r_info = ReadNode(&headNode, r_index);
printf("%s: CommNode %d's data = %d!\r\n", __FUNCTION__, r_index, r_info->data);
printf("\n");
//更新节点
temp_info.data = 66;
r_index = 0;
UpdateNode(&headNode, r_index, &temp_info);
temp_info.data = 77;
r_index = 2;
UpdateNode(&headNode, r_index, &temp_info);
temp_info.data = 88;
r_index = GetListSize(&headNode) - 1;
UpdateNode(&headNode, r_index, &temp_info);
//读取列表
ReadList(&headNode);
//插入节点
temp_info.data = 99;
r_index = 0;
InsertNode(&headNode, r_index, &temp_info);
temp_info.data = 0xAA;
r_index = 2;
InsertNode(&headNode, r_index, &temp_info);
temp_info.data = 0xBB;
r_index = GetListSize(&headNode) - 1;
InsertNode(&headNode, r_index, &temp_info);
//读取列表
ReadList(&headNode);
//删除列表
DeleteList(&headNode);
//读取列表
ReadList(&headNode);
}
int _tmain(int argc, _TCHAR* argv[])
{
NodeTest();
while (1);
//return 0;
}