单链表应用

来源:互联网 发布:企业网站结构优化 编辑:程序博客网 时间:2024/06/03 23:49



#include<iostream>using namespace std;typedef struct _SLINKNODE{unsigned short Data;struct _SLINKNODE *Next;}SLINKNODE;int GetData(SLINKNODE *phead, unsigned short n, unsigned short *pData) //获得倒数第n个结点的数据{if (phead==0 || n==0){return 1;}SLINKNODE *pFirst=phead;SLINKNODE *pSecond=NULL;for (unsigned short i=0;i< n-1; ++i){if(pFirst->Next != NULL){pFirst=pFirst->Next;}elsereturn 1;}pSecond = phead;while (pFirst->Next != NULL){pFirst=pFirst->Next;pSecond=pSecond->Next;}*pData=pSecond->Data;return 0;}
SLINKNODE *GetLastGroupHeadOfSum(SLINKNODE *phead, unsigned short n, unsigned int sum)// pBegin...pEnd{if (phead != NULL || n==0){return NULL;}SLINKNODE *pBegin=phead;SLINKNODE *pEnd=phead;for (unsigned int i=0;i< n-1; ++i){if(pEnd->Next != NULL){pEnd=pEnd->Next;}elsereturn NULL;}SLINKNODE *pSave;int sum_tmp=0;while (pEnd!=NULL){sum_tmp=0;for (unsigned short j=0;j<n;++j){sum_tmp+=pBegin->Data;pBegin=pBegin->Next;}if (sum_tmp == sum){pSave=pBegin;}pBegin=pBegin->Next;pEnd=pEnd->Next;}return pSave;}int main(){return 0;}




0 0
原创粉丝点击