链接列表问题

时间:2014-04-06 22:09:36

标签: c++ class struct nodes

所以我创建了一个带有Struct节点的类Linked_List,但似乎有一些错误我不知道如何解决,这里是代码:

#include <iostream>
using namespace std;



struct node{

int info;

node *link;

};


class Linked_List{


private :

int count;
node *first;
node *last;
node *current;

public:


Linked_List() {

count=0;
first=NULL;
last=NULL;
}



void Initialize_List(){

cout<<"Enter Number OF Nodes"<<endl;
cin>>count;

first=last=current=new node;

for(int i =0;i<count;i++){
cin>>current->info;
last->link=current;
last=current;
   current=new node;}
last->link=NULL;
}


bool Is_Empty(){
if(first==NULL)
{
cout<<"The List Is Empty"<<endl;
return true;
}

else{
cout<<"The List Is Not Empty"<<endl;
return false;}
}


bool Search(int x){

for(current=first;current!=NULL;current=current->link){

    if (current->info==x)
        return true;
    else return false;} }



void Insert_First(int x){

count++;

current=new node;
current->info=x;
current->link=first;

first=current;


if(last==NULL){
last=current;
last->link=NULL;}}

void Insert_Last(int x){
count++;
current=new node;
current->info=x;

last->link=current;

last=current;
last->link=NULL;

if(first==NULL)

first=current;
}


void Delete_First(){

if(!Is_Empty())
{ node *p;

p=first;
first=first->link;
delete p;
count --;
if(count==0)
first=last=Null;}
}


void Delete_Last(){

node *p;
if(count<=1){
count=0;
p=last;
delete p;
last=first=NULL;}

else {

   p=first;
for(p=first;P->link!=last;p=p->link){
last=p;
p=p->link;
delete p;
last->link=NULL;
count--;}}
}

};

void main (){



//nothing done here yet



}

编译器给了我这些错误(它在Delete_First函数上给了我这个错误):

1-'Null':未声明的标识符

2'=':无法从'int'转换为'struct node *'

两个错误都已开启(first = last = Null;})line

你的帮助非常赞赏

1 个答案:

答案 0 :(得分:2)

first=last=Null;是一个错误,因为Null应为NULL(全部大写)。无需声明/定义NULL,它已经在您的实现头文件中(在您的情况下为<iostream>)。

实际上,您会发现NULL实际上只是一个扩展为0(或(void*)0)的宏。即#define NULL (void*)0

如果这样可以解决这两个错误,请告诉我。