You are here:

C++/Doubly Linked List


     Sir, I implemented Doubly Linked List in C++.But the add and remove method can not work properly. So sir see the code and correct it. The code is,
The code of the Node class,
#ifndef _NODE_H
#define _NODE_H

class Node{
         int get( ){return object;};
         void set(int object){this -> object = object;};
         Node *getNext( ){return nextNode;};
         void setNext(Node *nextNode){this -> nextNode = nextNode;};
         Node *getPrev( ){return prevNode;};
         void setPrev(Node *prevNode){this -> prevNode = prevNode;};
         int object;
         Node *nextNode;
         Node *prevNode;

The code for the Linked List is,
#include <iostream.h>
#include <conio.c>
#include "Node.h"

class List{
         List( );
         void add(int);
         void remove( );
         int get( );
         int size;
         Node *headNode;
         Node *currentNode;
         List::List( )
         headNode = new Node( );
         headNode -> setNext(NULL);
         headNode -> setPrev(NULL);
         currentNode = NULL;
         size = 0;
         void List::add(int n)
         Node *newNode = new Node( );
         newNode -> set(n);
         if(currentNode != NULL)
         newNode -> setNext(currentNode -> getNext( ));
         newNode -> setPrev(currentNode);
         (currentNode -> getNext( )) -> setPrev(newNode);
         currentNode -> setNext(newNode);
         currentNode = newNode;
         newNode -> setNext(NULL);
         headNode -> setNext(newNode);
         newNode -> setPrev(headNode);
         currentNode = newNode;
         void List::remove( )
         if(currentNode != NULL && currentNode != headNode)
         (currentNode -> getPrev( )) -> setNext(currentNode -> getNext( ));
         (currentNode -> getNext( )) -> setPrev(currentNode -> getPrev( ));
         delete currentNode;
         currentNode = (currentNode -> getPrev( )) -> getNext( );
         int List::get( )
         if(currentNode != NULL)
         return currentNode -> get( );

main( )
   clrscr( );
   List list;
   cout << list.get( );

Sir see the code and correct the code.
         I am very thankful to you.

         Imran Ahmad Mughal

Imran Ahmad Mughal, Thank you for your question.

I regret that I am unable to do debugging for people due to the volume of questions received. This kind of problem is usually easy to solve by stepping through the code with your debugger, looking at the values of the variables until you see something wrong. This should show you the exact line of the bug.

David Spector


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2017 All rights reserved.