Swap kth element from the beginning and kth element from the end of linked list -- Amazon
来源:互联网 发布:贝叶斯算法 python 编辑:程序博客网 时间:2024/05/21 06:36
转载自: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 . . .
- Swap kth element from the beginning and kth element from the end of linked list -- Amazon
- Q2.2 Find the kth to last element of a singly linked list
- To find the kth to Last Element of a Singly Linked List
- P20 (*) P21 (*)Remove the Kth element from a list;Insert an element at a given position into a list.
- find the kth largest element in a list and find the first k largest elements in a list
- remove the nth node from the end of list leetcode
- P20 (*) Remove the K'th element from a list.
- Find the kth largest element in an unsorted array
- the kth largest element in an unsorted array.
- quick select to find the kth smallest element in array
- the code of Remove Nth Node From End of List
- 【Leetcode】Remove Nth Node From End of the List (lastN)
- From the very beginning
- the end ,and the beginning
- Together to the end, from a new beginning,CSDN and github
- Lintcode - kth largest element
- Kth Smallest element
- #5 Kth Largest Element
- Nt**、Zw**和Rtl** 开头的函数介绍
- 为何加索引后反而更慢
- 找到了一个减轻忧虑的思维方法
- 如何管理好电脑中的个人资料
- hadoop Map Reduce 原理(转载)
- Swap kth element from the beginning and kth element from the end of linked list -- Amazon
- H2.64中I帧和IDR帧的区别(zz)
- JAVA内存分配原理详解
- 我的桌面
- 在Sublime Text 2中设置任意扩展名文件的默认语法
- 程序员必知:平凡而又神奇的贝叶斯方法
- ubuntu下安装OpenGL并搭建OpenGL开发环境
- H264参数语法文档: SPS、PPS、IDR(zz)
- 完善商品分类导航