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 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站