zoukankan      html  css  js  c++  java
  • 编程练习 链表题目反序,合并

    Code Snippet
    1. #include <iostream>
    2. #include<fstream>
    3.  
    4. using namespace std;
    5.  
    6. struct Node
    7. {
    8.         int data ;
    9.         Node *next ;
    10. };
    11. typedef struct Node Node ;
    12.  
    13. void ReverseList(Node*& head)
    14. {
    15.     if(head == NULL || head->next == NULL)
    16.         return;
    17.  
    18.     Node* currentNode = head;
    19.     Node* previousNode = NULL;
    20.  
    21.     while(currentNode != NULL)
    22.         ...{
    23.             Node* temp = currentNode->next;
    24.             currentNode->next = previousNode;
    25.             previousNode = currentNode;
    26.             currentNode = temp;
    27.     }
    28.  
    29.     head = previousNode;
    30. }
    31.  
    32. // My answer.
    33. Node* MergeLinkList(Node* listA, Node* listB)
    34. {
    35.     if(listA == NULL)
    36.         return listB;
    37.     if(listB == NULL)
    38.         return listA;
    39.  
    40.     Node* pHeadSmall = listA -> data < listB -> data ? listA : listB;
    41.     Node* pHeadBig = pHeadSmall == listA ? listB : listA;
    42.  
    43.     Node* p = pHeadSmall;
    44.     Node* q = pHeadSmall->next;
    45.     Node* r = pHeadBig;
    46.  
    47.     while(q != NULL && r != NULL)
    48.         ...{
    49.             if(q->data <= r->data)
    50.                 ...{
    51.                     p->next = q;
    52.                     p = p->next;
    53.                     q = q->next;
    54.             }
    55.             else
    56.                 ...{
    57.                     p->next = r;
    58.                     p = p->next;
    59.                     r = r->next;
    60.             }
    61.     }
    62.     if(q == NULL)
    63.     {
    64.             p->next = r;
    65.     }
    66.     else
    67.     {
    68.             p->next = q;
    69.     }
    70.     return pHeadSmall;
    71. }
    72.  
    73. // Standard answer
    74. Node * Merge(Node *head1 , Node *head2)
    75. {
    76.     if ( head1 == NULL)
    77.         return head2 ;
    78.     if ( head2 == NULL)
    79.         return head1 ;
    80.     Node *head = NULL ;
    81.     Node *p1 = NULL;
    82.     Node *p2 = NULL;
    83.     if ( head1->data < head2->data )
    84.     {
    85.             head = head1 ;
    86.             p1 = head1->next;
    87.             p2 = head2 ;
    88.     }
    89.     else
    90.     {
    91.             head = head2 ;
    92.             p2 = head2->next ;
    93.             p1 = head1 ;
    94.     }
    95.     Node *pcurrent = head ;
    96.     while ( p1 != NULL && p2 != NULL)
    97.     {
    98.             if ( p1->data <= p2->data )
    99.             {
    100.                     pcurrent->next = p1 ;
    101.                     pcurrent = p1 ;
    102.                     p1 = p1->next ;
    103.             }
    104.             else
    105.             {
    106.                     pcurrent->next = p2 ;
    107.                     pcurrent = p2 ;
    108.                     p2 = p2->next ;
    109.             }
    110.     }
    111.     if ( p1 != NULL )
    112.         pcurrent->next = p1 ;
    113.     if ( p2 != NULL )
    114.         pcurrent->next = p2 ;
    115.     return head ;
    116. }
    117.  
    118. // Utillity function.
    119. void TestList(Node* head)
    120. {
    121.     Node* p = head;
    122.     while(p != NULL)
    123.     {
    124.             cout << p->data << endl;;
    125.             p= p->next;
    126.     }
    127. }
    128.  
    129. // Utillity function.
    130. Node* InputList(ifstream& inFile)
    131. {
    132.     int len;
    133.     inFile >> len;
    134.     Node* head = new Node[len];
    135.     Node* p = head;
    136.     for(int i = 0; i < len - 1; i++)...{
    137.         inFile >> p->data;
    138.         p->next = p + 1;
    139.         p = p->next;
    140.     }
    141.     inFile >> p->data;
    142.     p->next = NULL;
    143.  
    144.     return head;
    145. }
    146.  
    147. int main()
    148. {
    149.     ifstream inFile("d:\data.txt");
    150.     if(!inFile)
    151.         return -1;
    152.  
    153.     Node* headA = InputList(inFile);
    154.     Node* headB = InputList(inFile);
    155.  
    156.     TestList(headA);
    157.     TestList(headB);
    158.  
    159.     ReverseList(headA);
    160.     cout<< "Testing Reverse A" << endl;
    161.     TestList(headA);
    162.  
    163.     ReverseList(headA);
    164.     cout<< "Testing Reverse A back." << endl;
    165.     TestList(headA);
    166.  
    167.     Node* newHead = Merge(headA, headB);
    168.     cout<< "Testing Merged List." << endl;
    169.     TestList(newHead);
    170.  
    171.     while(newHead != NULL)
    172.     {
    173.         Node* ptr = newHead;
    174.         newHead = newHead->next;
    175.         delete ptr;
    176.     }
    177. }
  • 相关阅读:
    软件质量
    LINUX
    C# .net 多线程中集合数据同步
    一些常用COM接口表
    PHP、JAVA、C#、Object-C 通用的DES加密
    【C#公共帮助类】给大家分享一些加密算法 (DES、HashCode、RSA、AES等)
    C#中如何把int转换成两个字符的string 缺位补零
    C#中Math.Round()实现中国式四舍五入
    C# RGB与16进制颜色转换方法
    c# e语言 字节集 表示方式
  • 原文地址:https://www.cnblogs.com/awpatp/p/1597015.html
Copyright © 2011-2022 走看看