链表问题

Linked List Problems

本文关键字:问题 链表      更新时间:2023-10-16

所以我创建了一个带有 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"转换为"结构节点*"

两个错误都打开了(第一个=最后一个=空;})线

非常感谢您的帮助

first=last=Null;是一个

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

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

让我知道这是否可以修复这两个错误。