qq_34294203 2016-10-07 11:42 采纳率: 100%
浏览 1096
已采纳

c++链表二路归并法,求大神帮忙看看哪里错了

//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;

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-10-07 14:06
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?