编程练习- 链表题目-反序,合并
2009-11-05 22:30
357 查看
Code Snippet #include <iostream>
#include<fstream>
using namespace std;
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
void ReverseList(Node*& head)
{
if(head == NULL || head->next == NULL)
return;
Node* currentNode = head;
Node* previousNode = NULL;
while(currentNode != NULL)
...{
Node* temp = currentNode->next;
currentNode->next = previousNode;
previousNode = currentNode;
currentNode = temp;
}
head = previousNode;
}
// My answer.
Node* MergeLinkList(Node* listA, Node* listB)
{
if(listA == NULL)
return listB;
if(listB == NULL)
return listA;
Node* pHeadSmall = listA -> data < listB -> data ? listA : listB;
Node* pHeadBig = pHeadSmall == listA ? listB : listA;
Node* p = pHeadSmall;
Node* q = pHeadSmall->next;
Node* r = pHeadBig;
while(q != NULL && r != NULL)
...{
if(q->data <= r->data)
...{
p->next = q;
p = p->next;
q = q->next;
}
else
...{
p->next = r;
p = p->next;
r = r->next;
}
}
if(q == NULL)
{
p->next = r;
}
else
{
p->next = q;
}
return pHeadSmall;
}
// Standard answer
Node * Merge(Node *head1 , Node *head2)
{
if ( head1 == NULL)
return head2 ;
if ( head2 == NULL)
return head1 ;
Node *head = NULL ;
Node *p1 = NULL;
Node *p2 = NULL;
if ( head1->data < head2->data )
{
head = head1 ;
p1 = head1->next;
p2 = head2 ;
}
else
{
head = head2 ;
p2 = head2->next ;
p1 = head1 ;
}
Node *pcurrent = head ;
while ( p1 != NULL && p2 != NULL)
{
if ( p1->data <= p2->data )
{
pcurrent->next = p1 ;
pcurrent = p1 ;
p1 = p1->next ;
}
else
{
pcurrent->next = p2 ;
pcurrent = p2 ;
p2 = p2->next ;
}
}
if ( p1 != NULL )
pcurrent->next = p1 ;
if ( p2 != NULL )
pcurrent->next = p2 ;
return head ;
}
// Utillity function.
void TestList(Node* head)
{
Node* p = head;
while(p != NULL)
{
cout << p->data << endl;;
p= p->next;
}
}
// Utillity function.
Node* InputList(ifstream& inFile)
{
int len;
inFile >> len;
Node* head = new Node[len];
Node* p = head;
for(int i = 0; i < len - 1; i++)...{
inFile >> p->data;
p->next = p + 1;
p = p->next;
}
inFile >> p->data;
p->next = NULL;
return head;
}
int main()
{
ifstream inFile("d:\data.txt");
if(!inFile)
return -1;
Node* headA = InputList(inFile);
Node* headB = InputList(inFile);
TestList(headA);
TestList(headB);
ReverseList(headA);
cout<< "Testing Reverse A" << endl;
TestList(headA);
ReverseList(headA);
cout<< "Testing Reverse A back." << endl;
TestList(headA);
Node* newHead = Merge(headA, headB);
cout<< "Testing Merged List." << endl;
TestList(newHead);
while(newHead != NULL)
{
Node* ptr = newHead;
newHead = newHead->next;
delete ptr;
}
}
#include<fstream>
using namespace std;
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
void ReverseList(Node*& head)
{
if(head == NULL || head->next == NULL)
return;
Node* currentNode = head;
Node* previousNode = NULL;
while(currentNode != NULL)
...{
Node* temp = currentNode->next;
currentNode->next = previousNode;
previousNode = currentNode;
currentNode = temp;
}
head = previousNode;
}
// My answer.
Node* MergeLinkList(Node* listA, Node* listB)
{
if(listA == NULL)
return listB;
if(listB == NULL)
return listA;
Node* pHeadSmall = listA -> data < listB -> data ? listA : listB;
Node* pHeadBig = pHeadSmall == listA ? listB : listA;
Node* p = pHeadSmall;
Node* q = pHeadSmall->next;
Node* r = pHeadBig;
while(q != NULL && r != NULL)
...{
if(q->data <= r->data)
...{
p->next = q;
p = p->next;
q = q->next;
}
else
...{
p->next = r;
p = p->next;
r = r->next;
}
}
if(q == NULL)
{
p->next = r;
}
else
{
p->next = q;
}
return pHeadSmall;
}
// Standard answer
Node * Merge(Node *head1 , Node *head2)
{
if ( head1 == NULL)
return head2 ;
if ( head2 == NULL)
return head1 ;
Node *head = NULL ;
Node *p1 = NULL;
Node *p2 = NULL;
if ( head1->data < head2->data )
{
head = head1 ;
p1 = head1->next;
p2 = head2 ;
}
else
{
head = head2 ;
p2 = head2->next ;
p1 = head1 ;
}
Node *pcurrent = head ;
while ( p1 != NULL && p2 != NULL)
{
if ( p1->data <= p2->data )
{
pcurrent->next = p1 ;
pcurrent = p1 ;
p1 = p1->next ;
}
else
{
pcurrent->next = p2 ;
pcurrent = p2 ;
p2 = p2->next ;
}
}
if ( p1 != NULL )
pcurrent->next = p1 ;
if ( p2 != NULL )
pcurrent->next = p2 ;
return head ;
}
// Utillity function.
void TestList(Node* head)
{
Node* p = head;
while(p != NULL)
{
cout << p->data << endl;;
p= p->next;
}
}
// Utillity function.
Node* InputList(ifstream& inFile)
{
int len;
inFile >> len;
Node* head = new Node[len];
Node* p = head;
for(int i = 0; i < len - 1; i++)...{
inFile >> p->data;
p->next = p + 1;
p = p->next;
}
inFile >> p->data;
p->next = NULL;
return head;
}
int main()
{
ifstream inFile("d:\data.txt");
if(!inFile)
return -1;
Node* headA = InputList(inFile);
Node* headB = InputList(inFile);
TestList(headA);
TestList(headB);
ReverseList(headA);
cout<< "Testing Reverse A" << endl;
TestList(headA);
ReverseList(headA);
cout<< "Testing Reverse A back." << endl;
TestList(headA);
Node* newHead = Merge(headA, headB);
cout<< "Testing Merged List." << endl;
TestList(newHead);
while(newHead != NULL)
{
Node* ptr = newHead;
newHead = newHead->next;
delete ptr;
}
}
相关文章推荐
- PAT练习基础编程题目之求单链表结点的阶乘和
- 【剑指Offer面试编程题】题目1519:合并两个排序的链表--九度OJ
- 【剑指Offer面试编程题】题目1519:合并两个排序的链表--九度OJ
- 【编程题目】合并两个排序链表(C++实现)
- 已知两个链表 la和 lb,其元素值递增排序。编程将la和lb合并成一个递减有序(相同值元素只保留一个)的链表lc。(北方名校经典试题) 本题选做
- 编程题目练习-翻牌
- “用最小堆将k个已排序链表合并为一个排序链表”(算法导论 练习6.5-9)
- 【剑指Offer面试编程题】题目1518:反转链表--九度OJ
- PAT练习基础编程题目之统计个位数字
- 【编程题目】编程判断俩个链表是否相交 ☆
- PAT练习基础编程题目之简单求和
- 将两个非递减的有序链表合并为一个非递增的有序链表(C语言编程实现)
- 【编程练习】最近准备开始找工作,这篇文章作为一个code练手题目的总结吧
- 链表操作综合练习(创建、拆分、插入、删除、合并、打印、逆置)
- BIT网教c语言练习_编程复习4链表移动
- 剑指Offer编程练习014——链表中倒数第k个结点
- 【编程题目】请修改 append 函数,利用这个函数实现两个非降序链表的并集
- 题目1519:合并两个排序的链表
- 第六章堆排序之“用最小堆将k个已排序链表合并为一个排序链表”(练习6.5-8)
- 【剑指Offer面试编程题】题目1505:两个链表的第一个公共结点--九度OJ