C ++将结构更改为类

时间:2014-02-03 22:21:18

标签: c++ struct linked-list

我正在进行链表练习,目前了解如何以struct格式编写链接列表。但是,我想更改我的代码,使链表成为一个类,并将打印,排序,添加,删除,函数作为类的成员。请告诉我如何做到这一点。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstddef>

using namespace std;
struct mylist {
    int payload;
    struct mylist * link;
};

void addlink(struct mylist *, int);
struct mylist * droplink(struct mylist *);
void printmylist(struct mylist *);
void sortmylist(struct mylist *);
int main() {
    struct mylist head;
    struct mylist *lptr;
    head.payload = 15;
    head.link = 0;
    lptr = &head;
    printmylist(lptr);
    addlink(lptr, 21);
    printmylist(lptr);
    addlink(lptr, -5);
    printmylist(lptr);
    addlink(lptr, 90);
    printmylist(lptr);
    lptr = droplink(lptr);
    printmylist(lptr);
    sortmylist(lptr);
    printmylist(lptr);
    return 0;
}
void addlink(struct mylist *lp, int val) {
    struct mylist *temp;
    struct mylist *newlink;
    //run out to end of chain
    temp = lp;
    do {
        if (temp->link != 0)
            temp = temp->link;
    } while (temp->link != 0);
    newlink = (struct mylist *) malloc(sizeof(struct mylist));
    newlink->payload = val;
    newlink->link = 0;
    temp->link = newlink;
    return;
}
struct mylist * droplink(struct mylist *lp) {
    cout << "Releasing front value of " << lp->payload << endl;
    return lp->link;
}

void printmylist(struct mylist *lp) {
    struct mylist *temp;
    temp = lp;
    while (temp->link != 0) {
        cout << temp->payload << " then ";//if there is just one link, loop never runs
        if (temp->link != 0)
            temp = temp->link;
    }
    cout << temp->payload; //gets the last link's value
    cout << endl;
    return;
}
void sortmylist(struct mylist *lp) {
    struct mylist *temp;
    struct mylist *temp2;
    int linkcount = 1;
    int temppayload;
    temp = lp;
    while (temp->link != 0) {
        if (temp->link != 0) {
            ++linkcount;
            temp = temp->link;
        }
    }
    cout << linkcount << " links " << endl;
    temp = lp;
    for (int ct2 = 1; ct2 < linkcount; ++ct2) {
        temp = lp;
        for (int ct = 1; ct < linkcount; ++ct) {

            if (temp->link != 0)
                temp2 = temp->link;

            if (temp->payload > temp2->payload) {
                temppayload = temp->payload;
                temp->payload = temp2->payload;
                temp2->payload = temppayload;

            }
            if (temp2->link != 0)
                temp = temp2;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

C ++中structclass之间唯一真正的区别是默认情况下class的所有成员都是私有的,struct的所有成员都是公共

我假设您真正要问的是如何制作结构/类的链接列表方法成员。这很容易。

所有现有方法都接受struct mylist*作为第一个参数。在C ++中,编译器会自动将其作为名为this的隐藏参数提供。您可以明确地引用this,但也可以隐式访问它。

所以在C中你可能有:

lp->payload = 0;

在C ++类成员函数中,您可以:

this->payload = 0;

或者更常见的是:

payload = 0;

从广义上讲,“c ++ ify”C代码所需的步骤是:

  • 将方法的声明移动到struct
  • 的正文中
  • 从每个方法中删除struct mylist *参数
  • 删除每种方法中对lp的引用
  • 通过取消引用struct的实例(例如lptr->addlink(-5);)来调用成员函数

答案 1 :(得分:0)

通常,您可以从以下位置更改传递给链接列表函数的每个结构:

struct Node {
    int payload;
    struct Node *link;
};
...
void addlink(struct Node *lp, int val) {
...
}

到此:

class Node {
public:
    Node() : _link(0), _payload(0) { }  // initialize members on new empty node
    Node(int val) : _link(0), _payload(val) { } // pass a new value directly

    ~Node() { 
        // call a function to clear everything pointed to from _link
    }

    void addlink(int val) {
        Node *temp = this;
        do {
            if (temp->link() != 0)
                temp = temp->link();
        } while (temp->link() != 0);

        Node *newlink = new Node(val); // this will do what the next 2 lines do

        //Node *newlink = new Node;  <- you can also create a Node this way and then assign the payload
        //newlink->setPayload(val);

        // newlink->link = 0;  <-- not needed 'new Node' has already initialized it

        temp->setLink(newlink);
    }

// provide access to the data (get/set)

    int payload() { return _payload; }
    void setPayload(int n) { _payload = n; }
    Node *link() { return _link; }
    void setLink(Node *p) { _link = p; }

// data members
private:
    int _payload;
    Node *_link;
};

这个片段给你的想法。现在你可以添加剩下的:) 当然,最好的方法是将代码放在.cpp文件中的函数中,并在标题中保留类的定义:

class Node {
public:
    Node();
    Node(int val); // pass a new value directly

    ~Node();

    void addlink(int val);

// provide access to the data (get/set)
// you can leave thse one liners in the header, they'll most likely be inlined
    int payload() { return _payload; }
    void setPayload(int n) { _payload = n; }
    Node *link() { return _link; }
    void setLink(Node *p) { _link = p; }

// data members
private:
    int _payload;
    Node *_link;
};

cpp中函数的主体:

Node::Node() : _link(0), _payload(0)
{
}

Node::Node(int val) : _link(0), _payload(val) 
{
}

......

希望这有帮助。