将单链表中的第一个节点的数字复制,然后将复制好的数字添加到最后一个节点,然后返回新的链表的最后两个节点的和

关于单链表的知识点,小编就不在这里介绍了,这次主要是用递归的方式来完成这道题目,不用循环方式。对于刚接触的递归的同学们,看这篇文章还是没关系的,主要是理解逻辑就行了。

下面是展示list.h里的代码是什么样的:

//This is the list.h file

#include<iostream>
#include<cctype>
#include<cstring>

using namespace std;

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

class list
{
    public:
        //These functions are already provided for you
        list();  //Supplied
        ~list();  //Supplied
        void build(); //Supplied
        void display();  //Supplied

        //Duplicate the first node and place it at the end of a linear linked list
        int duplicate_first();

    private:
        //Duplicate the first node and place it at the end of a linear linked list
        int duplicate_first(node * head, node *& tail, int num);

        node * head;
        node * tail;
};

因为建立单链表的时候,用到了tail指针,那么就得在运行这个程序的时候,就得需要将tail指针pass by reference。

下面是展示如何实现这两个函数:

//This is the list.cpp file

#include "list.h"

//Duplicate the first node and place it at the end of a linear linked list
int list::duplicate_first()
{
    return duplicate_first(head,tail,head->data);
}

int list::duplicate_first(node * head, node *& tail, int num)
{
    if(!head)
        return 0;
    if(!head->next)
    {
        node * temp = new node;
        temp->data = num;
        temp->next = NULL;
        head->next = temp;
        int Num = tail->data;
        tail = temp;
        return Num+num;
    }
    return duplicate_first(head->next,tail,num);
}

因为在wrapper function里面就已经将num的值传给recursive function的num这个参数,那么就可以直接用这个num参数了。在recursive function里就直接用递归的方式就做出这个功能了。

下面是展示结果的:

从这个结果中就可以看出,The resulting list 中的已经将第一个节点插入到最后一个节点的后面去,然后返回了最后两个节点的总和。

循环单链表插入节点的操作可以分为以下几步: 1. 一个待插入的节点B,并将其数据元素赋值为b。 2. 遍历循环单链表,找到第一个数据元素为a的节点A。 3. 将节点B插入到节点A之前。 4. 修改链表的头节点指针和尾节点指针,保证链表的循环性。 下面是将数据元素b插入到循环单链表第一个数据元素为a的节点之前的代码实现: ``` // 定义循环单链表节点结构体 typedef struct ListNode { int data; struct ListNode *next; } ListNode, *ListPtr; // 将数据元素b插入到循环单链表第一个数据元素为a的节点之前 void InsertNode(ListPtr *head, int a, int b) { if (*head == NULL) { // 如果链表为空,则一个节点作为头节点 ListNode *node = (ListNode*)malloc(sizeof(ListNode)); node->data = a; node->next = node; // 循环单链表需要让头节点的next指向自己 *head = node; } ListNode *p = *head; ListNode *q = (*head)->next; // q指向头节点的下一个节点,即第一个节点 while (q != *head && q->data != a) { // 寻找数据元素为a的节点 p = q; q = q->next; } if (q == *head && q->data != a) { // 如果没找到,就在末尾插入节点 ListNode *node = (ListNode*)malloc(sizeof(ListNode)); node->data = b; node->next = *head; p->next = node; } else { // 如果找到,就在其之前插入节点 ListNode *node = (ListNode*)malloc(sizeof(ListNode)); node->data = b; node->next = q; p->next = node; if (q == *head) { // 如果插入的是头节点之前,则需要更节点指针 *head = node; } } } ``` 注意,以上代码的循环单链表需要让头节点的next指向自己,这样才能保证链表的循环性。同时,插入操作可能会改变链表的头节点指针和尾节点指针,需要特别处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值