//node.h
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data;
Node *next;
void printNode();
};
#endif
//node.cpp
#include "StdAfx.h"
#include "Node.h"
#include
using namespace std;
void Node::printNode()
{
cout<<data<<" ";
}
//list.h
#ifndef LIST_H
#define LIST_H
#include "Node.h"
class List
{
public:
List();
~List();
void ClearList();
void mergelink(List &listA);
void ListTraverse();
bool ListInsert(int i,Node *pNode);
bool ListInsertTail(Node *pNode);
private:
Node *m_pList;
Node *m_pFirst;
int m_iLength;
};
#endif
//list.cpp
#include "StdAfx.h"
#include "List.h"
#include
List::List()
{
m_pList=new Node;
m_pList->data=0;
m_pList->next=NULL;
m_iLength=0;
}
List::~List(){
ClearList();
delete m_pList;
m_pList=NULL;
}
void List::ClearList(){
Node *currentNode=m_pList->next;
while(currentNode!=NULL)
{
Node *temp=currentNode->next;
delete currentNode;
currentNode=temp;
}
m_pList->next=NULL;
}
bool List::ListInsertTail(Node *pNode){
Node *currentNode=m_pList;
while(currentNode->next!=NULL)
{
currentNode=currentNode->next;
}
Node *newNode=new Node;
if(newNode==NULL)
{
return false;
}
newNode->data =pNode->data;
newNode->next=NULL;
currentNode->next=newNode;
m_iLength++;
return true;
}
bool List::ListInsert(int i,Node *pNode)
{
if(im_iLength)
{
return false;
}
Node *currentNode=m_pList;
for(int k=0;k
{
currentNode=currentNode->next;
}
Node *newNode=new Node;
if(newNode==NULL)
{
return false;
}
newNode->data=pNode->data;
newNode->next=currentNode->next;
currentNode->next=newNode;
m_iLength++;
return true;
}
void List::ListTraverse(){
Node *currentNode=m_pList;
while(currentNode->next!=NULL)
{
currentNode=currentNode->next;
currentNode->printNode();
}
}
void List::mergelink(List &listA)
{
Node *head1=new Node();
head1->next=m_pFirst;
Node *first1=head1;
Node *first2=listA.m_pFirst;
while(first1->next != NULL && first2 != NULL)
{
if(first2->data<first1->next->data)
{
Node *mid=first2;
first2=first2->next;
mid->next=first1->next;
first1->next=mid;
}
else
{
first1=first1->next;
}
}
if(first2!=NULL)
{
first1->next=first2;
}
m_pFirst=head1->next;
delete head1;
listA.m_pFirst=NULL;
}
//主函数
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include
#include "List.h"
using namespace std;
int main()
{
List *pList1=new List();
List *pList2=new List();
List *pList3=new List();
Node node1,node2,node3,node4,node5,node6,nod
e7;
node1.data=1;
node2.data=3;
node3.data=5;
node4.data=2;
node5.data=4;
node6.data=6;
node7.data=7;
pList1->ListInsertTail(&node1);
pList1->ListInsertTail(&node2);
pList1->ListInsertTail(&node3);
pList1->ListInsertTail(&node7);
pList1->ListTraverse();
cout<
pList2->ListInsertTail(&node4);
pList2->ListInsertTail(&node5);
pList2->ListInsertTail(&node6);
pList2->ListTraverse();
pList1->mergelink(*pList2);
cout<<endl;
}