当前位置: 首页 > news >正文

吉林省建设部网站长尾关键词挖掘精灵官网

吉林省建设部网站,长尾关键词挖掘精灵官网,wordpress 搜索文章,网站建设哪些好给定一个单链表, 编写一个函数以成对交换元素。例如, 如果链接列表是1-> 2-> 3-> 4-> 5-> 6-> 7, 则函数应将其更改为2-> 1-> 4-> 3-> 6-> 5 -> 7, 并且如果链接列表是1-> 2-> 3-> 4-> 5-> 6, 则该函数应将其更改为2-> …

给定一个单链表, 编写一个函数以成对交换元素。例如, 如果链接列表是1-> 2-> 3-> 4-> 5-> 6-> 7, 则函数应将其更改为2-> 1-> 4-> 3-> 6-> 5 -> 7, 并且如果链接列表是1-> 2-> 3-> 4-> 5-> 6, 则该函数应将其更改为2-> 1-> 4-> 3-> 6-> 5

这个问题已经在lsbin((https://www.lsbin.com/tag/%e7%ae%97%e6%b3%95%e9%a2%98/)中讨论过了,那里提供的解决方案交换节点数据。如果数据包含许多字段, 将有许多交换操作。因此, 通常更改链接是一个更好的主意。以下是一个C实现, 它更改了链接而不是交换数据。

C ++/* This program swaps the nodes of

linked list rather than swapping the

field from the nodes.

Imagine a case where a node contains

many fields, there will be plenty

of unnecessary swap calls. */

#include

using namespace std;

/* A linked list node */

class node {

public :

int data;

node* next;

};

/* Function to pairwise swap elements

of a linked list. It returns head of

the modified list, so return value

of this node must be assigned */

node* pairWiseSwap(node* head)

{

// If linked list is empty or

// there is only one node in list

if (head == NULL || head->next == NULL)

return head;

// Initialize previous and current pointers

node* prev = head;

node* curr = head->next;

head = curr; // Change head before proceeeding

// Traverse the list

while ( true ) {

node* next = curr->next;

curr->next = prev; // Change next of

// current as previous node

// If next NULL or next is the last node

if (next == NULL || next->next == NULL) {

prev->next = next;

break ;

}

// Change next of previous to next next

prev->next = next->next;

// Update previous and curr

prev = next;

curr = prev->next;

}

return head;

}

/* Function to add a node at

the begining of Linked List */

void push(node** head_ref, int new_data)

{

/* allocate node */

node* new_node = new node();

/* put in the data */

new_node->data = new_data;

/* link the old list off the new node */

new_node->next = (*head_ref);

/* move the head to point to the new node */

(*head_ref) = new_node;

}

/* Function to print nodes

in a given linked list */

void printList(node* node)

{

while (node != NULL) {

cout << node->data << " " ;

node = node->next;

}

}

/* Driver code */

int main()

{

node* start = NULL;

/* The constructed linked list is:

1->2->3->4->5->6->7 */

push(&start, 7);

push(&start, 6);

push(&start, 5);

push(&start, 4);

push(&start, 3);

push(&start, 2);

push(&start, 1);

cout << "Linked list before "

<< "calling pairWiseSwap() " ;

printList(start);

start = pairWiseSwap(start); // NOTE THIS CHANGE

cout << "nLinked list after calling"

<< "pairWiseSwap() " ;

printList(start);

return 0;

}

// This code is contributed by Manoj N

C语言实现/* This program swaps the nodes of linked list rather than swapping the

field from the nodes.

Imagine a case where a node contains many fields, there will be plenty

of unnecessary swap calls. */

#include

#include

#include

/* A linked list node */

struct Node {

int data;

struct Node* next;

};

/* Function to pairwise swap elements of a linked list */

void pairWiseSwap( struct Node** head)

{

// If linked list is empty or there is only one node in list

if (*head == NULL || (*head)->next == NULL)

return ;

// Initialize previous and current pointers

struct Node* prev = *head;

struct Node* curr = (*head)->next;

*head = curr; // Change head before proceeeding

// Traverse the list

while ( true ) {

struct Node* next = curr->next;

curr->next = prev; // Change next of current as previous node

// If next NULL or next is the last node

if (next == NULL || next->next == NULL) {

prev->next = next;

break ;

}

// Change next of previous to next next

prev->next = next->next;

// Update previous and curr

prev = next;

curr = prev->next;

}

}

/* Function to add a node at the begining of Linked List */

void push( struct Node** head_ref, int new_data)

{

/* allocate node */

struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));

/* put in the data */

new_node->data = new_data;

/* link the old list off the new node */

new_node->next = (*head_ref);

/* move the head to point to the new node */

(*head_ref) = new_node;

}

/* Function to print nodes in a given linked list */

