[小算法] 交换链表中的两个节点
2012-04-24 14:16 Kevin Pan 阅读(332) 评论(0) 编辑 收藏 举报算法代码:
// SwitchLinkNodes.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "fstream"
using namespace std;
ifstream fin("data.txt");
struct Link
{
int Data;
Link *Next;
};
void SwitchLinkNodes(Link *head, Link *nodeA, Link *nodeB);
Link* BuildLink();
void PrintLink(Link *head);
Link* GetNodeAtIndex(Link *head,int index);
int _tmain(int argc, _TCHAR* argv[])
{
Link *head = BuildLink();
PrintLink(head);
cout << endl;
Link *nodeA = GetNodeAtIndex(head, 3);
Link *nodeB = GetNodeAtIndex(head, 5);
SwitchLinkNodes(head, nodeA, nodeB);
PrintLink(head);
cout << endl;
system("pause");
return 0;
}
void SwitchLinkNodes(Link *head, Link *nodeA, Link *nodeB)
{
if(nodeA == NULL || nodeB == NULL || head == NULL || head->Next == NULL)
return;
Link *nodeAPre, *nodeBPre, *pointer;
pointer = head;
//Get the previous nodes which will switch
while(pointer)
{
if(pointer->Next == nodeA)
{
nodeAPre = pointer;
}
if(pointer->Next == nodeB)
{
nodeBPre = pointer;
}
pointer = pointer->Next;
}
nodeAPre->Next = nodeB; //1
nodeBPre->Next = nodeA; //2
pointer = nodeB->Next; // 3.temp node
nodeB->Next = nodeA->Next; //4
nodeA->Next = pointer; //5
}
//Build link with the data in file data.txt
Link* BuildLink()
{
Link * head = new Link();
int data = -1;
Link *newNode, *pointer;
pointer = head;
while(fin >> data)
{
newNode = new Link();
newNode->Data = data;
newNode ->Next = NULL;
pointer->Next = newNode;
pointer = newNode;
}
return head;
}
void PrintLink(Link *head)
{
Link *pointer = head->Next;
while(pointer != NULL)
{
cout << pointer->Data << " ";
pointer = pointer->Next;
}
}
//zero based index
Link* GetNodeAtIndex(Link *head,int index)
{
int i = 0;
Link *pointer = head->Next;
for(i = 0;i < index;i++)
{
if(pointer != NULL)
{
pointer = pointer->Next;
}
else
{
return NULL;
}
}
return pointer;
}
//
#include "stdafx.h"
#include "iostream"
#include "fstream"
using namespace std;
ifstream fin("data.txt");
struct Link
{
int Data;
Link *Next;
};
void SwitchLinkNodes(Link *head, Link *nodeA, Link *nodeB);
Link* BuildLink();
void PrintLink(Link *head);
Link* GetNodeAtIndex(Link *head,int index);
int _tmain(int argc, _TCHAR* argv[])
{
Link *head = BuildLink();
PrintLink(head);
cout << endl;
Link *nodeA = GetNodeAtIndex(head, 3);
Link *nodeB = GetNodeAtIndex(head, 5);
SwitchLinkNodes(head, nodeA, nodeB);
PrintLink(head);
cout << endl;
system("pause");
return 0;
}
void SwitchLinkNodes(Link *head, Link *nodeA, Link *nodeB)
{
if(nodeA == NULL || nodeB == NULL || head == NULL || head->Next == NULL)
return;
Link *nodeAPre, *nodeBPre, *pointer;
pointer = head;
//Get the previous nodes which will switch
while(pointer)
{
if(pointer->Next == nodeA)
{
nodeAPre = pointer;
}
if(pointer->Next == nodeB)
{
nodeBPre = pointer;
}
pointer = pointer->Next;
}
nodeAPre->Next = nodeB; //1
nodeBPre->Next = nodeA; //2
pointer = nodeB->Next; // 3.temp node
nodeB->Next = nodeA->Next; //4
nodeA->Next = pointer; //5
}
//Build link with the data in file data.txt
Link* BuildLink()
{
Link * head = new Link();
int data = -1;
Link *newNode, *pointer;
pointer = head;
while(fin >> data)
{
newNode = new Link();
newNode->Data = data;
newNode ->Next = NULL;
pointer->Next = newNode;
pointer = newNode;
}
return head;
}
void PrintLink(Link *head)
{
Link *pointer = head->Next;
while(pointer != NULL)
{
cout << pointer->Data << " ";
pointer = pointer->Next;
}
}
//zero based index
Link* GetNodeAtIndex(Link *head,int index)
{
int i = 0;
Link *pointer = head->Next;
for(i = 0;i < index;i++)
{
if(pointer != NULL)
{
pointer = pointer->Next;
}
else
{
return NULL;
}
}
return pointer;
}
data.txt 文件内容如下:
2 3 6 8 9 12 7 45 56 66 4
程序运行结果:
2 3 6 8 9 12 7 45 56 66 4
2 3 6 12 9 8 7 45 56 66 4
Press any key to continue . . .