使用C ++列表标准库帮助链接列表程序 [英] Using the C++ list standard library to help in linked-list program

查看:63
本文介绍了使用C ++列表标准库帮助链接列表程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个C ++程序,该程序应该利用链接列表来创建超级卡堆栈(无论如何).问题是,我不知道自己在做什么.我已经编写了一些代码,但是它崩溃了,说实话,我不知道它是否符合我写的代码的要求.这是我想出的代码.

I'm working on a C++ program that is supposed to utilize a linked list to create a hypercard stack (whatever that is). The problem is, I have no idea what I'm doing. I've written some code, but it crashes, and honestly I have no idea if it even meets the specifications for what I need it to do if it did work as written. Here's the code I've come up with.

更新 另外,正如您中的某些人所提到的,我想使用std::list库,如果这样做会更容易,但是我不知道该怎么做,任何帮助将不胜感激.

Update Also, as some of you have mentioned, I'd like to use the std::list library if that would make this easier, but I have no idea how to do so, any help would be greatly appreciated.

这是我的.h文件:

//program6.h
#include <iostream>
#include <fstream>
#include <list>
using namespace std;

class Node {
public:
    Node();
    Node(char code, int num, string data);
    Node(Node & node);
    ~Node();

    bool readFile();
    void setNext(Node* next);
    void print();

private:
    char Code;
    int Num;
    string Data;
    Node *Next;
};

我的实施文件:

//program6.cpp
#include "program6.h"
#include <iostream>
#include <string>
#include <list>
using namespace std;

class Node {
public:
    Node();
    Node(char code, int num, string data);
    Node(Node & node);
    ~Node();

    bool readFile();
    void setNext(Node* next);
    void print();

private:
    char Code;
    int Num;
    string Data;
    Node *Next;
};

Node::Node() {
    Code = '\0';
    Num = 0;
    Data = "";
    Next = NULL;
}

Node::Node(char code, int num, string data) {
    char Code = code;
    int Num = num;
    string Data = data;
    Next = NULL;
}

Node::Node(Node & node) {
    Code = node.Code;
    Num = node.Num;
    Data = node.Data;
    Next = NULL;
}

Node::~Node() {
}

bool Node::readFile() {
    char code = '\0';
    int num = 0;
    string data = "";

    ifstream inputFile;
    inputFile.open("prog6.dat");

    if(!inputFile) {
        cerr << "Open Faiulre" << endl;
        exit(1);
        return false;
    }

    Node *head = NULL;
    while(!inputFile.eof()) {
        inputFile >> code >> num >> data;

        Node *temp = new Node(code, num, data);
        temp->setNext(head);
        head = temp;
    }

    inputFile.close();
    head->print();
    return true;
}

void Node::setNext(Node* next) {
    Next = next;
}

void Node::print() {
    cout << Code << " " << Num << " " << Data;
    if(Next != NULL) 
        Next->print();
}

还有我的主文件/测试文件:

And my main/test file:

//program6test.cpp
#include "program6.h"
#include <iostream>
#include <fstream>
#include <list>
using namespace std;

int main() {
    Node list;
    if(list.readFile()) 
        cout << "Success" << endl;
    else
        cout << "Failure" << endl;

    return 0;
}

这是我编译并运行该程序时得到的输出:

And here is the output I get when I compile and run this program:

[cs331129@cs ~]$ g++ -o prog6 program6test.cpp
[cs331129@cs ~]$ prog6
terminate called after throwing an instance of 'St9bad_alloc'
  what():  St9bad_alloc
Aborted

这是给我的指示":

超级卡的堆栈"概念比堆栈更笼统,因为卡" (元素)可以插入到任何位置,并且整个堆栈"都可以遍历 到居家卡".使用循环列表,实现超级卡堆栈.

The hypercard "stack" concept is more general than a stack since "cards" (elements) can be inserted anywhere and the entire "stack" can be traversed back to the "home card". Using circular lists, implement a hypercard stack.

假设我们有一个包含键码的类或记录结构(如下) (整数),信息字符串(Entry类型)(少于25个字符)和一个指针

Suppose we have a class or record structure (below) that contains a key code (integer), a string of information (type Entry) 25 characters or fewer and a pointer

typedef struct elem_tag {
   Key key;
   Entry fact;
   struct elem_tag * link;
} Node_type;

我们可以创建超级卡堆栈",从文件中读取信息,建立卡的链接列表,然后执行操作(insert(i)delete(d)traverse(t)home(h)forward(f)print(p)).例如,给定一个包含以下内容的数据文件:

We can create a "hypercard stack", read in information from a file, build a linked list of cards, and perform operations (insert(i), delete(d), traverse(t), home(h), forward(f), print(p)) on the list. For example, given a data file containing:

i 27 Mary had a little lamb
i 15 Today is a good day
i 35 Now is the time!
i 9 This lab is easy and fun
p

我们在下面生成一个超级卡堆栈".

we produce a "hypercard stack" below.

                                                         -------Tail/Current
                                                         v
--->   27       --->   15        --->   35       --->   9       ---
|      Mary...         Today...         Now...          This...    |
----------------------------------------------------------------------------

