转载自:https://sites.google.com/site/spaceofjameschen/home
Problem
Swap kth element from the beginning and kth element from the end of linked list.
Solution
/*
============================================================================
Author : James Chen
Email : a.james.chen@gmail.com
Description : Swap kth element from the beginning and kth element from the end of linked list.
Created Data : 22-06-2013
Last Modified :
============================================================================
*/
#include <iostream>
#include <iomanip>
using namespace std;
typedef struct linked_node{
int data;
struct linked_node *next;
}LinkedNode;
bool AddNode(LinkedNode** head, int data)
{
LinkedNode *l = new LinkedNode;
if(l == NULL) return false;
l->data = data;
l->next = NULL;
if(*head == NULL){
*head = l;
return true;
}
LinkedNode* t = *head;
while(t->next != NULL){
t = t->next;
}
t->next = l;
return true;
}
void DeleteList(LinkedNode *head)
{
while(head != NULL){
LinkedNode *t = head;
head = head->next;
delete t;
}
}
void PrintList(LinkedNode *head)
{
while(head != NULL){
cout << setw(4) << head->data;
head = head->next;
}
cout << endl;
}
void InitializeList(LinkedNode *head)
{
int i = 0;
while(head != NULL){
head->data = i++;
head = head->next;
}
}
bool SwapKthNode(LinkedNode *head, int k)
{
if(k < 1){
cout << " k must be great than 0" << endl;
return false;
}
LinkedNode *kthStart = NULL;
LinkedNode *kthEnd = head;
int index = 0;
while(head != NULL){
index ++;
if(index == k){
kthStart = head;
}
else if(index > k){
kthEnd = kthEnd->next;
}
head = head->next;
}
if(index < k || kthStart == NULL){
cout << "List length is less than " << k;
cout << ". cannot swap 2 kth elements" << endl;
return false;
}
else{
int t = kthStart->data;
kthStart->data = kthEnd->data;
kthEnd->data = t;
return true;
}
}
void DoTest(LinkedNode *head, int k)
{
cout << "before swapping 2 " << k << "th elements" << endl;
PrintList(head);
if(SwapKthNode(head, k)){
cout << "after swapping 2 " << k << "th elements" << endl;
PrintList(head);
}
}
int main(int argc, char* argv[])
{
for(int i = 1; i < 10; ++i){
LinkedNode* head = NULL;
for(int j = 0; j < i; ++j){
AddNode(&head, j);
}
DoTest(head, 0);
cout << "--------------------" << endl;
InitializeList(head);
DoTest(head, i / 2);
cout << "--------------------" << endl;
InitializeList(head);
DoTest(head, i);
cout << "--------------------" << endl;
InitializeList(head);
DoTest(head, i + 2);
cout << "--------------------" << endl;
DeleteList(head);
cout << "=========================================" << endl;
}
return 0;
}
Output
before swapping 2 0th elements
0
k must be great than 0
--------------------
before swapping 2 0th elements
0
k must be great than 0
--------------------
before swapping 2 1th elements
0
after swapping 2 1th elements
0
--------------------
before swapping 2 3th elements
0
List length is less than 3. cannot swap 2 kth elements
--------------------
=========================================
before swapping 2 0th elements
0 1
k must be great than 0
--------------------
before swapping 2 1th elements
0 1
after swapping 2 1th elements
1 0
--------------------
before swapping 2 2th elements
0 1
after swapping 2 2th elements
1 0
--------------------
before swapping 2 4th elements
0 1
List length is less than 4. cannot swap 2 kth elements
--------------------
=========================================
before swapping 2 0th elements
0 1 2
k must be great than 0
--------------------
before swapping 2 1th elements
0 1 2
after swapping 2 1th elements
2 1 0
--------------------
before swapping 2 3th elements
0 1 2
after swapping 2 3th elements
2 1 0
--------------------
before swapping 2 5th elements
0 1 2
List length is less than 5. cannot swap 2 kth elements
--------------------
=========================================
before swapping 2 0th elements
0 1 2 3
k must be great than 0
--------------------
before swapping 2 2th elements
0 1 2 3
after swapping 2 2th elements
0 2 1 3
--------------------
before swapping 2 4th elements
0 1 2 3
after swapping 2 4th elements
3 1 2 0
--------------------
before swapping 2 6th elements
0 1 2 3
List length is less than 6. cannot swap 2 kth elements
--------------------
=========================================
before swapping 2 0th elements
0 1 2 3 4
k must be great than 0
--------------------
before swapping 2 2th elements
0 1 2 3 4
after swapping 2 2th elements
0 3 2 1 4
--------------------
before swapping 2 5th elements
0 1 2 3 4
after swapping 2 5th elements
4 1 2 3 0
--------------------
before swapping 2 7th elements
0 1 2 3 4
List length is less than 7. cannot swap 2 kth elements
--------------------
=========================================
before swapping 2 0th elements
0 1 2 3 4 5
k must be great than 0
--------------------
before swapping 2 3th elements
0 1 2 3 4 5
after swapping 2 3th elements
0 1 3 2 4 5
--------------------
before swapping 2 6th elements
0 1 2 3 4 5
after swapping 2 6th elements
5 1 2 3 4 0
--------------------
before swapping 2 8th elements
0 1 2 3 4 5
List length is less than 8. cannot swap 2 kth elements
--------------------
=========================================
before swapping 2 0th elements
0 1 2 3 4 5 6
k must be great than 0
--------------------
before swapping 2 3th elements
0 1 2 3 4 5 6
after swapping 2 3th elements
0 1 4 3 2 5 6
--------------------
before swapping 2 7th elements
0 1 2 3 4 5 6
after swapping 2 7th elements
6 1 2 3 4 5 0
--------------------
before swapping 2 9th elements
0 1 2 3 4 5 6
List length is less than 9. cannot swap 2 kth elements
--------------------
=========================================
before swapping 2 0th elements
0 1 2 3 4 5 6 7
k must be great than 0
--------------------
before swapping 2 4th elements
0 1 2 3 4 5 6 7
after swapping 2 4th elements
0 1 2 4 3 5 6 7
--------------------
before swapping 2 8th elements
0 1 2 3 4 5 6 7
after swapping 2 8th elements
7 1 2 3 4 5 6 0
--------------------
before swapping 2 10th elements
0 1 2 3 4 5 6 7
List length is less than 10. cannot swap 2 kth elements
--------------------
=========================================
before swapping 2 0th elements
0 1 2 3 4 5 6 7 8
k must be great than 0
--------------------
before swapping 2 4th elements
0 1 2 3 4 5 6 7 8
after swapping 2 4th elements
0 1 2 5 4 3 6 7 8
--------------------
before swapping 2 9th elements
0 1 2 3 4 5 6 7 8
after swapping 2 9th elements
8 1 2 3 4 5 6 7 0
--------------------
before swapping 2 11th elements
0 1 2 3 4 5 6 7 8
List length is less than 11. cannot swap 2 kth elements
--------------------
=========================================
Press any key to continue . . .