c语言i++和++i程序_使用C ++程序修改链接列表的内容

c语言i++和++i程序

Problem statement:

问题陈述:

Given a linked list, you modified that linked list in such a way that the elements of the first half of that linked list are the difference of the first node to the last node and next node is the difference of the second node to the second last node and goes on.

给定一个链表,您修改链表的方式是,链表的前半部分的元素是第一个节点与最后一个节点的差,下一个节点是第二个节点与第二个节点的差。节点并继续。

Example:

例:

    Input: 
    4 → 5 → 9 → 3 → 8 → 1 → 2 → 5
    Output: 
    -1 → 3 → 8 → -5 → 8 → 1 → 2 → 5
    
    Input:
    6 → 5 → 4 → 9 → 1 → 2 → 7
    Output:
    -1 → 3 → 3 → 9 → 1 → 2 → 7

Algorithm:

算法:

To solve the problem we can follow this algorithm:

为了解决这个问题,我们可以遵循以下算法:

  1. First, we find the midpoint of that linked list and take a copy of that.

    首先,我们找到该链表的中点并复制它。

  2. Then we divide that linked list from the middle point.

    然后,我们从中间点划分该链表。

  3. Then reverse the second part of that linked list.

    然后反转该链接列表的第二部分。

  4. Then calculate the difference of the first node of the two linked list and put the value to the original linked list.

    然后计算两个链接列表的第一个节点的差并将该值放入原始链接列表。

  5. Continue to find the difference until we go to the last node of the second element.

    继续找到差异,直到我们转到第二个元素的最后一个节点。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>

using namespace std;

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

void print(node*);

//Create a new node
struct node* create_node(int x)
{
    struct node* temp = new node;
    temp -> data = x;
    temp -> next = NULL;
    return temp;
}

//Enter the node into the linked list
void push(node** head, int x)
{
    struct node* store = create_node(x);
    if (*head == NULL) {
        *head = store;
        return;
    }
    struct node* temp = *head;
    while (temp -> next) {
        temp = temp -> next;
    }
    temp -> next = store;
}

void split_list(node* head, node** a, node** b)
{
    struct node* fast = head -> next;
    struct node* slow = head;
    while (fast != NULL && fast -> next != NULL) {
        slow = slow -> next;
        fast = fast -> next -> next;
    }
    struct node* temp = slow -> next;
    slow -> next = NULL;
    *a = head;
    *b = temp;
}

struct node* reverse(node* b)
{
    struct node* curr = b;
    struct node* next = NULL;
    struct node* prev = NULL;
    while (curr != NULL) {
        next = curr -> next;
        curr -> next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

void merge(node* a, node* b, node** c)
{
    struct node* temp = a;
    *c = create_node(0);
    struct node* curr = *c;
    while (temp && b) {
        curr -> next = create_node(temp -> data - b -> data);
        b = b -> next;
        temp = temp -> next;
        curr = curr -> next;
        //cout<<curr->data<<" ";
    }
    if (b != NULL) {
        curr -> next = b;
        curr = curr -> next;
    }
    curr -> next = reverse(a);
    *c = (*c) -> next;
}

struct node* modifyTheList(struct node* head)
{
    //add code here.
    struct node* a;
    struct node* b;
    struct node* c;
    split_list(head, &a, &b);
    struct node* temp = reverse(b);
    merge(temp, a, &c);
    return c;
}

//Print the list
void print(node* head)
{
    struct node* temp = head;
    while (temp) {
        cout << temp -> data << " ";
        temp = temp -> next;
    }
}

int main()
{
    struct node* l = NULL;
    push(&l, 1);
    push(&l, 2);
    push(&l, 3);
    push(&l, 4);
    push(&l, 5);
    push(&l, 6);
    cout << "Before the modify operation" << endl;
    print(l);
    l = modifyTheList(l);
    cout << "\nAfter the modify operation" << endl;
    print(l);

    return 0;
}

Output

输出量

Before the modify operation
1 2 3 4 5 6
After the modify operation
5 3 1 4 5 6    


翻译自: https://www.includehelp.com/cpp-programs/modify-contents-of-linked-list.aspx

c语言i++和++i程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值