小编Mar*_*can的帖子

在C++中以相反的顺序打印我的链表

所以我对C++还不熟悉,今天我决定坐下来了解链表是如何工作的.到目前为止,我有很多乐趣,但是当我尝试以相反的顺序打印我的链接列表时遇到了一个问题(而不是颠倒链接列表的顺序!)

另外,我想在没有双链表的情况下这样做:

#include <iostream>
#include <string>

using namespace std;

class LinkedList
{
    public:
        LinkedList()
        {
            head = NULL;
        }

        void addItem(string x)
        {
            if(head == NULL)
            {
                head = new node();
                head->next = NULL;
                head->data = x;
            } else {
                node* temp = head;
                while(temp->next != NULL)
                    temp = temp->next;

                node* newNode = new node();
                newNode->data = x;
                newNode->next = NULL;
                temp->next = newNode;
            }
        }
        void printList()
        {
            node *temp = head;
            while(temp->next != NULL)
            {
                cout << temp->data …
Run Code Online (Sandbox Code Playgroud)

c++ linked-list

9
推荐指数
1
解决办法
2万
查看次数

C++:清空if语句

这更像是一个入门级问题,但我想知道是否有一个空if语句是一个好习惯.

考虑以下代码:

void RabbitList::purge()
{
    if(head == NULL)
    {
        //cout << "Can't purge an empty colony!" << endl;
    }
    else
    {
        //Kill half the colony
        for(int amountToKill = (getColonySize()) / 2; amountToKill != 0;)
        {
            RabbitNode * curr = head;
            RabbitNode * trail = NULL;

            bool fiftyFiftyChance = randomGeneration(2);

            //If the random check succeeded but we're still on the head node
            if(fiftyFiftyChance == 1 && curr == head)
            {
                head = curr->next;
                delete curr;
                --size;
                --amountToKill;
            }
            //If the random …
Run Code Online (Sandbox Code Playgroud)

c++ if-statement

3
推荐指数
2
解决办法
1万
查看次数

标签 统计

c++ ×2

if-statement ×1

linked-list ×1