void printList( struct Node* node)

{

while (node != NULL) {

printf ( "%d " , node->data);

node = node->next;

}

}

/* Druver program to test above function */

int main()

{

struct Node* start = NULL;

/* The constructed linked list is:

1->2->3->4->5->6->7 */

push(&start, 7);

push(&start, 6);

push(&start, 5);

push(&start, 4);

push(&start, 3);

push(&start, 2);

push(&start, 1);

printf ( "n Linked list before calling pairWiseSwap() " );

printList(start);

pairWiseSwap(&start);

printf ( "n Linked list after calling pairWiseSwap() " );

printList(start);

getchar ();

return 0;

}

Java// Java program to swap elements of linked list by changing links

class LinkedList {

static Node head;

static class Node {

int data;

Node next;

Node( int d)

{

data = d;

next = null ;

}

}

/* Function to pairwise swap elements of a linked list */

Node pairWiseSwap(Node node)

{

// If linked list is empty or there is only one node in list

if (node == null || node.next == null ) {

return node;

}

// Initialize previous and current pointers

Node prev = node;

Node curr = node.next;

node = curr; // Change head before proceeeding

// Traverse the list

while ( true ) {

Node next = curr.next;

curr.next = prev; // Change next of current as previous node

// If next NULL or next is the last node

if (next == null || next.next == null ) {

prev.next = next;

break ;

}

// Change next of previous to next next

prev.next = next.next;

// Update previous and curr

prev = next;

curr = prev.next;

}

return node;

}

/* Function to print nodes in a given linked list */

void printList(Node node)

{

while (node != null ) {

System.out.print(node.data + " " );

node = node.next;

}

}

// Driver program to test above functions

public static void main(String[] args)

{

/* The constructed linked list is:

1->2->3->4->5->6->7 */

LinkedList list = new LinkedList();

list.head = new Node( 1 );

list.head.next = new Node( 2 );

list.head.next.next = new Node( 3 );

list.head.next.next.next = new Node( 4 );

list.head.next.next.next.next = new Node( 5 );

list.head.next.next.next.next.next = new Node( 6 );

list.head.next.next.next.next.next.next = new Node( 7 );

System.out.println( "Linked list before calling pairwiseSwap() " );

list.printList(head);

Node st = list.pairWiseSwap(head);

System.out.println( "" );

System.out.println( "Linked list after calling pairwiseSwap() " );

list.printList(st);

System.out.println( "" );

}

}

// This code has been contributed by Mayank Jaiswal

C// C# program to swap elements of

// linked list by changing links

using System;

public class LinkedList {

Node head;

public class Node {

public int data;

public Node next;

public Node( int d)

{

data = d;

next = null ;

}

}

/* Function to pairwise swap

elements of a linked list */

Node pairWiseSwap(Node node)

{

// If linked list is empty or there

// is only one node in list

if (node == null || node.next == null ) {

return node;

}

// Initialize previous and current pointers

Node prev = node;

Node curr = node.next;

// Change head before proceeeding

node = curr;

// Traverse the list

while ( true ) {

Node next = curr.next;

// Change next of current as previous node

curr.next = prev;

// If next NULL or next is the last node

if (next == null || next.next == null ) {

prev.next = next;

break ;

}

// Change next of previous to next next

prev.next = next.next;

// Update previous and curr

prev = next;

curr = prev.next;

}

return node;

}

/* Function to print nodes

in a given linked list */

void printList(Node node)

{

while (node != null ) {

Console.Write(node.data + " " );

node = node.next;

}

}

// Driver code

public static void Main(String[] args)

{

/* The constructed linked list is:

1->2->3->4->5->6->7 */

LinkedList list = new LinkedList();

list.head = new Node(1);

list.head.next = new Node(2);

list.head.next.next = new Node(3);

list.head.next.next.next = new Node(4);

list.head.next.next.next.next = new Node(5);

list.head.next.next.next.next.next = new Node(6);

list.head.next.next.next.next.next.next = new Node(7);

Console.WriteLine( "Linked list before calling pairwiseSwap() " );

list.printList(list.head);

Node st = list.pairWiseSwap(list.head);

Console.WriteLine( "" );

Console.WriteLine( "Linked list after calling pairwiseSwap() " );

list.printList(st);

Console.WriteLine( "" );

}

}

// This code contributed by Rajput-Ji

输出如下:Linked list before calling pairWiseSwap() 1 2 3 4 5 6 7

Linked list after calling pairWiseSwap() 2 1 4 3 6 5 7

时间复杂度:上面程序的时间复杂度为O(n), 其中n是给定链表中节点的数量。 while循环遍历给定的链表。

以下是递归实现,同样的方法,我们更改前两个节点, 然后重复其余列表。感谢geek和omer salem提出了这种方法。

