链表实现

    xiaoxiao2021-03-25  71

    // //初始化 #include <stdio.h> #include <windows.h> #include <malloc.h> typedef int DataType; typedef struct Node {  DataType data;  struct Node *next; }Node, *PNode; void InitList(PNode *pHead) { *pHead = NULL; } //创建新节点 PNode BuyNode(DataType data) { PNode pCur = (PNode)malloc(sizeof(Node)); pCur->data = data; pCur->next = NULL; return pCur; } //获取最后的结点 PNode Back(PNode pHead) { PNode pCur = NULL; if (NULL != pHead) { pCur = pHead; while (NULL != pCur->next) { pCur = pCur->next; } } return pCur; } //释放结点 void Free(PNode pNode) { free(pNode); } //判断链表是否为空 int NoEmpty(PNode pHead) { if (NULL != pHead) return 1; return 0; } //结点个数 size_t Size(PNode pHead) { size_t count = 0; if (NoEmpty(pHead)) { while (NULL != pHead) { pHead = pHead->next; count++; } } return count; } //打印 void Print(PNode *pHead) {  PNode pCur = *pHead;  while (pCur)  {   printf("%d-> ", pCur->data);   pCur = pCur->next;  }  printf("\n"); } / //尾插法插入新的结点 void PushBack(PNode *pHead, DataType data) { PNode pCur = Back(*pHead); PNode pTmp = BuyNode(data); if (NoEmpty(*pHead)) { pCur->next = pTmp; } else { *pHead = pTmp; } } //删除最后一个节点 void PopBack(PNode *pHead) { if (NoEmpty(*pHead)) { PNode pCur = *pHead; while (NULL != pCur->next->next) { pCur = pCur->next; } Free(pCur->next); pCur->next = NULL; } } //头插法插入新的结点 void PushFront(PNode *pHead, DataType data) { PNode pTmp = BuyNode(data); PNode pCur = *pHead; if (NoEmpty(*pHead)) { *pHead = pTmp; (*pHead)->next = pCur; } else { *pHead = pTmp; } } //删除第一个节点 void PopFront(PNode *pHead) { if (NoEmpty(*pHead)) { PNode pCur = *pHead; *pHead = (*pHead)->next; Free(pCur); } } / //查找值为data的结点(第一个),返回该节点的地址 PNode Find(PNode pHead, DataType data) { PNode pCur = NULL; if (NoEmpty(pHead)) { pCur = pHead; while (NULL != pCur->next && pCur->data != data) { pCur = pCur->next; } } return pCur; } //在pos位置之后插入新节点 void Insert(PNode pos, DataType data) { if (NULL != pos) { PNode pCur = pos->next; pos->next = BuyNode(data); pos->next->next = pCur; } } //删除POS位置上的结点 void Erase(PNode *pHead, PNode pos) { if (NULL != *pHead) { PNode pCur = *pHead; while (pCur->next != pos) { pCur = pCur->next; } pCur->next = pos->next; Free(pos); } } //删除第一个值为data的结点 void Remove(PNode *pHead, DataType data) { if (NULL != *pHead) { PNode pCur = *pHead; while (pCur->data != data) { pCur = pCur->next; } Erase(pHead, pCur); } } //删除全部值为data的结点 void RemoveAll(PNode *pHead, DataType data) { if (NULL != *pHead) { PNode pCur = *pHead; PNode pTmp = *pHead; while (NULL != pTmp) { while (NULL != pCur && pCur->data != data) { pCur = pCur->next; pTmp = pTmp->next; } if (NULL != pCur) { pTmp = pTmp->next; Erase(pHead, pCur); pCur = pTmp; } } } } //删除链表 void DeleteList(PNode *pHead) { if ((NULL != pHead) && (NULL != *pHead)) { PNode pCur = *pHead; while (pCur) { PNode pNext = pCur->next; Free(pCur); pCur = pNext; } *pHead = NULL; } } int main() {  PNode pHead1 = NULL;  PNode pHead2 = NULL;  PushBack(&pHead1, 1);  PushBack(&pHead1, 2);  PushBack(&pHead1, 3);  Print(&pHead1);  PushFront(&pHead2, 4);  PushFront(&pHead2, 5);  PushFront(&pHead2, 6);  Print(&pHead2);  Insert(Back(pHead1), 3);  Print(&pHead1);  PopFront(&pHead1);  Print(&pHead1);  RemoveAll(&pHead1, 3);  Print(&pHead1);  system("pause");  return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-37552.html

    最新回复(0)