【发布时间】:2012-10-16 22:39:30
【问题描述】:
我正在尝试创建一个链接列表,从用户那里获取单词,直到输入为空白,然后添加每个单词,以便列表保持字母顺序。但是,只打印第一个节点。有什么我做错了吗?这是我所拥有的(减去标题和声明):
//put in additional nodes until the input is blank
while(in != " "){
cin >> in;
newPtr->data = in;
prevPtr->data = "";
prevPtr->next = NULL;
nextPtr = list;
//shift the prevPtr and nextPtr until newPtr is alphabetically between them
while(!(prevPtr->data<=in && nextPtr->data>in)){
prevPtr = nextPtr;
nextPtr = prevPtr->next;
}
//make newPtr point to the next node
if(nextPtr != NULL){
newPtr->next = nextPtr;
}
//make newPtr the "next" pointer of the previous node, if any
if(prevPtr != NULL){
prevPtr->next = newPtr;
}
//if there's nothing before newPtr, make it the first node
else{
list = newPtr;
}
printList(list);
};
}
【问题讨论】:
-
我认为你应该发布你的整个程序。
-
我同意 paddy,你至少应该给我们 printList 的代码
标签: c++ sorting linked-list