struct SNode
{
struct SNode *pNext;
};
int GetNodeCnt(struct SNode *pNode)
{
struct SNode *pN = pNode;
int iRet = 0;
while (pN->pNext != NULL)
{
++iRet;
pN = pN->pNext;
}
return iRet;
}
singly_linked_list_node.h
struct SNode
{
int iData;
struct SNode *pNext;
};
main.c
#include <stdio.h>
#include <stdlib.h>
#include "singly_linked_list_node.h"
struct SNode *GetSpecificValue(struct SNode *pNode, int iSpecificValue)
{
struct SNode *pN = pNode;
while (pN != NULL)
{
pN = pN->pNext; // 入参的是Root,而Root没有值,从next开始
if (pN == NULL || pN->iData == iSpecificValue)
break;
}
return pN;
}
int main()
{
struct SNode *pRoot = (struct SNode *)malloc(sizeof(struct SNode));
pRoot->pNext = NULL;
GetSpecificValue(pRoot, 5);
system("pause");
return EXIT_SUCCESS;
}
使得函数更加复杂,根指针不再以节点指针的方式进行操作
doubly_liked_list_node.h
typedef struct NODE
{
struct NODE *fwd;
struct NODE *bwd;
int value;
} Node;
main.c
int dll_insert(Node *pHead, Node *pRear, int iValue)
{
register Node *this = NULL;
register Node *next = NULL;
register Node *new = NULL;
this = pHead;
while (this != NULL)
{
next = this->fwd;
if (next == NULL)
break;
if (iValue == next->value)
return 0;
if (next->value > iValue)
break;
this = next;
}
new = (Node *)malloc(sizeof(Node));
if(new == NULL)
return -1;
new->value = iValue;
if(this == pHead)
pHead = new;
else
this->fwd = new;
if(pHead == NULL || next == NULL)
pRear = new;
else
next->bwd = new;
new->fwd = next;
new->bwd = this;
return 0;
}
singly_linked_list_node.h
struct NODE
{
int iData;
struct NODE *pNext;
};
main.c
#include <stdio.h>
#include <stdlib.h>
#include "singly_linked_list_node.h"
struct NODE * sll_reverse( struct NODE *first)
{
if (first == NULL)
return NULL;
struct NODE *pPre = first; // 上一个
if (first->pNext == NULL)
return first;
struct NODE *pNow = pPre->pNext; // 现在的
if (first->pNext->pNext == NULL)
return first->pNext;
struct NODE *pN = pNow->pNext; // 下一个
struct NODE *pFirst = first;
while (pFirst != NULL)
{
pPre = first;
pNow = pPre->pNext;
pN = pNow->pNext;
while (pN != NULL)
{
if (pNow->iData < pN->iData) // 得到最大值
{
// 先转换Now和Next指针
pNow = pPre->pNext->pNext;
pN = pPre->pNext;
// 再断开指针的指向
pN->pNext = pNow->pNext;
pNow->pNext = pN;
pPre->pNext = pNow;
}
pPre = pPre->pNext;
pNow = pPre->pNext;
pN = pNow->pNext;
}
pFirst = pFirst->pNext;
}
return first->pNext;
}
int main()
{
struct NODE *pRoot = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->iData = 0;
pRoot->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->iData = 1;
pRoot->pNext->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->pNext->iData = 2;
pRoot->pNext->pNext->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->pNext->pNext->iData = 3;
pRoot->pNext->pNext->pNext->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->pNext->pNext->pNext->iData = 5;
pRoot->pNext->pNext->pNext->pNext->pNext = NULL;
sll_reverse(pRoot);
system("pause");
return EXIT_SUCCESS;
}
singly_linked_list_node.h
struct NODE
{
int iData;
struct NODE *pNext;
};
main.c
#include <stdio.h>
#include <stdlib.h>
#include "singly_linked_list_node.h"
int sll_remove( struct NODE **rootp, struct NODE *node )
{
if (rootp == NULL || (*rootp) == NULL || node == NULL)
return 0;
struct NODE *pRoot = (*rootp);
while (pRoot != NULL)
{
if (pRoot->pNext == node)
{
pRoot->pNext = node->pNext;
return 1;
}
pRoot = pRoot->pNext;
}
return 0;
}
int main()
{
struct NODE *pRoot = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->iData = 0;
pRoot->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->iData = 1;
pRoot->pNext->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->pNext->iData = 2;
pRoot->pNext->pNext->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->pNext->pNext->iData = 3;
pRoot->pNext->pNext->pNext->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->pNext->pNext->pNext->iData = 5;
pRoot->pNext->pNext->pNext->pNext->pNext = NULL;
sll_remove( &pRoot, pRoot->pNext->pNext->pNext );
system("pause");
return EXIT_SUCCESS;
}
虽然名字是双向链表的删除,但是单链表完全够用
doubly_linked_list_node.h
struct NODE
{
int iData;
struct NODE *pPre;
struct NODE *pNext;
};
main.c
#include <stdio.h>
#include <stdlib.h>
#include "doubly_linked_list_node.h"
int dll_remove( struct NODE *rootp, struct NODE *node )
{
if (rootp == NULL || node == NULL)
return 0;
struct NODE *pRoot = rootp;
while (pRoot != NULL)
{
if (pRoot->pNext == node)
{
pRoot->pNext = node->pNext;
pRoot->pNext->pPre = pRoot;
return 1;
}
pRoot = pRoot->pNext;
}
return 0;
}
int main()
{
struct NODE *pRoot = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->iData = 0;
pRoot->pPre = NULL;
pRoot->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->iData = 1;
pRoot->pNext->pPre = pRoot;
pRoot->pNext->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->pNext->iData = 2;
pRoot->pNext->pNext->pPre = pRoot->pNext;
pRoot->pNext->pNext->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->pNext->pNext->iData = 3;
pRoot->pNext->pNext->pNext->pPre = pRoot->pNext->pNext;
pRoot->pNext->pNext->pNext->pNext = (struct NODE *)malloc(sizeof(struct NODE));
pRoot->pNext->pNext->pNext->pNext->iData = 5;
pRoot->pNext->pNext->pNext->pNext->pPre = pRoot->pNext->pNext->pNext;
pRoot->pNext->pNext->pNext->pNext->pNext = NULL;
dll_remove( pRoot, pRoot->pNext->pNext->pNext );
system("pause");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct STwoLevelLink
{
char *pchStr; // 存放单词
struct STwoLevelLink *pNext; // 指向相同首字母的下一个指针
};
struct SOneLevelLink
{
char chInitial; // 首字母
struct SOneLevelLink *pBrother; // 指向具有其他首字母的指针
struct STwoLevelLink *pChild; // 指向这个首字母对应的单词的指针
};
int InsertWord(struct SOneLevelLink **pList, char *pchWords)
{
if (pList == NULL || (*pList) == NULL || pchWords == NULL || !isalpha(*pchWords))
return 0;
char chFlag = *pchWords; // 单词的首字母
int iCnt = strlen(pchWords);
struct SOneLevelLink *pListFlag = (*pList);
while (pListFlag != NULL)
{
if (pListFlag->chInitial == chFlag) // 存在这个首字母
{
struct STwoLevelLink **pListTwoLevel = &(pListFlag->pChild);
if (pListTwoLevel != NULL && (*pListTwoLevel) == NULL)
{
(*pListTwoLevel) = (struct STwoLevelLink *)malloc(sizeof(struct STwoLevelLink));
(*pListTwoLevel)->pchStr = (char *)malloc(sizeof(char) * iCnt);
strcpy((*pListTwoLevel)->pchStr, pchWords);
(*pListTwoLevel)->pNext = NULL;
return 1;
}
while (pListTwoLevel != NULL && (*pListTwoLevel) != NULL)
{
if (0 == strcmp((*pListTwoLevel)->pchStr, pchWords))
{
return 0; // 存在这个单词
}
else
{
if (0 > strcmp((*pListTwoLevel)->pchStr, pchWords))
{
if ((*pListTwoLevel)->pNext == NULL)
{
(*pListTwoLevel)->pNext = (struct STwoLevelLink *)malloc(sizeof(struct STwoLevelLink));
(*pListTwoLevel)->pNext->pchStr = (char *)malloc(sizeof(char) * iCnt);
strcpy((*pListTwoLevel)->pNext->pchStr, pchWords);
(*pListTwoLevel)->pNext->pNext = NULL;
return 1;
}
if (0 < strcmp((*pListTwoLevel)->pNext->pchStr, pchWords))
{
struct STwoLevelLink *pN = (struct STwoLevelLink *)malloc(sizeof(struct STwoLevelLink));
pN->pchStr = (char *)malloc(sizeof(char) * iCnt);
strcpy(pN->pchStr, pchWords);
pN->pNext = (*pListTwoLevel)->pNext;
(*pListTwoLevel)->pNext = pN;
}
}
return 1;
}
(*pListTwoLevel) = (*pListTwoLevel)->pNext;
}
// 不存在这个单词
}
pListFlag = pListFlag->pBrother;
}
// 表中没有这个字母开头的一级节点
pListFlag = (*pList);
while (pListFlag != NULL)
{
if (pListFlag->pBrother->chInitial > chFlag) // 不可能等于,只能大于或者小于
{
struct SOneLevelLink *pNew = (struct SOneLevelLink *)malloc(sizeof(struct SOneLevelLink));
pNew->pBrother = pListFlag->pBrother;
pListFlag->pBrother = pNew;
pNew->chInitial = chFlag;
struct STwoLevelLink *pStr = (struct STwoLevelLink *)malloc(sizeof(struct STwoLevelLink));
pStr->pchStr = (char *)malloc(sizeof(char) * iCnt);
strcpy(pStr->pchStr, pchWords);
pStr->pNext = NULL;
pNew->pChild = pStr;
return 1;
}
pListFlag = pListFlag->pBrother;
}
// 表示新增的节点首字母是最大的情况
// 此时pListFlag == NULL, 即上一个节点的pListFlag->pBrother为空
pListFlag = (struct SOneLevelLink *)malloc(sizeof(struct SOneLevelLink));
pListFlag->pBrother = NULL;
pListFlag->chInitial = chFlag;
struct STwoLevelLink *pStr = (struct STwoLevelLink *)malloc(sizeof(struct STwoLevelLink));
pStr->pchStr = (char *)malloc(sizeof(char) * iCnt);
strcpy(pStr->pchStr, pchWords);
pStr->pNext = NULL;
pListFlag->pChild = pStr;
return 1;
}
int main()
{
struct SOneLevelLink *pRoot = (struct SOneLevelLink *)malloc(sizeof(struct SOneLevelLink));
pRoot->chInitial = 0;
pRoot->pChild = NULL;
pRoot->pBrother = (struct SOneLevelLink *)malloc(sizeof(struct SOneLevelLink));
pRoot->pBrother->chInitial = 'a';
pRoot->pBrother->pChild = NULL;
pRoot->pBrother->pBrother = (struct SOneLevelLink *)malloc(sizeof(struct SOneLevelLink));
pRoot->pBrother->pBrother->chInitial = 'b';
pRoot->pBrother->pBrother->pBrother = NULL;
pRoot->pBrother->pBrother->pChild = NULL;
InsertWord(&pRoot, "apple");
// system("pause");
return EXIT_SUCCESS;
}