-
problems on linkedlist
Code:
typedef struct Node{
int data;
struct Node* prev;
struct Node* next;
struct Node(int data)//Expected unqualified-id before "int"
{
this->data = data;
prev = NULL;
next = NULL;
};
} NODE;
class LinkedList{
private:
NODE* front;
NODE* back;
public:
LinkedList();
~LinkedList();
void appendNode(int);
void displayNodes();
void displayNodesReverse();
void destroyList();
};
LinkedList::LinkedList(){
front=NULL;
back=NULL;
}
LinkedList::~LinkedList(){
destroyList();
}
void LinkedList::appendNode(int data){
NODE* n=new NODE(data);//no matching function for call to Node::Node(int&)
if(back==NULL)
{
back=n;
front=n;
}
else{
back->next=n;
n->prev=back;
back=n;
}
}
void LinkedList::displayNodes(){
NODE* temp = front;
while(temp!=NULL){
temp=temp->next;
}
}
void LinkedList::displayNodesReverse(){
NODE* temp = back;
while(temp!=NULL){
temp=temp->prev;
}
}
void LinkedList::destroyList(){
NODE* temp = back;
while(temp!=NULL){
NODE* tmp2 = temp;
temp=temp->prev;
delete tmp2;
}
back=NULL;
front=NULL;
}
i have a problem with a linked list class, the problem is at the code below...how can you solve this kind of problem
-
what problem?
we are not mind readers.
-
The problem I suppose is the "expected unqualified-id before "int"" thing.
Your problem is that you think that structs have the same syntax as in C. In fact structs have the same syntax as classes. That is, you don't need to typedef the struct and you don't need to use the struct keyword when declaring pointers and instances. And you definitely can't use the struct keyword before the constructor.
(The C syntax is supported for backward compatibility, but apparently not for C++ features, such as constructor.)
Code:
struct Node{
int data;
Node* prev;
Node* next;
Node(int data)
{
this->data = data;
prev = NULL;
next = NULL;
};
};
typedef Node NODE;
//or you could just use Node in the rest of the code
-
I got it.....this file works normally.....do you have an idea on sorting in a linked list?
-
Sorting linked list
...is best done by swapping pointers and not done in the same way as an array. If I had to do it, I would probably choose the mergesort algorithm, as it lends itself well to a linked list structure and is a solid, fast, sort (not sure about stable...)