单链表代码实现:
#include <stdio.h> #include <malloc.h> #include <stdlib.h> typedef enum { true = 1, false = 0 } boolean; typedef struct Data { int item1; char item2; } * PDATA, Data; typedef struct Node{ Data data; struct Node * nextNode; } * PLINKLIST, LinkList, Node; /** * 创建链表 * @param pLinklist */ void initLinkList(PLINKLIST pLinklist) { pLinklist = (PLINKLIST) malloc(sizeof (LinkList) * 1); if (NULL == pLinklist) exit(-1); pLinklist -> nextNode = NULL; } /** * 判断链表是否为空 * @param pLinklist * @return */ boolean isEmpty(PLINKLIST pLinklist) { return NULL == pLinklist -> nextNode; } /** * 释放资源 * @param pLinklist */ void reliefLinkList(PLINKLIST pLinklist) { PLINKLIST tempPointer01 = pLinklist -> nextNode; PLINKLIST tempPointer02 = NULL; while (NULL != tempPointer01) { tempPointer02 = tempPointer01; tempPointer01 = tempPointer01 -> nextNode; free(tempPointer02); } } void clearLinkList(PLINKLIST pLinklist) { PLINKLIST nodeA = NULL, nodeB = NULL; nodeA = pLinklist -> nextNode; while (NULL != nodeA) { nodeB = nodeA -> nextNode; free(nodeA); nodeA = nodeB; } pLinklist -> nextNode = NULL; } int length(PLINKLIST pLinklist) { int len = 0; // if (isEmpty(pLinklist)) return len; PLINKLIST tempNode = pLinklist -> nextNode; while (NULL != tempNode) { tempNode = tempNode -> nextNode; ++ len; } return len; } /** * 获取指定索引,查找所在节点下的元素 * @param pLinklist * @param index * @param pData * @return */ boolean getElementByIndex(PLINKLIST pLinklist, int index, PDATA pData) { PLINKLIST tempNode = pLinklist -> nextNode; int i = 0; while (NULL != tempNode && i < index) { tempNode = tempNode -> nextNode; ++ i; } if (NULL == tempNode || i > index) return false; pData = &tempNode -> data; return true; } /** * 根据元素的地址来查找所在的节点 * @param pLinklist * @param pData * @return */ PLINKLIST getNodeByElement(PLINKLIST pLinklist, PDATA pData) { PLINKLIST tempNode = pLinklist -> nextNode; while (NULL != tempNode && &tempNode -> data != pData) tempNode = tempNode -> nextNode; return tempNode; } /** * 在指定位置上插入节点并携带元素 * @param pLinklist * @param pData * @param index * @return */ boolean insertNode(PLINKLIST pLinklist, PDATA pData, int index) { PLINKLIST tempNode = pLinklist -> nextNode; int i = 0; while (NULL != tempNode && i < index) { tempNode = tempNode -> nextNode; ++ i; } if (NULL == tempNode || i > index) return false; PLINKLIST newNode = (PLINKLIST)malloc(sizeof(LinkList) * 1); newNode -> data = *pData; newNode -> nextNode = tempNode -> nextNode; tempNode -> nextNode = newNode; return true; } /** * 删除指定位置的节点且保留该节点的元素 * @param pLinklist * @param pData * @param index * @return */ boolean deleteNode(PLINKLIST pLinklist, PDATA pData, int index) { PLINKLIST tempNode = pLinklist -> nextNode; int i = 0; while (NULL != tempNode && i < index) { tempNode = tempNode -> nextNode; ++ i; } if (NULL == tempNode || i > index) return false; PLINKLIST tempNode2 = tempNode -> nextNode; tempNode -> nextNode = tempNode2 -> nextNode; pData = &tempNode2 -> data; free(tempNode2); return true; } /** * 遍历输出 * @param pLinklist */ void traverse(PLINKLIST pLinklist) { if (isEmpty(pLinklist)) { printf("[]\n"); return; } PLINKLIST iterator = pLinklist -> nextNode; int i = 1; printf("["); while (NULL != iterator) { if (NULL == iterator -> nextNode) { printf( "(%d){%d, %c}]\n", i, iterator -> data.item1, iterator -> data.item2 ); break; } printf( "(%d){%d, %c}, ", i, iterator -> data.item1, iterator -> data.item2 ); iterator = iterator -> nextNode; ++ i; } } int main() { LinkList linkList; initLinkList(&linkList); PLINKLIST temp = &linkList; for (int i = 0; i < 5; ++i) { Node * newNode = (PLINKLIST) malloc(sizeof (Node)); // 模拟存储的数据 newNode -> data.item1 = rand() % 200 + 1; newNode -> data.item2 = (char)(rand() % 100 + 1); // 节点衔接与重置 temp -> nextNode = newNode; newNode -> nextNode = NULL; temp = newNode; } traverse(&linkList); return 0; }