c++ - Linked List Problems -
so created class linked_list struct node seems there errors don't know how fix , here 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 =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 empty"<<endl; return true; } else{ cout<<"the list 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 errors on delete_first function):
1-'null' : undeclared identifier
2- '=' : cannot convert 'int' 'struct node *'
both errors on (first=last=null;}) line
your appreciated
first=last=null;
error because null
should null
(all-caps). no need declare/define null
, it's in implementations header files (in case, <iostream>
).
actually, you'll find null
macro expands 0
(or (void*)0
). ie #define null (void*)0
.
let me know if fixes both errors.
Comments
Post a Comment