C++/* This program swaps the nodes of linked list rather than swapping the

field from the nodes.

Imagine a case where a node contains many fields, there will be plenty

of unnecessary swap calls. */

#include

using namespace std;

/* A linked list node */

class node {

public :

int data;

node* next;

};

/* Function to pairwise swap elements of a linked list.

It returns head of the modified list, so return value

of this node must be assigned */

node* pairWiseSwap(node* head)

{

// Base Case: The list is empty or has only one node

if (head == NULL || head->next == NULL)

return head;

// Store head of list after two nodes

node* remaing = head->next->next;

// Change head

node* newhead = head->next;

// Change next of second node

head->next->next = head;

// Recur for remaining list and change next of head

head->next = pairWiseSwap(remaing);

// Return new head of modified list

return newhead;

}

/* Function to add a node at the begining of Linked List */

void push(node** head_ref, int new_data)

{

/* allocate node */

node* new_node = new node();

/* put in the data */

new_node->data = new_data;

/* link the old list off the new node */

new_node->next = (*head_ref);

/* move the head to point to the new node */

(*head_ref) = new_node;

}

/* Function to print nodes in a given linked list */

void printList(node* node)

{

while (node != NULL) {

cout << node->data << " " ;

node = node->next;

}

}

/* Druver program to test above function */

int main()

{

node* start = NULL;

/* The constructed linked list is:

1->2->3->4->5->6->7 */

push(&start, 7);

push(&start, 6);

push(&start, 5);

push(&start, 4);

push(&start, 3);

push(&start, 2);

push(&start, 1);

cout << "Linked list before calling pairWiseSwap() " ;

printList(start);

start = pairWiseSwap(start); // NOTE THIS CHANGE

cout << "nLinked list after calling pairWiseSwap() " ;

printList(start);

return 0;

}

// This is code is contributed by rathbhupendra

C语言实现/* This program swaps the nodes of linked list rather than swapping the

field from the nodes.

Imagine a case where a node contains many fields, there will be plenty

of unnecessary swap calls. */

#include

#include

#include

/* A linked list node */

struct node {

int data;

struct node* next;

};

/* Function to pairwise swap elements of a linked list.

It returns head of the modified list, so return value

of this node must be assigned */

struct node* pairWiseSwap( struct node* head)

{

// Base Case: The list is empty or has only one node

if (head == NULL || head->next == NULL)

return head;

// Store head of list after two nodes

struct node* remaing = head->next->next;

// Change head

struct node* newhead = head->next;

// Change next of second node

head->next->next = head;

// Recur for remaining list and change next of head

head->next = pairWiseSwap(remaing);

// Return new head of modified list

return newhead;

}

/* Function to add a node at the begining of Linked List */

void push( struct node** head_ref, int new_data)

{

/* allocate node */

struct node* new_node = ( struct node*) malloc ( sizeof ( struct node));

/* put in the data */

new_node->data = new_data;

/* link the old list off the new node */

new_node->next = (*head_ref);

/* move the head to point to the new node */

(*head_ref) = new_node;

}

/* Function to print nodes in a given linked list */

void printList( struct node* node)

{

while (node != NULL) {

printf ( "%d " , node->data);

node = node->next;

}

}

/* Druver program to test above function */

int main()

{

struct node* start = NULL;

/* The constructed linked list is:

1->2->3->4->5->6->7 */

push(&start, 7);

push(&start, 6);

push(&start, 5);

push(&start, 4);

push(&start, 3);

push(&start, 2);

push(&start, 1);

printf ( "n Linked list before calling pairWiseSwap() " );

printList(start);

start = pairWiseSwap(start); // NOTE THIS CHANGE

printf ( "n Linked list after calling pairWiseSwap() " );

printList(start);

return 0;

}

Java// Java program to swap elements of linked list by changing links

class LinkedList {

static Node head;

static class Node {

int data;

Node next;

Node( int d)

{

data = d;

next = null ;

}

}

/* Function to pairwise swap elements of a linked list.

It returns head of the modified list, so return value

of this node must be assigned */

Node pairWiseSwap(Node node)

{

// Base Case: The list is empty or has only one node

if (node == null || node.next == null ) {

return node;

}

// Store head of list after two nodes

Node remaing = node.next.next;

// Change head

Node newhead = node.next;

// Change next of second node

node.next.next = node;

// Recur for remaining list and change next of head

node.next = pairWiseSwap(remaing);

// Return new head of modified list

return newhead;

}

/* Function to print nodes in a given linked list */

void printList(Node node)

{

while (node != null ) {

System.out.print(node.data + " " );

node = node.next;

}

}

// Driver program to test above functions

public static void main(String[] args)

{

/* The constructed linked list is:

1->2->3->4->5->6->7 */

LinkedList list = new LinkedList();

list.head = new Node( 1 );

list.head.next = new Node( 2 );

list.head.next.next = new Node( 3 );

list.head.next.next.next = new Node( 4 );

list.head.next.next.next.next = new Node( 5 );

list.head.next.next.next.next.next = new Node( 6 );

list.head.next.next.next.next.next.next = new Node( 7 );

System.out.println( "Linked list before calling pairwiseSwap() " );

list.printList(head);

head = list.pairWiseSwap(head);

System.out.println( "" );

System.out.println( "Linked list after calling pairwiseSwap() " );

list.printList(head);

System.out.println( "" );

}

}

