脚本宝典收集整理的这篇文章主要介绍了伪链表的相关操作,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdio.h>#include <stdlib.h>
struct Node{ int ShuJu; //数据 struct Node* pNext; //连接下一个节点的地址};
void Look_ShuJu(struct Node* pHead); //查看数据struct Node* FindDate(struct Node* pHead, int iDate); //查找数据struct Node* FindIndex(struct Node* pHead, int iIndex); //根据下标查找数据void ShowDate(struct Node* pNode); //输出查找结果int Count(struct Node* pHead, int iDate); //统计指定数据的数量void GetNode(struct Node* pHead, struct Node* arr[4], int iDate); //根据数据得到对应的节点void Change_ShuJu(struct Node* pHead, int i, int changeI); //修改数据 //地址;要修改的数据;修改为
int main(void){ struct Node a = { 123,NULL }, b = { 2,NULL }, c = { 888,NULL }, d = { 398,NULL }; a.pNext = &b; b.pNext = &c; c.pNext = &d;
//printf("%dn", a.ShuJu); //printf("%dn", a.pNext ->ShuJu ); //printf("%dn", a.pNext ->pNext ->ShuJu ); //printf("%dn", a.pNext->pNext ->pNext ->ShuJu);
//struct Node* pHead = &a; //printf("%dn", pHead->ShuJu);
//pHead = pHead->pNext; //printf("%dn", pHead->ShuJu);
//pHead = pHead->pNext; //printf("%dn", pHead->ShuJu);
//pHead = pHead->pNext; //printf("%dn", pHead->ShuJu);
//Look_ShuJu(&a); //查看数据
//struct Node* pFind=FindDate(&a, 3); //查找数据 //if (pFind == NULL) // printf("未找到节点n"); //else // printf("已找到节点:%dn", pFind->ShuJu );
//struct Node* pFind=FindIndex(&a, 2); //按下标查找数据 //if (pFind == NULL) // printf("未找到该下标对应的节点n"); //else // printf("已找到该下标对应的节点:%dn", pFind->ShuJu );
//printf("出现的次数为:%dn",Count(&a, 2) ); //统计指定数据的数量
//ShowDate(FindIndex(&a, 2)); //显示输出函数 //struct Node* arr[4] = { NULL }; //GetNode(&a, arr, 2); //for (int i = 0; i < 4 && arr[i] != NULL; i++) //{ // printf("数据:%dn", arr[i]->ShuJu); //}
////头添加 //struct Node A = { 111,&a }; //Look_ShuJu(&A);
////尾添加 //struct Node B = { 999,NULL }; //d.pNext = &B; //Look_ShuJu(&A);
////中间添加 //struct Node C = { 555,&c }; //b.pNext = &C; //Look_ShuJu(&A);
////删除头 //a.pNext = NULL; //Look_ShuJu(&b);
////删除尾 //c.pNext = NULL; //Look_ShuJu(&a);
////删除中间 //c.pNext = NULL; //b.pNext = &d; //Look_ShuJu(&a);
////修改数据 //Look_ShuJu(&a); //printf("修改后:n"); //Change_ShuJu(&a, 2, 222); //Look_ShuJu(&a);
system("pause>0"); return 0;}
void Look_ShuJu(struct Node* pHead){ while (pHead != NULL) { printf("%dn", pHead->ShuJu); pHead = pHead->pNext; //达到连接的作用 }}
struct Node* FindDate(struct Node* pHead, int iDate){ while (pHead != NULL) { if (pHead->ShuJu == iDate) return pHead; //找到数据 pHead = pHead->pNext; } return NULL; //未找到数据
}
struct Node* FindIndex(struct Node* pHead, int iIndex){ int iNum=0; //从0开始是以下标查找数据,1开始是第几个数据 while (pHead != NULL) { if (iNum == iIndex) { return pHead; } iNum++; pHead = pHead->pNext; } return NULL;}
void ShowDate(struct Node* pNode){ if (pNode == NULL) printf("未找到此节点n"); else printf("已找到此节点为:%dn", pNode->ShuJu);}
int Count(struct Node* pHead, int iDate){ int iCount = 0; while (pHead != NULL) { if (pHead->ShuJu == iDate) iCount++; pHead = pHead->pNext; } return iCount;}
void GetNode(struct Node* pHead, struct Node* arr[4], int iDate){ int i = 0; while (pHead != NULL) { if (iDate == pHead->ShuJu) { arr[i++] = pHead; //arr[i] = pHead; //等效 //i++; } pHead = pHead->pNext; }}
void Change_ShuJu(struct Node* pHead, int i, int changeI){ while (pHead != NULL) { if (pHead->ShuJu == i) { pHead->ShuJu = changeI; } pHead = pHead->pNext; }}
以上是脚本宝典为你收集整理的伪链表的相关操作全部内容,希望文章能够帮你解决伪链表的相关操作所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。