请注意,27是居家卡"(居家卡是尾巴指向后的下一张),并且打印的卡将与当前"(即本练习...")相关联.如果我们现在处理以下数据文件项:

Note that 27 is the "home card" (the home card is the next one after the one tail points to) and the card printed will be associate with "Current" (i.e., "This lab ..."). If we now process the following data file items:

d 35
t
i 37 Better now.

我们将拥有以下列表(请注意,遍历(t)应该从当前指针输出整个列表中遇到的每个事实".

we will have the following list (note that traverse (t) should output each "fact" encountered for the entire list from the current pointer).

                         --- Current                                 ---- Tail
                         v                                          v
--->   27       --->     15         --->     37          --->     9       ---
|      Mary...           Today...            Better...            This...    |
-------------------------------------------------------------------------------

如果我们处理数据项

h

列表将是

  --- Current                                                --- Tail
         v                                                          v
--->     27       --->     15        --->     37          --->     9       ---
}        Mary...           Today...           Better...            This...    |
-------------------------------------------------------------------------------

要删除9(即d 9出现在数据文件中),必须先找到该卡(当前指向它")并调整尾巴".

To delete 9 (i.e., d 9 appears in the data file) the card before must be found ("Current points to it") and "Tail" adjusted.

编写C ++程序以执行以下操作:

Write a C++ program to do the following:

  • 阅读信息并构建超级卡"链接列表
  • 处理所有代码和数据.尝试阅读之前,请先检查代码 任何其他数据.请记住要检查特殊情况(例如,列表为空,列表具有一个元素,等等).
  • Read in the information and build a "hypercard" linked list
  • Process all codes and data. Check the code first before attempting to read any additional data. Remember to check for special cases (i.e., list is empty, list has one element, etc.).

输入: 文件中的每个输入数据行将包含一个字符代码和适当的数据(不会引入任何错误),并将存储在prog6.dat中.分别读取和处理每一行,直到文件末尾.

Input: Each input data line in the file will consist of a character code and the appropriate data (no errors will be introduced) and will be in prog6.dat. Read and process each line separately to the end of the file.

输出: 输出将包括来自print(p)或遍历(t)命令的事实(字符串)以及每个命令的活动响应.

Output: Output will consist of facts (strings) from print (p) or traverse (t) commands and an active response for each command.

提示: 以类或记录的形式声明列表类型(结构是公共类)-例如:

Hints: Declare a list type, either as a class or a record (struct is a public class) - for example:

typedef struct list_tag{
    Node_type * current;
    Node_type * tail;
} List_type;

通过在列表末尾依次插入每个数据项来构建初始链接列表.保持尾巴"和当前"指针.您必须检查 插入之前,尾巴"指针为NULL,否则在当前"位置之后插入.删除将导致当前"指针设置为指向要删除的卡之前的卡(除非它是最后一张卡,在这种情况下,尾"和当前"必须为NULL).前进(f)应将当前"指针移至下一张卡片.

Build the initial linked list by inserting each data item in turn at the end of the list. Maintain "Tail" and "Current" pointers. You must check to see if the " Tail" pointer is NULL before insertion, otherwise insertion is AFTER the "Current" position. Deletion will cause the "Current" pointer to be set to point at the card before the one being deleted (unless it is the last card in which case "Tail" and "Current" must become NULL). Forward (f) should move the "Current" pointer to the next card.

为每个ADT列表操作(即init/create,insert,delete,print,traverse,home)编写一个单独的功能,首先开发为存根,然后再开发为完整功能.使用标准技术将main,function和headers放置在单独的文件中

Write a separate function for each ADT list operation (i.e., init/create, insert, delete, print, traverse, home), developed first as stubs, then as full functions. Use the standard technique of placing the main, functions, and headers in separate files

这是我将用于输入的文件:

Here is the file I'll be using for the input:

i 27 Mary had a little lamb
i 15 Today is a good day
i 35 Now is the time!
i 9 This lab is easy and fun
p
d 35
t
i 37 Better Now.
f
p
h
p
d 27
d 15
d 37
d 9
i 44 This should be it!
t
p

我不知道该从哪里去或者如何做其他事情.我整夜都在阅读教程,但是这并不是在单击如何应用我正在阅读的该程序中的任何内容.我只是想通过C这门课来学习,因为自从我吸了所有这些后,我已经改变了下学期的专业.有人可以帮忙吗?

I have no idea where to go from here or how to do anything else. I've been reading tutorials all night, but it's just not clicking as to how to apply anything I'm reading it this program. I'm just trying to make it through this class with a C seeing as I've already changed my major for next semester since I suck at all of this. Can anyone help?

推荐答案

问题是,我不知道自己在做什么

The problem is, I have no idea what I'm doing

提示:

我正在研究应该利用链表的C ++程序 创建超级卡堆栈(无论是什么).

I'm working on a C++ program that is supposed to utilize a linked list to create a hypercard stack (whatever that is).

没有任何必要声明两次Node类:一种是独立类,另一种是List类的内部类.还有一些功能也没有意义.例如成员函数

There is no any sense to declare two times class Node: one as a stand alone class and other as an internal class of class List. Also some functions also have no sense. For example member function

bool readFile():

创建本地列表

Node *head = NULL;
while(!inputFile.eof()) {
    inputFile >> code >> num >> data;

    Node *temp = new Node(code, num, data);
    temp->setNext(head);
    head = temp;
}

这会导致内存泄漏.

最好定义一个内部结构为Node的列表.

It would be better to define a list that has internal structure Node.

考虑到C ++中存在标准的单个链表std::forward_list

Take into account that there is standard single linked liststd::forward_list in C++

这篇关于使用C ++列表标准库帮助链接列表程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