c语言双链表排序交换节点6,数据结构:单向链表系列6--交换相邻两个节点1(交换数据域)...

给定一个单向链表,编写函数交换相邻 两个元素

输入: 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

通过观察发现:当输入的与元素个数是单数的时候,最后一位不参与交换。

方法1(迭代)

从头节点开始遍历列表,遍历过程中使用每个节点的下一个节点和当前节点的数据进行交换。

时间复杂度:O(n)

实现过程:

c语言

/*C Program to pairwise swap elements in a given linked list*/#include#include

/*A linked list node*/

structNode {intdata;struct Node*next;

};/*Function to swap two integers at addresses a and b*/

void swap(int *a, int *b);/*Function to pairwise swap elements of a linked list*/

void pairWiseSwap(struct Node*head)

{struct Node* temp =head;/*Traverse further only if there are at-least two nodes left*/

while( temp != NULL && temp->next !=NULL)

{/*Swap data of node with its next node's data*/swap(&temp->data, &temp->next->data);/*Move temp by 2 for the next pair*/temp= temp->next->next;

}

}/*Utility functions*/

/*function to swap two integers*/

void swap(int *a, int *b)

{inttemp;

temp= *a;*a = *b;*b =temp;

}/*Funtion to add a node at the beginning of Linked List*/

void push(struct Node** head_ref, intnew_data)

{/*allocate node*/

struct Node* new_node = (struct Node*)malloc(sizeof(structNode));/*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;

}

printf("");

}/*driver program to test above function*/

intmain() {struct Node* start =NULL;/*the constructed linked list is:

1 -> 2 -> 3 -> 4 -> 5*/push(&start ,5);

push(&start, 4);

push(&start, 3);

push(&start, 2);

push(&start, 1) ;

printf("Linked list before calling pairwise swap function");

printList(start);

pairWiseSwap(start);

printf("Linked list after calling pairwise swap function");

printList(start);return 0;

}

结果

8bfcb58b96833b4c9cceae16bde2169d.png

java代码:

//Java program to pairwise swap elements of a linked list

classLinkedList {

Node head;//head of list

/*Linked list Node*/

classNode {intdata;

Node next;

Node(intd)

{

data=d;

next= null;

}

}voidpairWiseSwap()

{

Node temp=head;/*Traverse only till there are atleast 2 nodes left*/

while (temp != null && temp.next != null) {/*Swap the data*/

int k =temp.data;

temp.data=temp.next.data;

temp.next.data=k;

temp=temp.next.next;

}

}/*Utility functions*/

/*Inserts a new Node at front of the list.*/

public void push(intnew_data)

{/*1 & 2: Allocate the Node &

Put in the data*/Node new_node= newNode(new_data);/*3. Make next of new Node as head*/new_node.next=head;/*4. Move the head to point to new Node*/head=new_node;

}/*Function to print linked list*/

voidprintList()

{

Node temp=head;while (temp != null) {

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

temp=temp.next;

}

System.out.println();

}/*Driver program to test above functions*/

public static voidmain(String args[])

{

LinkedList llist= newLinkedList();/*Created Linked List 1->2->3->4->5*/llist.push(5);

llist.push(4);

llist.push(3);

llist.push(2);

llist.push(1);

System.out.println("Linked List before calling pairWiseSwap() ");

llist.printList();

llist.pairWiseSwap();

System.out.println("Linked List after calling pairWiseSwap() ");

llist.printList();

}

}/*This code is contributed by Rajat Mishra*/

c#代码

//C# program to pairwise swap elements of a linked list

usingSystem;classLinkedList {

Node head;//head of list

/*Linked list Node*/

public classNode {public intdata;publicNode next;public Node(intd)

{

data=d;

next= null;

}

}voidpairWiseSwap()

{

Node temp=head;/*Traverse only till there are atleast 2 nodes left*/

while (temp != null && temp.next != null) {/*Swap the data*/

int k =temp.data;

temp.data=temp.next.data;

temp.next.data=k;

temp=temp.next.next;

}

}/*Utility functions*/

/*Inserts a new Node at front of the list.*/

public void push(intnew_data)

{/*1 & 2: Allocate the Node &

Put in the data*/Node new_node= newNode(new_data);/*3. Make next of new Node as head*/new_node.next=head;/*4. Move the head to point to new Node*/head=new_node;

}/*Function to print linked list*/

voidprintList()

{

Node temp=head;while (temp != null) {

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

temp=temp.next;

}

Console.WriteLine();

}/*Driver program to test above functions*/

public static voidMain(String[] args)

{

LinkedList llist= newLinkedList();/*Created Linked List 1->2->3->4->5*/llist.push(5);

llist.push(4);

llist.push(3);

llist.push(2);

llist.push(1);

Console.WriteLine("Linked List before calling pairWiseSwap()");

llist.printList();

llist.pairWiseSwap();

Console.WriteLine("Linked List after calling pairWiseSwap()");

llist.printList();

}

}//This code is contributed by Arnab Kundu

方法2(递归)

如果链表中含有两个以上节点,先交换前两个节点,然后递归调用剩下的节点

时间复杂度:O(n)

1886086879d16cf4d31a9d336da9acda.png

算法主要部分代码

/*Recursive function to pairwise swap elements

of a linked list*/

void pairWiseSwap(struct node*head)

{/*There must be at-least two nodes in the list*/

if (head != NULL && head->next !=NULL) {/*Swap the node's data with data of next node*/swap(&head->data, &head->next->data);/*Call pairWiseSwap() for rest of the list*/pairWiseSwap(head->next->next);

}

}

以上代码只是交换数据域部分的指针,如果一个节点含有很多字段,将会产生频繁的交换动作。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值