'*'

时间:2015-10-13 01:31:49

标签: c++ list bubble-sort

//node.h

#ifndef NODE_H
#define NODE_H

class Node {

  public:
    int key;
    Node *next;
    void add(int key);
    void print();
    Node();
    Node(int key);
    Node *bubble();
    Node *swap(Node *x, Node *y);
};

#endif

//NodeSort.cpp

#include <iostream>
#include <cstdlib>
#include "node.h"

namespace node{

struct node{
  int key;
  struct node *next;
};

DNode *DNode::bubble(){              //This is where the error is.
  struct node *pos = head;
  while(pos->next !=  NULL){
    if(pos->key > pos->next->key)
      swap(pos,pos->next);
  pos=pos->next;
  }
}

void DNode::swap(Node *x,Node *y){
  node *temp = x->key;
  x->key     = y->key;
  y->key     = temp;
}

int main(){
  struct node *head;
  init(&head);
  add(head,2);
  add(head,3);
  add(head,5);
  bubble(head);
  print(head);
 }
}

NodeSort.cpp的目标是对链表(或双链表??)进行冒泡排序并返回新头。从标题中可以看出,这是我从NodeSort.cpp获得的一个错误。编译器也显示我错过了一个大括号。

*注意:函数“bubble”和“swap”是我的教师想要命名的方式。老实说,我不知道除了新类或头文件之外DNode会代表什么? Node.h中的函数在另一个名为main.cpp的文件中定义。

0 个答案:

没有答案