先上结果
#include
#include
#include
struct node
{
node *pNext;
node* pPre;
int Id;
int count;
};
void freeStrList(char ***strList, int *subCount);
void freeNodeList(node **nodeList);
int s2i(const char * str, int *num);
void addNode(node **pNodeList, char *strData);
int getNodeLen(node *pNodeList);
node * getNode(node *pNodeList,int arrPos);
int splitString(char* str, char splitStr, char ***pStr, int *subStrCount);
void operatorPorcess(char * inputBuf);
void main()
{
char *inputBuf = NULL;
char **subBuf = NULL;
int subCount = 0;
int i;
node *pNodeList = NULL;
int nodeCount = 0;
node *pNodeTmp = NULL;
inputBuf = (char*)malloc(sizeof(char)* 8192);
if (NULL == inputBuf)
return;
//scanf("%s", inputBuf);
sprintf(inputBuf, "1-12/2-2/3-3/4-4/1-22/1-1/2-2/3-3/4-5/1-12/");
operatorPorcess(inputBuf);
sprintf(inputBuf, "20140203-15/20140204-5669/20140331-1/20140406-6/");
operatorPorcess(inputBuf);
sprintf(inputBuf, "20140203-15/20140204-5669/20140331-1/");
operatorPorcess(inputBuf);
free(inputBuf);
}
/**/
void operatorPorcess(char * inputBuf)
{
char **subBuf = NULL;
int subCount = 0;
int i;
node *pNodeList = NULL;
int nodeCount = 0;
node *pNodeTmp = NULL;
printf("\n-----inPut-----\n");
printf("%s\n", inputBuf);
splitString(inputBuf, '/', &subBuf, &subCount);
printf("\n-----Data List-----\n");
for (i = 0; i < subCount; i++)
{
printf("%d : %s\n", i, *(subBuf + i));
addNode(&pNodeList, *(subBuf + i));
}
freeStrList(&subBuf, &subCount);
printf("\n-----Node List-----\n");
pNodeTmp = pNodeList;
while (pNodeTmp)
{
printf("%d : %d\n", pNodeTmp->Id, pNodeTmp->count);
pNodeTmp = pNodeTmp->pNext;
}
printf("\n-----outPut-----\n");
nodeCount = getNodeLen(pNodeList);
pNodeTmp = getNode(pNodeList, nodeCount / 2);
printf("%d , %d\n", nodeCount, pNodeTmp->count);
freeNodeList(&pNodeList);
}
/*字符串分割工具
把字符串 str 以 splitStr 分割到 pStr,记录分割后子字符串数 subStrCount
执行成功返回 0
*/
int splitString(char* str, char splitStr,char ***pStr, int *subStrCount)
{
int strLen = strlen(str);
int pLen = (strLen + 4) / 4 * 4;
int flg = 1;
char *pSource = (char *)malloc(sizeof(char)* pLen);
memset(pSource, 0, pLen);
memcpy(pSource, str, strLen);
char *p;
(*subStrCount) = 0;
for (int i = 0; i < strLen; i++)
{
if (*(pSource + i) == splitStr)
{
(*subStrCount)++;
*(pSource + i) = 0;
if (i == strLen - 1)//处理是否以分割符为结尾,不是则在当前计数上还得加一
flg = 0;
}
}
if (flg)
(*subStrCount)++;
(*pStr) = (char **)malloc(sizeof(char **)* (*subStrCount));
p = pSource;
for (int i = 0; i < *subStrCount; i++)
{
//给每个字串分配空间,副职
pLen = (strlen(p) + 4) / 4 * 4;
*(*pStr + i) = (char *)malloc(pLen * sizeof(char *));
memset(*(*pStr + i), 0, pLen);
memcpy(*(*pStr + i), p, strlen(p));
p += strlen(p) + 1;
}
return 0;
}
/*字符串转整型数
数字型字符串转换为相应的整型数字
转换正常返回 0
转换失败返回 -1;
*/
int s2i(const char * str, int *num)
{
int i;
*num = 0;
if (strlen(str) == 0)
{
return -1;
}
for (i = 0; i < strlen(str); i++)
{
if (*(str + i)<'0' || *(str + i) > '9')
{
return -1;
}
*num = (*num) * 10 + (*(str + i)) - '0';
}
return 0;
}
/*
将strData 记录的 id、count 记录到链表里
记录时以id从小到大排序*/
void addNode(node **pNodeList, char *strData)
{
node *pNodeTmp = *pNodeList;
char **subBuf = NULL;
int subCount = 0;
int id = 0;
int count = 0;
splitString(strData, '-', &subBuf, &subCount);
s2i(*(subBuf + 0), &id);
s2i(*(subBuf + 1), &count);
freeStrList(&subBuf, &subCount);
if (*pNodeList == NULL)
{
//第一次操作,链表为空
*pNodeList = (node*)malloc(sizeof(node));
(*pNodeList)->pPre = NULL;
(*pNodeList)->pNext = NULL;
pNodeTmp = *pNodeList;
}
else
{
//遍历链表找到第一个节点id大于等于当前id的节点
while (pNodeTmp->pNext &&id > pNodeTmp->Id)
{
pNodeTmp = pNodeTmp->pNext;
}
//先处理等于情况,不新增节点
if (id == pNodeTmp->Id)
{
pNodeTmp->count += count;
return;
}
//遍历到链表尾,再尾插入新节点
else if(pNodeTmp->pNext == NULL)
{
pNodeTmp->pNext = (node*)malloc(sizeof(node));
pNodeTmp->pNext->pPre = pNodeTmp;
pNodeTmp->pNext->pNext = NULL;
pNodeTmp = pNodeTmp->pNext;
}
//在链表中间插入节点
else if (id < pNodeTmp->Id)
{
pNodeTmp->pPre->pNext = (node*)malloc(sizeof(node));
pNodeTmp->pPre = pNodeTmp->pPre->pNext;
pNodeTmp->pPre->pNext->pPre = pNodeTmp->pPre;
pNodeTmp->pPre->pNext->pNext = pNodeTmp;
pNodeTmp = pNodeTmp->pPre->pNext;
}
//
else
{
}
}
pNodeTmp->Id = id;
pNodeTmp->count = count;
}
//获取链表长度
int getNodeLen(node *pNodeList)
{
int len = 0;
while (pNodeList)
{
len++;
pNodeList = pNodeList->pNext;
}
return len;
}
//获取链表第 arrPos 位置节点
node * getNode(node *pNodeList, int arrPos)
{
while (arrPos)
{
arrPos--;
pNodeList = pNodeList->pNext;
}
return pNodeList;
}
//释放子字串
void freeStrList(char ***strList,int *subCount)
{
int i;
for (i = 0; i < *subCount; i++)
{
free(*(*strList + i));
}
free(*strList);
*strList = NULL;
*subCount = 0;
}
//释放链表
void freeNodeList(node **nodeList)
{
node *nodeTmp = NULL;
node *pNodeList = *nodeList;
while (pNodeList)
{
nodeTmp = pNodeList;
pNodeList = pNodeList->pNext;
free(nodeTmp);
}
*nodeList = NULL;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////玩过头了。。。。。。。。。。。
///////////////////////