单链表二

#include <iostream>
using namespace std;

//assembly access specifiers are only available in code compiled with a /clr option,
//i.e. they are wrong for class or struct, members of which are appropriate.
struct Node{
    int data;
    Node* next;
    Node(int nData)
    {
       this->data = nData;
       this->next = NULL;
    }
};

/// --- pList ---
#define ListSize 10
typedef Node *pList[ListSize];

void InsertList(pList pLt, int* nArray)
{
    pLt[0] = new Node(nArray[0]);
    for (int i = 1; i < ListSize; i++)
    {
       pLt[i] = new Node(nArray[i]);
       pLt[i - 1]->next = pLt[i];
    }
}
//

//Return pHead
Node* CreateList(int* nArray, int nLengthOfArray)
{
    if (nLengthOfArray < 1)  return NULL;

    Node* nodeTemp = new Node(nArray[0]);
    Node* pHead = nodeTemp;

    for(int i = 1; i < nLengthOfArray; i++)
    {
        Node* nodeCurrent = new Node(nArray[i]);
        nodeTemp->next = nodeCurrent;
        nodeTemp = nodeCurrent;
    }

    return pHead;
}

void InsertList(Node *pNode, int nInsertData)
{
    Node *pInsert = new Node(nInsertData);

    while(pNode->next != NULL)
    {
        pNode = pNode->next;
    }
    pNode->next = pInsert;
}

Node* ReverseList(Node* pHead)
{
    if(pHead->next == NULL)
        return pHead;

    Node* Next = NULL;
    Node* Prev = NULL;
    while(pHead != NULL)
    {
        Next = pHead->next;
        pHead->next = Prev;
        Prev = pHead;
        pHead = Next;
    }

    return Prev;
}

Node* MergeList(Node* pHead1, Node* pHead2)
{
    Node* pHead = new Node(0);

    if (NULL == pHead1)  return pHead2;
    if (NULL == pHead2)  return pHead1;

    if (pHead1->data <= pHead2->data)
    {
        pHead = pHead1;
        pHead->next = MergeList(pHead1->next, pHead2);
    }
    else
    {
        pHead = pHead2;
        pHead->next = MergeList(pHead1, pHead2->next);
    }

    return pHead;
}


void PrintList(Node* pHead)
{
    cout << "***" << endl;
    while(NULL != pHead)
    {
        cout << pHead->data << endl;
        pHead = pHead->next;
    }
}

int main()
{
    int* nArray = new int[ListSize];
    int* nArray1 = new int[ListSize];
    for (int i = 0; i < ListSize; i++)
        nArray[i] = i + 1;
    for (int i = 0; i < ListSize; i++)
        nArray1[i] = i * 2;
    
    Node* pHead = CreateList(nArray, ListSize);
    Node* pHead1 = CreateList(nArray1, ListSize);
    PrintList(pHead);
    InsertList(pHead, 100);
    PrintList(pHead1);
    //Merge
    cout << "--- MergeList ---" << endl;
    PrintList(MergeList(pHead, pHead1));

    PrintList(ReverseList(pHead));
    
    //pList
    cout << endl << "--- pList ---" << endl;
    pList pLt;
    InsertList(pLt, nArray);
    for (int i = 0; i < ListSize; i++)
    {
        cout << pLt[i]->data << endl;
    }

    //
    Node **ppListArray = new Node* [ListSize];   //Node *pListArray[ListSize]; is also valid, but its size is unchageable;
    InsertList(ppListArray, nArray);
    for (int i = 0; i < ListSize; i++)
    {
        cout << ppListArray[i]->data << endl;
    }

    delete nArray;

    system("pause");
    return 0;
}

转载于:https://my.oschina.net/ucliaohh/blog/822642

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值