gpt4 book ai didi

c++ - C++ XCODE需要帮助初学者使用

转载 作者:行者123 更新时间:2023-12-02 11:11:51 25 4
gpt4 key购买 nike

大家好我写了下面的代码

除非我在xcode中编译时遇到错误,否则任何帮助将是最大的帮助

#include <iostream.h>

class node
{
public:
int value; //value stored in the node
node *next; //pointer to next node
node *prev; //pointer to previous node
};

class dlist
{
public:
node *front; //pointer to front of list
node *back; //pointer to back of list

dlist()
{
front=NULL;
back=NULL;
}

void insertFront(int value);
void insertBack(int value);
void removeFront();
void removeBack();
void insertBefore(int value,node *nodeB);
void insertAfter(int value,node *nodeA);
void removeBefore(node *nodeB);
void removeAfter(node *nodeA);
void removeNode(node *newNode);
void printDListFront();
void printDListBack();
};

//insert a node before nodeB
void dlist::insertBefore(int value,node *nodeB)
{
node *newNode;
newNode=new node();
newNode->prev=nodeB->prev;
newNode->next =nodeB;
newNode->value =value;
if(nodeB->prev==NULL)
{
this->front=newNode;
}
nodeB->prev=newNode;

}

//insert a node before the front node
void dlist::insertFront (int value)
{
node *newNode;
if(this->front==NULL)
{
newNode=new node();
this->front=newNode;
this->back =newNode;
newNode->prev=NULL;
newNode->next=NULL;
newNode->value=value;

}
else
{
insertBefore(value,this->front );
}
}

//insert a node after nodeB
void dlist::insertAfter(int value,node *nodeB)
{
node *newNode;
newNode=new node();
newNode->next= nodeB->next ;
newNode->prev =nodeB;
newNode->value =value;

if(nodeB->next==NULL)
{
cout<<"\n "<< endl;
this->back =newNode;
}
nodeB->next=newNode;
cout<<"2"<<endl;
}
//insert a node after the last node
void dlist::insertBack (int value)
{
if(this->back==NULL)
{
cout<<"insert at back";
insertFront(value);
}
else
{
cout<<"insert at back";
insertAfter(value,this->back );
}
}

//remove the front node
void dlist::removeFront ()
{
removeNode(this->front);
}

//remove a back node
void dlist::removeBack ()
{
removeNode(this->back);

}

//remove before a node
void dlist::removeBefore(node *nodeB)
{

if(nodeB->prev==this->front)
{
this->front=nodeB;
this->front->prev=NULL;
}
else
{
removeNode(nodeB->prev);
}
}

//remove after a node
void dlist::removeAfter(node *nodeA)
{
if(nodeA->next==this->back)
{
this->back=nodeA;
this->back->next=NULL;
}
else
{
removeNode(nodeA->next);
}
}

//remove a perticular node
void dlist::removeNode(node *nodeToRemove)
{
if(nodeToRemove==this->front)
{
this->front=this->front->next;
this->front->prev=NULL;
}
else if (nodeToRemove==this->back)
{
this->back=this->back->prev;
this->back->next=NULL ;
}
else
{
nodeToRemove->prev->next=nodeToRemove->next;
nodeToRemove->next->prev=nodeToRemove->prev;
}
}

//Print the list from front
void dlist::printDListFront()
{
node* curr2;
curr2= this->front;
cout<<"\n-----\n";
cout<<"Queue\n";
cout<<"-----\n";
//cout<<"size:"<<getQueueSize()<<endl;
while(curr2!=NULL)
{
cout<<" |"<<curr2->value<<"|";
curr2=curr2->next;
}
cout<<endl;
}// print the Double Linked List from front


// print the Double Linked List from backwards
void dlist::printDListBack()
{
node* curr2;
curr2= this->back;
cout<<"\n-----\n";
cout<<"Queue\n";
cout<<"-----\n";
//cout<<"size:"<<getQueueSize()<<endl;
while(curr2!=NULL)
{
cout<<" |"<<curr2->value<<"|";
curr2=curr2->prev;
}
cout<<endl;
}// print the Double Linked List from back

void main()
{
dlist *st ;
st= new dlist();
st->insertBack(8);
st->printDListFront ();
st->insertBack(5);
st->printDListFront ();
st->insertBack(6);
st->printDListFront ();
st->insertFront(1) ;
st->printDListFront ();
st->insertFront(3) ;
st->printDListFront ();
st->insertBack(7);
st->printDListFront ();
st->removeFront();
st->printDListFront ();
st->removeBack();
st->printDListFront ();
}

我得到的错误是必须返回“int”

最佳答案

从ISO/IEC 14882:2003(E)-3.6.1

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main:


int main() { /* ... */ } and

int main(int argc, char* argv[]) { /* ... */ }

同样,对于标准头文件也无需包含 .h
#include <iostream> // is sufficient

关于c++ - C++ XCODE需要帮助初学者使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7226044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com