【发布时间】:2015-04-12 22:16:29
【问题描述】:
我有一个 C 语言编程问题,我正在尝试设计一个列表并在给定索引处添加一个元素。
这是我的 insertElement 方法:
ListElement* getNextElement(ListElement *listElement){
return listElement->nextElement;
}
/* Insert a given element at the specified index in a specified list. Shifts
* all other elements to the right, increasing their index by 1.
* Requires 0 <= index <= listSize(), otherwise the element should not be inserted.
*/
void insertElement(List *list, ListElement *listElement, int index) {
ListElement* tempElement = list->headElement;
int count = 0;
while (tempElement != NULL) {
if (count == index) {
}
tempElement = getNextElement(tempElement);
count++;
}
}
但我实际上并不知道如何移动和插入元素。
这是我尝试插入的方式:
int main() {
ListElement* newElement = malloc(sizeof(ListElement));
insertElement(&myList, newElement, 1);
exit(EXIT_SUCCESS);
}
谁能帮帮我?提前致谢。
【问题讨论】:
-
索引和列表听起来很奇怪。马修给你一些提示