#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; }