头文件
#ifndef _LINK_H_
#define _LINK_H_
typedef struct StringNode
{
char *pData;
int nLen;
StringNode *next;
}StringNode;
/*1.向链表尾部插入一个元素*/
int TailInsertNode(StringNode **pNode, char *pTailData);
/*2.遍历*/
void PrintList(StringNode *pHead);
/*3.清空*/
void DeleteList(StringNode **pHead);
/*4.返回链表长度*/
int SizeList(StringNode *pHead);
/*5.删除指定链表节点*/
int DeleteOneNode(StringNode **pHead, char *strDel);
/*6.是否空链表*/
bool IsEmptyList(StringNode *pHead);
/*7.查找指定字符串*/
bool IsExistInList(StringNode *pHead, char *strFind);
#endif
源文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "link.h"
/**使用范例
int main(void)
{
StringNode* pFileNode = NULL;
char str[256]={0};
strncpy(str, "hello world \n", 6);
TailInsertNode(&pFileNode, str);
TailInsertNode(&pFileNode, "asdj asda");
PrintList(pFileNode);
if (!IsEmptyList(pFileNode))
{
if (IsExistInList(pFileNode, "hello "))
{
DeleteOneNode(&pFileNode, "hello ");
PrintList(pFileNode);
}
DeleteList(&pFileNode);
PrintList(pFileNode);
}
getchar();
return 0;
}
*/
/*1.在尾部插入新节点*/
int TailInsertNode(StringNode **pNode, char *pTailData)
{
StringNode *pInsert;
StringNode *pHead;
StringNode *pTmp;<span style="white-space:pre"> </span>//定义一个临时链表用来存放第一个节点
pHead = *pNode;
pTmp = pHead;
pInsert = (StringNode *)malloc(sizeof(StringNode));<span style="white-space:pre"> </span>//申请一个新节点
if (NULL == pInsert)
{
printf("内存分配失败\n");
<span style="white-space:pre"> </span>exit(0);
}
memset(pInsert, 0, sizeof(StringNode));
pInsert->nLen = strlen(pTailData)+1;
pInsert->pData = (char *)malloc(pInsert->nLen);
if (NULL == pInsert->pData)
{
printf("内存分配失败\n");
<span style="white-space:pre"> </span>exit(0);
}
strcpy(pInsert->pData, pTailData);
pInsert->next = NULL;
/*如果头结点没有数据,则新节点作为走节点*/
if (pHead == NULL)
{
pHead = pInsert;
}
else
{
while(pTmp->next != NULL)
{
pTmp = pTmp->next;
}
pTmp->next = pInsert;<span style="white-space:pre"> </span>//将链表末尾节点的下一结点指向新添加的节点
}
*pNode = pHead;
return 0;
}
/*2.遍历*/
void PrintList(StringNode *pHead)
{
while (NULL != pHead)
{
printf("%s\n", pHead->pData);
pHead = pHead->next;
}
}
/*3.清空*/
void DeleteList(StringNode **pHead)
{
StringNode *pTmp;
while((*pHead) != NULL)
{
pTmp = (*pHead);
(*pHead) = (*pHead)->next;
free(pTmp->pData);
free(pTmp);
}
*pHead = NULL;
}
/*4.返回链表长度*/
int SizeList(StringNode *pHead)
{
int nSize = 0;
while(pHead != NULL)
{
nSize++;
pHead = pHead->next;
}
return nSize;
}
/*5.删除指定链表节点*/
int DeleteOneNode(StringNode **pHead, char *strDel)
{
StringNode *pTmp;
StringNode *pBack; ///< 上一节点
pTmp = *pHead;
while(1)
{
/*首节点*/
if (strcmp((*pHead)->pData, strDel) == 0)
{
if (1 == SizeList(*pHead)) ///< 链表长度为1
{
DeleteList(pHead);
break;
}
else
{
*pHead = pTmp->next;
free(pTmp->pData);
free(pTmp);
break;
}
}
if (pTmp->next == NULL)
{
break;
}
pBack = pTmp;
pTmp = pTmp->next;
if (strcmp(pTmp->pData, strDel) == 0)
{
pBack->next = pTmp->next;
free(pTmp->pData);
free(pTmp);
break;
}
}
return 0;
}
/*6.是否空链表*/
bool IsEmptyList(StringNode *pHead)
{
if (NULL == pHead)
{
return true;
}
return false;
}
bool IsExistInList(StringNode *pHead, char *strFind)
{
while (NULL != pHead)
{
if (strcmp(pHead->pData, strFind) == 0)
{
return true;
}
pHead = pHead->next;
}
return false;
}