双链表列出故障

时间:2018-03-09 23:05:20

标签: c++ linked-list

我试图向后显示一个双向链表,但每次我尝试运行任何东西,甚至远程触摸程序中的“prev”指针,我得到一个seg错误。 我一直在努力解决这个问题大约4个小时,我似乎无法把它搞定。我无法分辨问题是来自我的打印后退功能还是来自实际的指针本身。

#include <iostream> 
#include "list.h"

LinkedList::LinkedList(){
    head = NULL;
    tail = NULL;     

};

bool LinkedList::addAtBeginning(int val){
    Node *upd8L = head; // This Node will update Last
    Node *upd8 = head;;  // This Node will update the previous pointers
    Node *point = new Node(); // This Node will insert the new node at the beginning
    point->data=val;      // This sets the data in the new node
    point->next=head;         // This sets the next pointer to the same as head
    head = point;         // This sets the head to the new Node

    while(upd8){
    upd8 = upd8->next;
    upd8->prev = upd8L;
    upd8L=upd8L->next; 
    }

return true; 
};

bool LinkedList::remove(int val){
    Node *temp = head;
    Node *trail = 0;
    while(temp != NULL){
        if(temp->data == val){
            if(temp->next == head->next){
            head = head->next; 
            }else{
            trail->next = temp->next; 
            }
        delete temp; 
        }   
    trail = temp; 
    temp = temp->next;
    }
return true; 
};

void LinkedList::printForward() const{
    Node *temp; 
    temp = head;
    while(temp){
        cout << temp -> data << endl;
        temp = temp->next; 
        }

};

void LinkedList::printBackward() const{
    Node *temp = head; 
    while(temp){
    temp = temp->next;
    cout << temp->data << endl;
    }
    while(temp){
    cout << temp->data;
    cout << "Pop" << endl;
    temp = temp-> prev;
    }
};

如果可能的话,我会喜欢解释什么是烦扰我的程序,而不仅仅是一个简单的答案,我想知道我做错了什么以及为什么这是错的。 谢谢!

修改 这是list.h

#ifndef LIST_H
#define LIST_H

#include <iostream>
using namespace std;

class LinkedList
{
private:
    struct Node
    {
        int data;
        Node * next;
        Node * prev;
    };
    Node * head, * tail;
public:
    LinkedList();
    bool addAtBeginning(int val);
    bool remove(int val);
    void printForward() const;
    void printBackward() const;
};


#endif

2 个答案:

答案 0 :(得分:0)

函数printBackward()可能会在循环的最后一次迭代中导致seg-fault。 while(temp)表示迭代,直到您从列表NULL中获取元素。然后,您分配temp = temp->next,其中temp->nextNULL。现在,当您调用cout << temp->data << endl;时,您正试图从NULL指针获取数据。尝试更改顺序。首先显示节点数据,然后更改temp指针。一个例子:

void LinkedList::printBackward() const{
    Node *temp = head; 
    while(temp){
    cout << temp->data << endl;
    temp = temp->next;
    }

你做错了是从NULL指针获取数据。

答案 1 :(得分:0)

所以,经过大量的反复试验,我想出来了! 我遇到的最大问题就是给我分段错误,每当我删除列表中的元素时,我都无法更新&#34; prev&#34;节点的一部分,因此任何时候我试图向后读取列表我得到一个seg错误。

package main

import (
    "fmt"
    "log"
    "os/exec"
    "os"
    "github.com/jroimartin/gocui"
    "time"
    "sync"
)

var (
    do_quit = make(chan int)
    do_pause = make(chan int)
    show_aux = false
    wg  sync.WaitGroup
)

func main() {
    for {
        // DEBUG: fmt.Println("start gui")

        g, err := gocui.NewGui(gocui.OutputNormal)
        if err != nil {
            log.Panicln(err)
        }
        defer g.Close()

        g.SetManagerFunc(layout)
        setKeys(g)

        wg.Add(1)
        go showTime(g)

        if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
            continue        // restart gui if something goes wrong
        }
        wg.Wait()
    }
}


func layout(g *gocui.Gui) error {
    maxX, _ := g.Size()

    if v, err := g.SetView("main", -1, -1, maxX/2-1, 4); err != nil {
        if err != gocui.ErrUnknownView {
            return err
        }
        fmt.Fprintf(v, "sys: %s\n", time.Now().Format("2006-01-02 15:04:05.000"))
        v.Frame = false
    }

    return nil
}

func runPager(g *gocui.Gui, v *gocui.View) error {
    do_quit <-1
    g.Close()
    cmd := exec.Command("less", "/etc/sysctl.conf")
    cmd.Stdout = os.Stdout
    err := cmd.Run()

    return err
}

func runEditor(g *gocui.Gui, v *gocui.View) error {
    do_quit <-1
    g.Close()
    cmd := exec.Command("vi", "/tmp/strace.out")
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    err := cmd.Run()

    return err
}

func runPsql(g *gocui.Gui, v *gocui.View) error {
    do_quit <-1
    g.Close()
    cmd := exec.Command("psql", "-U", "postgres")
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    err := cmd.Run()

    return err
}

func showAux(g *gocui.Gui, _ *gocui.View) error {
    if !show_aux {
        maxX, maxY := g.Size()
        if v, err := g.SetView("aux", -1, 3*maxY/5-1, maxX-1, maxY-1); err != nil {
            if err != gocui.ErrUnknownView {
                return err
            }
            fmt.Fprintln(v, "")
            v.Frame = false
        }
    } else {
        g.DeleteView("aux")
    }
    show_aux = !show_aux
    return nil
}

func showTime(g *gocui.Gui) {
    var pause = false
    defer wg.Done()

    for {
        select {
        case <-do_pause:
            pause = !pause
        case <-do_quit:
            return
        case <-time.After(1 * time.Second):
            if pause { continue }

            g.Update(func(g *gocui.Gui) error {
                v, err := g.View("main")
                if err != nil {
                    return err
                }
                v.Clear()
                fmt.Fprintf(v, "sys: %s\n", time.Now().Format("2006-01-02 15:04:05.000"))

                if show_aux {
                    v, err := g.View("aux")
                    if err != nil {
                        return err
                    }
                    v.Clear()
                    fmt.Fprintf(v, "aux: %s\n", time.Now().Format("2006-01-02 15:04:05.000"))
                }
                return nil
            })
        }
    }
}

func doQuit(g *gocui.Gui, v *gocui.View) error {
    do_quit <- 1
    g.Close()
    os.Exit(0)
    return gocui.ErrQuit
}

func setKeys(g *gocui.Gui) {
    if err := g.SetKeybinding("", gocui.KeyCtrlQ, gocui.ModNone, doQuit); err != nil {
        log.Panicln(err)
    }
    if err := g.SetKeybinding("", 'c', gocui.ModNone, runPager); err != nil {
        log.Panicln(err)
    }
    if err := g.SetKeybinding("", 'e', gocui.ModNone, runEditor); err != nil {
        log.Panicln(err)
    }
    if err := g.SetKeybinding("", 'p', gocui.ModNone, runPsql); err != nil {
        log.Panicln(err)
    }
    if err := g.SetKeybinding("", 'b', gocui.ModNone, showAux); err != nil {
        log.Panicln(err)
    }
}