#ifndef DNODE_H
#define DNODE_H
class DNode{
private:
int val; // data
DNode * prev;
DNode * next;
friend class DLinkedList;
};
#endif
#include"DNode.h"
#ifndef DLINKEDLIST_H
#define DLINKEDLIST_H
class DLinkedList{
private:
DNode * head; //pointer to the first node
DNode * trailer; // pointer to last node
public:
DLinkedList();
~DLinkedList(); // Destructor
bool empty() const; // is list empty?
const int& front() const; // get front element
const int& back() const; // get back element
void addFront(const int & e); // add to front of list
void addBack(const int & e); // add to back of list
void removeFront(); // remove front item from list
void removeBack(); // remove back item from list
void displayList();
};
#endif
#include"DLinkedList.h"
DLinkedList::DLinkedList(){
head = new DNode; //creating the first element in the Linked List
trailer = new DNode; //creating the last element in the Linked List
// Both head and trailer have each other's address
head->next = trailer;
trailer->next = head;
}
DLinkedList::~DLinkedList(){
delete head;
delete trailer;
}
bool DLinkedList::empty() const{
return head->next == trailer;
} // is list empty?
const int& DLinkedList::front() const{
return head->next->val;
} // get front element
const int& DLinkedList::back() const{
return trailer->prev->val;
} // get back element
void DLinkedList::addFront(const int & e){
DNode *newNode = new DNode(); // creating a new node pointer object
newNode->val = e; // // Assigning a value to the newly created node that is passed as parameter in our addFront function
newNode->next = head; // Our new node points to head- going to be the first element(head) of the Linked List
head = newNode; //Now the head is the same as the new node
} // add to front of list
void DLinkedList::addBack(const int & e){
DNode *newNode = new DNode(); // creating a new node pointer object
newNode->val = e;
newNode->next = NULL;
if (head == NULL){
head = newNode;
return;
}
DNode * lastNode = head;
while(lastNode->next!=NULL){
lastNode=lastNode->next;
}
lastNode->next = newNode;
} // add to back of list
void DLinkedList::removeFront(){
/* if(head != NULL){
std::cout<< "The List if Empty";
}*/
DNode * temp = head;
head = head->next;
delete temp;
} // remove front item from list
void DLinkedList::removeBack(){
if(head != NULL){
if(head->next==NULL){
head=NULL;
}
else{
DNode *temp = head;
while(temp->next->next != NULL){
temp = temp->next;
DNode *last = temp->next;
temp->next = NULL;
delete last;
}
}
}
}
void DLinkedList::DLinkedList::displayList(){
DNode *current = head;
while(current != NULL){
std::cout<<current->val<<" --> ";
current = current->next;
}
std::cout<<"NULL"<<std::endl;
}
#include <iostream>
#include<string>
#include"DLinkedList.cpp"
int main() {
DLinkedList list;
list.addFront(10);
list.addFront(20);
list.addFront(30);
list.addBack(40);
list.addBack(50);
list.displayList();
}
Error:
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList::removeBack()':
/home/runner/DLisnked-List/./DLinkedList.cpp:55: multiple definition of `DLinkedList::removeBack()'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:55: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList::displayList()':
/home/runner/DLisnked-List/./DLinkedList.cpp:71: multiple definition of `DLinkedList::displayList()'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:71: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList::removeFront()':
/home/runner/DLisnked-List/./DLinkedList.cpp:50: multiple definition of `DLinkedList::removeFront()'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:50: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList::addBack(int const&)':
/home/runner/DLisnked-List/./DLinkedList.cpp:31: multiple definition of `DLinkedList::addBack(int const&)'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:31: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList::addFront(int const&)':
/home/runner/DLisnked-List/./DLinkedList.cpp:25: multiple definition of `DLinkedList::addFront(int const&)'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:25: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList':
/home/runner/DLisnked-List/./DLinkedList.cpp:4: multiple definition of `DLinkedList::DLinkedList()'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:4: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList':
/home/runner/DLisnked-List/./DLinkedList.cpp:4: multiple definition of `DLinkedList::DLinkedList()'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:4: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `~DLinkedList':
/home/runner/DLisnked-List/./DLinkedList.cpp:11: multiple definition of `DLinkedList::~DLinkedList()'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:11: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `~DLinkedList':
/home/runner/DLisnked-List/./DLinkedList.cpp:11: multiple definition of `DLinkedList::~DLinkedList()'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:11: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList::back() const':
/home/runner/DLisnked-List/./DLinkedList.cpp:23: multiple definition of `DLinkedList::back() const'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:23: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList::empty() const':
/home/runner/DLisnked-List/./DLinkedList.cpp:17: multiple definition of `DLinkedList::empty() const'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:17: first defined here
/nix/store/v8imx1nvyz0hgvx9cbcmh6gp4ngw3ffj-binutils-2.35.1/bin/ld: /tmp/DLinkedList-41fe40.o: in function `DLinkedList::front() const':
/home/runner/DLisnked-List/./DLinkedList.cpp:20: multiple definition of `DLinkedList::front() const'; /tmp/main-7d3742.o:/home/runner/DLisnked-List/./DLinkedList.cpp:20: first defined here
clang-12: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:9: main] Error 1
exit status 2