C// C# program to swap elements

// of linked list by changing links

using System;

public class LinkedList {

Node head;

class Node {

public int data;

public Node next;

public Node( int d)

{

data = d;

next = null ;

}

}

/* Function to pairwise swap

elements of a linked list.

It returns head of the modified

list, so return value of this

node must be assigned */

Node pairWiseSwap(Node node)

{

// Base Case: The list is empty

// or has only one node

if (node == null || node.next == null ) {

return node;

}

// Store head of list after two nodes

Node remaing = node.next.next;

// Change head

Node newhead = node.next;

// Change next of second node

node.next.next = node;

// Recur for remaining list

// and change next of head

node.next = pairWiseSwap(remaing);

// Return new head of modified list

return newhead;

}

/* Function to print nodes in a given linked list */

void printList(Node node)

{

while (node != null ) {

Console.Write(node.data + " " );

node = node.next;

}

}

// Driver program to test above functions

public static void Main()

{

/* The constructed linked list is:

1->2->3->4->5->6->7 */

LinkedList list = new LinkedList();

list.head = new Node(1);

list.head.next = new Node(2);

list.head.next.next = new Node(3);

list.head.next.next.next = new Node(4);

list.head.next.next.next.next = new Node(5);

list.head.next.next.next.next.next = new Node(6);

list.head.next.next.next.next.next.next = new Node(7);

Console.WriteLine( "Linked list before calling pairwiseSwap() " );

list.printList(list.head);

list.head = list.pairWiseSwap(list.head);

Console.WriteLine( "" );

Console.WriteLine( "Linked list after calling pairwiseSwap() " );

list.printList(list.head);

Console.WriteLine( "" );

}

}

// This code is contributed by PrinciRaj1992

输出如下:Linked list before calling pairWiseSwap() 1 2 3 4 5 6 7

Linked list after calling pairWiseSwap() 2 1 4 3 6 5 7

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请发表评论。

查看以下更多算法相关的内容:

http://www.lbrq.cn/news/2457379.html

相关文章:

  • 移动端网站案例谷歌广告
  • 绍兴网站建设制作百度指数行业排行
  • 纪委网站建设外贸建站与推广如何做
  • 网站的ftp别的公司会给么软文模板app
  • 简述可口可乐公司的企业网站建设seo是什么字
  • 百度网站权重it培训机构排名
  • 做百度网站浙江seo推广
  • 个人主机做网站代运营公司排行榜
  • 网站建设微信公众号小程序制作高端网站建设专业公司
  • 轻松做网站自己如何做链接推广
  • 手机开发者选项开启的好还是关闭的好天津seo渠道代理
  • 广州网站排名优化费用网站的营销策略
  • 江苏省建设厅网站公示河北seo
  • 网页制作成app哈尔滨seo关键词排名
  • 厦门网站建设报价seo网站推广专员
  • 大连专业手机自适应网站建设百度seo招聘
  • 网站上的报名表链接是怎么做的链接生成器在线制作
  • 宣传 网站建设方案廊坊seo优化排名
  • 京东商城网站设计网络安全培训
  • 建站宝盒做的网站互联网推广项目
  • 网站建设要花在哪些项目上网络整合营销理论案例
  • 网站恶意做评论万能软文模板
  • 易点科技网站建设本地推广最好用的平台
  • dw怎么做网站跳转今日足球赛事数据
  • 云南网站优化排名网络软文营销
  • 如何添加网站白名单今日新闻最新头条10条摘抄
  • 在手机上做网站是什么软件同城推广引流平台
  • 大学生做外包项目的网站seo排名软件免费
  • 摄影培训网站建设百度推广电话是多少
  • 第一个做装修的网站网络营销有哪些就业岗位
  • 图片查重从设计到实现(3)图片存储MinIO 应用介绍及 Docker 环境下的安装部署
  • MySQL 表的操作
  • C# 实现:动态规划解决 0/1 背包问题
  • 使用Langchain调用模型上下文协议 (MCP)服务
  • API获取及调用(以豆包为例实现图像分析)
  • UE5多人MOBA+GAS 番外篇:移植Lyra的伤害特效(没用GameplayCue,因为我失败了┭┮﹏┭┮)