简单链接列表以读取整数并将它们显示回C ++

时间:2012-03-17 20:08:05

标签: c++ linked-list

我在头文件中编译我的编程程序时遇到错误,我无法弄明白。

这是一个简单的链表程序,用户可以输入整数列表并将其显示回来。我将非常感谢所有的帮助。

Heres是我的头文件代码

#ifndef Linklist
#define Linklist
#include<cstdlib>


class linked_list {



public:

linked_list() {head = NULL; tail = NULL;}

void insert_front (int num);
bool empty() {return (head == NULL);}

private:

node *head;
node *tail;


};

以下是我得到的错误

1>Compiling...
1>Linklist.cpp

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(19) : error C2143: syntax error : missing ';' before '*'

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(19) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(20) : error C2143: syntax error : missing ';' before '*'

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(12) : error C2065: 'head' : undeclared identifier

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(12) : error C2065: 'tail' : undeclared identifier

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(15) : error C2065: 'head' : undeclared identifier

1>c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h(27) : fatal error C1070: mismatched #if/#endif pair in file 'c:\users\albert\documents\visual studio 2008\projects\linklist\linklist\linklist.h'

1>Build log was saved at "file://c:\Users\albert\Documents\Visual Studio 2008\Projects\Linklist\Linklist\Debug\BuildLog.htm"

1>Linklist - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这是我的程序实现

#include "linklist.h"
#include <iostream>
using namespace std;


int main()
{



void linked_list::insert_front (int num) {

node *head;
      node *tail;
      node *p;
      int num;

      head = NULL;
      tail = NULL;

 node *p = new node;
 p->set_data ( num):
 p->set_next ( head);
 head = p;

   for (int i=0; i<3; i++)
    {
       cout << "Enter number :";
       cin >> num;
       newNode = new nodeType;         // Create the new node
       newNode->data = num;            // and assign its data value
       newNode->link = NULL;           // make its link point to nothing

       if (first == NULL)              // If there is nothing in the list, then make the 
        {                               
           first = newNode;            // newNode the first item and the last item
            last = newNode;
        }

       else                            // Else if first already has a value
        {                               
           last->link = newNode;     // make the last item link to the newNode
           last = newNode;           // and make newNode the last item

        }
   }

    // Display the list

    DisplayList(first);
    system("PAUSE");
    return(0);


}

5 个答案:

答案 0 :(得分:2)

你在整个地方都使用了某种类型的node,但它没有在你的程序中的任何地方定义。你需要这样做。

此外,main()中的嵌套函数还有一些有趣的业务。我不知道你是否打算这样做,但它确实很奇怪。

答案 1 :(得分:1)

您是否在任何地方定义node班级?您还需要在头文件中包含它所包含的文件,或至少定义它:

class node;

class linked_list { ... };

另外,不要忘记头文件末尾的#endif

答案 2 :(得分:1)

如果这不是某种家庭作业,那么请考虑使用std :: list

#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    std::cout << "Enter ints end with q" << std::endl;
    std::list<int> l; //a deque is probably better TBH
    std::copy(std::istream_iterator<int>(std::cin),
              std::istream_iterator<int>(),
              std::inserter<int>(l, l.begin()));

    std::copy(l.begin(), l.end(),
              std::ostream_iterator<int>(std::cout, " "));
}

重写现有容器只是一个坏主意;同样在这个用例中,deque优于列表。

答案 3 :(得分:0)

类型node不在范围内。

如果在其他地方使用类型,您可能需要一个新的头文件 node.h 来定义缺少的类型。否则,您至少应该在列出的文件的顶部定义它。

更令人担忧的是,您不了解正确缩进代码的重要性。在你这样做之前,这样的问题会困扰你。

答案 4 :(得分:0)

我从未参与VC ++ ...但尝试在公共部分之前声明头文件中的私有部分...并且节点是VC ++中的内置数据类型吗?

相关问题