添加节点按字母用C [英] Add node alphabetical in C
本文介绍了添加节点按字母用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以退换货addWord一部分,这将增加字母词列表?我试过,但没有位于C programming.Actually work.I'm很新,我的工作就可以了,因为一个星期,但我不能问题。
typedef结构节点{
字符*海峡;
诠释计数;
结构NODE * pNext;
}节点;
无效addWord(字符*字)
{
NODE *的Pcounter = NULL;
NODE * PLAST = NULL; 如果(PSTART == NULL)// PSTART是全局定义PSTART = NULL;
{
PSTART = createWordCounter(字);
返回;
} / *如果单词列表中,递增其计* /
Pcounter可= PSTART;
而(Pcounter可!= NULL)
{
如果(STRCMP(文字,pCounter-> STR)== 0)
{
++ pCounter->计数; 返回;
}
PLAST =的Pcounter;
Pcounter可= pCounter-> pNext;
} / *单词不在列表中,添加* /
pLast-> pNext = createWord(字);
} NODE * createWord(字符*字)
{
NODE *的Pcounter = NULL;
Pcounter可=(NODE *)malloc的(的sizeof(节点));
pCounter-> STR =(字符*)malloc的(strlen的(字)+1);
的strcpy(pCounter-> STR,字);
pCounter->计数= 1;
pCounter-> pNext = NULL;
返回的Pcounter;
}
我想这部分,但它并不会返回result.Here
无效addWord(字符*字)
{
NODE *的Pcounter = NULL;
NODE * PLAST = NULL;
NODE * Pnew最多= NULL; 如果(PSTART == NULL)
{
PSTART = createWordCounter(字);
返回;
} / *如果单词列表中,递增其计* /
Pcounter可= PSTART;
而(Pcounter可!= NULL)
{
如果(STRCMP(文字,pCounter-> STR)== 0)
{
++ pCounter->计数; 返回;
}
PLAST =的Pcounter;
Pcounter可= pCounter-> pNext;
}
而(Pcounter可!= NULL){
如果(的strcmp(字,pCounter-> STR)℃,){
Pnew最多= createWordCounter(字);
pLast-> pNext = Pnew最多;
pNew-> pNext =的Pcounter;
Pcounter可= pLast-> pNext;
}
其他{
Pnew最多= createWordCounter(字);
pNew-> pNext =的Pcounter;
pLast-> pNext = Pnew最多;
}
}
解决方案
更新:
试试这个
而(的Pcounter!= NULL)
{
如果(STRCMP(文字,pCounter-> STR)== 0)
{
++ pCounter->计数;
返回;
} 如果(的strcmp(字,pCounter-> STR)℃的)
{
如果(Pcounter可== PSTART)
{
// TODO:在插入开始时
返回;
}
其他
{
Pnew最多= createWordCounter(字);
pLast-> pNext = Pnew最多;
pNew-> pNext =的Pcounter;
Pcounter可= pLast-> pNext;
返回;
}
} PLAST =的Pcounter;
Pcounter可= pCounter-> pNext;
}/ *单词不在列表中,添加* /
如果(PLAST == NULL)
{
// TODO:在插入开始时
}
其他
pLast-> pNext = createWord(字);
Can you return addWord part which will add word the list alphabetically?i tried but it didn't work.I'm very new at C programming.Actually,i'm working on it since one week but i can not the problem.
typedef struct NODE {
char *str;
int count;
struct NODE *pNext;
} NODE;
void addWord(char *word)
{
NODE *pCounter = NULL;
NODE *pLast = NULL;
if(pStart == NULL) // pstart is globally defined pStart=NULL;
{
pStart = createWordCounter(word);
return;
}
/* If the word is in the list, increment its count */
pCounter = pStart;
while(pCounter != NULL)
{
if(strcmp(word, pCounter->str) == 0)
{
++pCounter->count;
return;
}
pLast = pCounter;
pCounter = pCounter->pNext;
}
/* Word is not in the list, add it */
pLast->pNext = createWord(word);
}
NODE* createWord(char *word)
{
NODE *pCounter = NULL;
pCounter = (NODE*)malloc(sizeof(NODE));
pCounter->str = (char*)malloc(strlen(word)+1);
strcpy(pCounter->str, word);
pCounter->count = 1;
pCounter->pNext = NULL;
return pCounter;
}
I tried this part but it didn't return the result.Here
void addWord(char *word)
{
NODE *pCounter = NULL;
NODE *pLast = NULL;
NODE *pNew = NULL;
if(pStart == NULL)
{
pStart = createWordCounter(word);
return;
}
/* If the word is in the list, increment its count */
pCounter = pStart;
while(pCounter != NULL)
{
if(strcmp(word, pCounter->str) == 0)
{
++pCounter->count;
return;
}
pLast = pCounter;
pCounter = pCounter->pNext;
}
while(pCounter != NULL){
if(strcmp(word,pCounter->str)<0){
pNew =createWordCounter(word);
pLast->pNext = pNew ;
pNew->pNext = pCounter;
pCounter = pLast->pNext;
}
else{
pNew=createWordCounter(word);
pNew->pNext=pCounter;
pLast->pNext=pNew;
}
}
解决方案
Updated:
Try this
while(pCounter != NULL)
{
if(strcmp(word, pCounter->str) == 0)
{
++pCounter->count;
return;
}
if(strcmp(word,pCounter->str)<0)
{
if (pCounter == pStart)
{
// TODO: insert at the begining
return;
}
else
{
pNew = createWordCounter(word);
pLast->pNext = pNew;
pNew->pNext = pCounter;
pCounter = pLast->pNext;
return;
}
}
pLast = pCounter;
pCounter = pCounter->pNext;
}
/* Word is not in the list, add it */
if (pLast == NULL)
{
// TODO: insert at the begining
}
else
pLast->pNext = createWord(word);
这篇关于添加节点按字母用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文