Frage

So i created a class Linked_List with a Struct node but it seems there is some errors i don't know how to fix , here is the code :

#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



}

The compiler Gave me these Errors (It gave me this errors on Delete_First Function):

1-'Null' : undeclared identifier

2- '=' : cannot convert from 'int' to 'struct node *'

both errors were on (first=last=Null;}) line

your help is very Appreciated

War es hilfreich?

Lösung

first=last=Null; is an error because Null should be NULL (all-caps). No need to declare/define NULL, it's in your implementations header files already (in your case, <iostream>).

Actually, you'll find that NULL is actually just a macro which expands to 0 (or (void*)0). ie #define NULL (void*)0.

Let me know if this fixes both errors.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top