我收到錯誤,試圖編譯我的頭文件中編程程序,我無法弄清楚它們。簡單鏈接列表讀取整數並將其顯示回C++
這是一個簡單的鏈接列表程序,用戶輸入一個整數列表並將其顯示回來。我會感謝所有的幫助。
繼承人是我的頭文件中的代碼
#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);
}
這是一個assignement,爲什麼你不使用'std :: list'?它是雙重鏈接的,但你會得到更多的糖果。 – 2012-03-17 20:10:26