[小算法] 反转单链表
2012-04-18 15:32 Kevin Pan 阅读(170) 评论(0) 编辑 收藏 举报#include "stdafx.h"
#include <iostream>
using namespace std;
struct Link
{
int Data;
Link *Next;
};
void PrintLink(Link *head);
void PrintReverse(Link *node);
Link ReverseLink(Link *head);
Link RecurseReverseLink(Link *head);
int PrintReverseAt(Link *node, int lastIndex);
int _tmain(int argc, _TCHAR* argv[])
{
struct Link head, node1,node2,node3,node4;
head.Data = 0;
head.Next = &node1;
node1.Data = 1;
node1.Next = &node2;
node2.Data = 2;
node2.Next = &node3;
node3.Data = 3;
node3.Next = &node4;
node4.Data = 4;
node4.Next = NULL;
PrintLink(&head);
cout << endl;
PrintReverseAt(&head, 3);
cout << endl;
PrintReverse(head.Next);
cout << endl;
ReverseLink(&head);
PrintLink(&head);
cout << endl;
cin.get();
return 0;
}
void PrintLink(Link *head)
{
if(head == NULL) return;
Link *node = head->Next;
while(node)
{
cout << node->Data << " ";
node = node->Next;
}
}
//倒序打印链表
void PrintReverse(Link *node)
{
if(node->Next != NULL)
{
PrintReverse(node->Next);
}
cout << node->Data << " ";
}
//输出链表中倒数第N个数
int PrintReverseAt(Link *node, int lastIndex)
{
if(node->Next != NULL)
{
int currentLastIndex = PrintReverseAt(node->Next, lastIndex) + 1;
if(currentLastIndex == lastIndex)
{
cout << node->Data;
}
return currentLastIndex;
}
if(lastIndex == 0)
{
cout << node->Data;
}
return 0;
}
Link ReverseLink(Link *head)
{
if(head == NULL || head->Next == NULL || head->Next->Next == NULL) return *head;
Link *current = head->Next;
Link * next = head->Next->Next;
Link * nnext = head->Next->Next->Next;
current->Next = NULL;
while(next != NULL)
{
nnext = next->Next;
head->Next = next;
next->Next = current;
current = next;
next = nnext;
}
return *head;
}
#include <iostream>
using namespace std;
struct Link
{
int Data;
Link *Next;
};
void PrintLink(Link *head);
void PrintReverse(Link *node);
Link ReverseLink(Link *head);
Link RecurseReverseLink(Link *head);
int PrintReverseAt(Link *node, int lastIndex);
int _tmain(int argc, _TCHAR* argv[])
{
struct Link head, node1,node2,node3,node4;
head.Data = 0;
head.Next = &node1;
node1.Data = 1;
node1.Next = &node2;
node2.Data = 2;
node2.Next = &node3;
node3.Data = 3;
node3.Next = &node4;
node4.Data = 4;
node4.Next = NULL;
PrintLink(&head);
cout << endl;
PrintReverseAt(&head, 3);
cout << endl;
PrintReverse(head.Next);
cout << endl;
ReverseLink(&head);
PrintLink(&head);
cout << endl;
cin.get();
return 0;
}
void PrintLink(Link *head)
{
if(head == NULL) return;
Link *node = head->Next;
while(node)
{
cout << node->Data << " ";
node = node->Next;
}
}
//倒序打印链表
void PrintReverse(Link *node)
{
if(node->Next != NULL)
{
PrintReverse(node->Next);
}
cout << node->Data << " ";
}
//输出链表中倒数第N个数
int PrintReverseAt(Link *node, int lastIndex)
{
if(node->Next != NULL)
{
int currentLastIndex = PrintReverseAt(node->Next, lastIndex) + 1;
if(currentLastIndex == lastIndex)
{
cout << node->Data;
}
return currentLastIndex;
}
if(lastIndex == 0)
{
cout << node->Data;
}
return 0;
}
Link ReverseLink(Link *head)
{
if(head == NULL || head->Next == NULL || head->Next->Next == NULL) return *head;
Link *current = head->Next;
Link * next = head->Next->Next;
Link * nnext = head->Next->Next->Next;
current->Next = NULL;
while(next != NULL)
{
nnext = next->Next;
head->Next = next;
next->Next = current;
current = next;
next = nnext;
}
return *head;
}