Ch2-2: return the nth to the last node data of a singly linked list

本文介绍了一种使用递归的方法来找到单链表中倒数第N个节点的数据。通过递归特性实现后进先出(LIFO)原则,文章详细展示了递归函数的设计思路及其实现代码,并对比了两种不同实现方式的效果。

In this problem, I need to find the nth to the last node data of a singly linked list. 

In order to do so, one good way is to use recursion, since it is automatically put recursion part into stack and what we want is to implement LIFO.

So here it comes:

void findn(node* head){
	if(head==NULL) return;
	//head = head->next; // if use it, output n-1th not nth
	findn(head->next);
	if(nn==1) pp = head;
	--nn;
}

It is very important to fully understand recursion. A good example that I followed is from <Absolute C++, 4th>.


full code comes here, very useful to learn C++. But still lack of use of class...Need to learn it, as well as .h file.

// 2-2 tony

#include 

using namespace std;

struct node{
    int data;
    node* next;
};

node* init(int* a, int n){
	node *head, *p;
	for (int i=0; idata = a[i];
		if(i==0){
			head = p = nd;
			continue;
		}
		p->next = nd;
		p = nd;
	}
    return head;
}

void print(node* head){
    if(head==NULL) {
        cout << "end" << endl;
        return;  // must add return here so 
        			// as to jump out of recursion
    }
    cout<data<<"_ ";
    head=head->next;     
	print(head);
}

//
	int nn;
	node* pp;
//

void findn(node* head){
	if(head==NULL) return;	
	findn(head->next);
	if(nn==1) pp = head;
	--nn;
}

node* findn1(node *head, int n){
	if(head==NULL || n<1) return NULL;
	node *p, *q;
	p = q = head;
	while(n>0 && q){
		n--;
		q = q->next;
	}
	if(n>0) return NULL;
	while(q){
		p=p->next;
		q=q->next;
	}
	return p;
}

int main(){
	int n = 10;
	int a[] = {
        10,9,8,7,6,5,4,3,2,1
    };

    node* head = init(a,n);
    
    nn = 6;
    print(head);
    node* p = findn1(head, 6);
    if(p) cout<< "solution1: " <data<data << endl;
    return 0;

}

/*
node* init(int* a, int n){
	node *head, *p = head->next;
	if(n==0) return;
	else if(n==1) return;
	else{
		head->data = a[0];
		for(int i=1; i< n; ++i){
			p->data = a[i]
		}
		return head;
	}
}
*/
Executing the program....
$demo 
10_ 9_ 8_ 7_ 6_ 5_ 4_ 3_ 2_ 1_ end
solution1: 6
solution2: 6


I can also have depth variable feed as input to findn() function but should use (depth==2) so as to get the 6th to the last as output instead of 5th. need more deep understanding of recursion: chaining;;;;
void findn(node* head, int depth){
	if(head==NULL) return;
	findn(head->next, depth-1);
	if(depth==1) pp = head;  //But the index is wrong
	//--depth;
}
Here is the not correct output: should get 6 instead of 5
Executing the program....
$demo 
10_ 9_ 8_ 7_ 6_ 5_ 4_ 3_ 2_ 1_ end
5


 

<think>嗯,用户想简化他们的CSS代码。具体来说,他们有一个类名为.health-card-footer-list的元素,使用了:nth-child(n)来为n=1、2、3的情况添加右边框和内边距。现在他们想简化这段代码。 首先,我需要回顾一下CSS的伪类选择器,特别是:nth-child的用法。根据引用[2],:nth-child(n)选择的是父元素下的第n个子元素,不论类型。而用户可能想对前三个子元素应用相同的样式。 用户目前的代码可能是这样的: .health-card-footer-list:nth-child(1), .health-card-footer-list:nth-child(2), .health-card-footer-list:nth-child(3) { border-right: 1px solid #C5C5C5; padding-right: 10px; } 但这样写重复了三次,比较冗长。用户希望简化它,所以需要找到一种更简洁的写法。 根据引用[1],使用:nth-child(-n+3)可以选择前三个元素。因为-n+3的公式中,当n=0时是3,n=1时是2,n=2时是1,n=3时是0,所以选中的是第1、2、3个子元素。所以可以替换为: .health-card-footer-list:nth-child(-n+3) { border-right: 1px solid #C5C5C5; padding-right: 10px; } 不过需要注意的是,这里用户的选择器是.health-card-footer-list:nth-child(-n+3),这意味着只有当该元素同时是.health-card-footer-list类,并且是父元素的第1、2、3个子元素时才会生效。如果父元素下的其他类型的子元素存在,可能会影响选择结果。比如,如果父元素的前三个子元素中有非.health-card-footer-list的元素,那么选择器可能不会命中预期的元素。这时候可能需要使用:nth-of-type,但用户的问题中没有提到类型的问题,所以假设父元素下的前三个子元素都是.health-card-footer-list类的话,使用:nth-child(-n+3)是正确的。 另外,引用[4]提到:nth-of-type(n)和:nth-child(n)的区别,如果用户需要确保选中的是同类型的元素(比如都是div或li),可能需要考虑使用:nth-of-type。但根据用户提供的类名,可能不需要,因为类选择器已经限定了元素的类,所以:nth-child(-n+3)应该足够。 总结一下,用户可以通过将三个单独的nth-child选择器合并为:nth-child(-n+3)来简化代码。这样不仅代码更简洁,也更容易维护。</think>要简化`.health-card-footer-list:nth-child(n)`在$n=1,2,3$时的CSS代码,可以使用`:nth-child(-n+3)`选择器。这是通过公式计算实现的批量选择方式,符合CSS伪类选择器的语法规则[^1][^2]。 原始代码可简化为: ```css .health-card-footer-list:nth-child(-n+3) { border-right: 1px solid #C5C5C5; padding-right: 10px; } ``` **原理说明**: 1. `-n+3`公式表示选择序号$\leq3$的元素 2. 当$n=0$时选中第3个元素,$n=1$时选中第2个,$n=2$时选中第1个,覆盖了前3个元素的选择需求 3. 该选择器会匹配父元素中**所有类型**的前3个子元素,若需限定元素类型应改用`:nth-of-type(-n+3)`[^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值