为什么这个自定义链表不存储c ++中给出的每个元素?

时间:2016-08-05 17:50:35

标签: c++ struct linked-list

我花了几个小时来解决这个问题,但我认为如果有人可以帮我解决这个问题会更好:

func createQuickActions(application: UIApplication) {
    let shortcut1 = UIMutableApplicationShortcutItem(type: NSBundle.mainBundle().bundleIdentifier!+".CrearActividad", localizedTitle: "Actividad", localizedSubtitle: nil, icon: UIApplicationShortcutIcon.init(templateImageName: "iphone-3d-touch-checkbox"), userInfo: nil)
    let shortcut2 = UIMutableApplicationShortcutItem(type: NSBundle.mainBundle().bundleIdentifier!+".CrearNota", localizedTitle: "Nota", localizedSubtitle: nil, icon: UIApplicationShortcutIcon.init(templateImageName: "iphone-3d-touch-notebook"), userInfo: nil)
    let shortcut3 = UIMutableApplicationShortcutItem(type: NSBundle.mainBundle().bundleIdentifier!+".CrearReunion", localizedTitle: "Reunión", localizedSubtitle: nil, icon: UIApplicationShortcutIcon.init(templateImageName: "iphone-3d-touch-calendar"), userInfo: nil)

    application.shortcutItems = [shortcut1,shortcut2,shortcut3]
}

我不希望这看起来像我的作业,因为它真的不是。我已经试着弄清楚了几个小时改变它很多,谢谢你的帮助!

编辑:重写它:

struct node{
    int x;
    node *next;
}; // basic node struct

class LinkedList{
private:
    node *head;
public:
    LinkedList(int init){ // initalizating the list 
        head = new node;
        node *rot = new node;
        rot->next = 0;
        rot->x = init;
        head->x = -1;
        head->next = rot;
        cout << "added:  " << head->next->x << endl;
    }

    void add(int adds){
        if(head != 0) {
            while ( head->next){ // goes to the latest head
                head = head->next;
            }
        }
        node *rot = new node;
        rot->next = 0;
        rot->x = adds;
        head->next = rot;
        cout << "added:  " << head->next->x << endl;
    }

    int push_last() { // pushes the last element, works fine 
        node *temp = head;
        while( temp->next)
            temp = temp->next;

        return temp->x;
    }

    int push_first(){ //shows the penultimate element instead of first one
        return head->x;
    }

};

int main()
{
  LinkedList lt(1);
  lt.add(2);
  lt.add(3);
  lt.add(4);
  cout << lt.push_first() << endl; // prints 3 / the penultimate element each time
  cout << lt.push_last() << endl; // prints last element always(4 in this case)
  return 0;
}

2 个答案:

答案 0 :(得分:1)

在你的add函数中,你将头指针移动到最后一个元素,丢失所有先前的元素(并泄漏它们的内存)。

您应该像在push_last

中一样使用临时工具

答案 1 :(得分:0)

void add(int adds){
    if(head != 0) {
        while ( head->next){ // goes to the latest head
            head = head->next;
        }
    }
    node *rot = new node;
    rot->next = 0;
    rot->x = adds;
    head->next = rot;
    cout << "added:  " << head->next->x << endl;
}

这里的问题是

    head->next = rot;

总是在列表的前面添加新节点。

由于您表示这是一项学习练习,因此他会通过一些提示对您的代码进行返工。我添加了一个析构函数,从列表的构造中解除了节点的添加,但是包含了一个create-and-add ctor,它演示了两个操作的委托(创建空列表并添加第一个节点),我添加了一个{{1帮助器和last_node函数。

我还重命名了emptypush_first:在引用这样的容器时,push_last几乎总是指示一个动作,而你的函数是简单的访问者。我重命名了pushfront,这是标准库使用的约定。

我还在LinkedList类中移动了back结构并将其设为私有(默认情况下Node的成员身份为classprivate在{之前定义} { {1}}访问者)。如果 从外部可见,则会使其成为Node

public

现场演示:http://ideone.com/UzYCii

我鼓励您接下来学习LinkedList::Node:一个处理指针所有权的标准库对象,而不是处理C ++的遗留原始指针。当#include <iostream> using std::cout; using std::endl; class LinkedList { struct Node { int x; Node *next; }; Node *head; // get a pointer to the last node in the list. // returns: head if the list is empty. Node* last_node() { Node* cur = head; while (cur->next) cur = cur->next; return cur; } public: LinkedList() : head(new Node { -1, nullptr }) { } // If you absolutely have to have a constructor that adds a node: LinkedList(int adds) : LinkedList() { add(adds); } ~LinkedList() // destructor { // Free all of the nodes we created Node *next; for (Node* cur = head; cur != nullptr; cur = next) { next = cur->next; delete cur; } } void add(int adds) { Node* tail = last_node(); tail->next = new Node { adds, nullptr }; cout << "added: " << tail->next->x << endl; } bool empty() const { return (head->next == nullptr); } // returns: value of the last node (-1 if list is empty) int back() { return last_node()->x; } // Normal implementation requires caller to check !empty() first: // returns: value of the first non-head node // if the list is empty, undefined behavior. int front() { return head->next->x; } // alternatively: // returns: value of the first non-head node or -1 if the list is empty. int front_safe() { return (head->next) ? head->next->x : head->x; } }; int main() { LinkedList lt; lt.add(1); lt.add(2); lt.add(3); lt.add(4); cout << lt.front() << endl; cout << lt.back() << endl; } 超出范围时,它会自动释放它指向的内存(如果有)。这包括std::unique_ptr是一个正在消失或被删除的实例的成员。

http://en.cppreference.com/w/cpp/memory/unique_ptr