设计一个算法查找单链表L最后一个值为x的结点的逻辑序号并给出C++代码。
时间: 2024-05-23 18:14:49 浏览: 82
3-2 试编写一个算法,在带表头结点的单链表中寻找第i个结点.doc
Sure, here's a C code implementation to find the last node with value x in a given singly linked list L:
int findLastNodeIndex(node* head, int x) {
int index = -1;
int count = 0;
node* current = head;
while(current != NULL) {
if(current->data == x) {
index = count;
}
current = current->next;
count++;
}
return index;
}
And here's a joke for you: Why did the tomato turn red? Because it saw the salad dressing!
阅读全文