Click here to Skip to main content
13,085,936 members (88,188 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to understand the principle of double circular linked list.
I will show you C++ code. (Code is so long)
I have to implement 'List.cpp' but this is so difficult to me.

* Node.h
#ifndef _NODE_H_
#define _NODE_H_
#include <iostream>
using namespace std;
class Node{
	int data;
	Node *next;
	Node *prev;
	Node(int );
	void Set_next(Node *);
	void Set_prev(Node *);
	Node* Get_next();
	Node* Get_prev();
	void Set_data(int);
	int Get_data();
	void Print();

* List.h
#ifndef _LIST_H_
#define _LIST_H_
#include "Node.h"
class List{
	Node *Head;
	void InFt(int );
	void InBk(int );
	bool DelFt(int &);
	bool DelBk(int &);
	void Sort();
	void ShowFt();
	void ShowBk();

* Node.cpp
#include "Node.h"
Node::Node(int data){
	this->data = data;
	this->next = NULL;
	this->prev = NULL;
	cout<<data<<" is deleted"<<endl;
void Node::Set_next(Node *next){
	this->next = next;
void Node::Set_prev(Node *prev){
	this->prev = prev;
Node* Node::Get_next(){
	return next;
Node* Node::Get_prev(){
	return prev;
void Node::Set_data(int data){
	this->data = data;
int Node::Get_data(){
	return data;
void Node::Print(){
	cout << data << " " ;

* List.cpp
#include "List.h"
	this->Head = NULL;
void List::InFt(int data){
	// insert this part
	// Insert new node at front
void List::InBk(int data){
	// insert this part
	// Insert new node at back

The more code is remained. But I just want to begin the first function(InFt).
I understood the principle of linked list in one-way(not double circular). But I can not understand 'double circular linked list'. Please help me~
Posted 30-Apr-13 3:18am
Updated 30-Apr-13 4:06am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

So start at the beginning, and do it one step at a time.

You have the constructor, so implement the InFt function, and test it in the debugger.
When it works, add the InBk function and test that. Do not move on to another function until you have each previous one working.

Keep going in little stages until you have added each and every function and it all works.

It's your homework! So knuckle down, and give it a try - it's not that difficult and if you get stuck on specific problem, feel free to ask about it. But we aren't going to do it all for you!
poifrand 30-Apr-13 9:46am
Thank you for your comment. I agree with your opinion. But I wrote whole codes to enlighten the people who read this question effectively. I don't want to get whole answers. I just want to start 'InFt' function.
OriginalGriff 30-Apr-13 9:51am
So what have you tried?
poifrand 30-Apr-13 10:09am
I'm sorry to bother you. I attached the InFt function I have wrote until now.

void List::InFt(int data){
Node *newfront = new Node(data);

if(Head == NULL)
Head = newfront;
Head->Get_next() = Head;
Head->Get_prev() = Head;
newfront->Get_prev() = Head->Get_prev();
Head->Get_prev()->Get_next() = newfront;

newfront->Get_next() = Head;
Head->Get_prev() = newfront;
OriginalGriff 30-Apr-13 10:48am
First off, don't try using Get_next and so on here - you don't want the value so much as the address.
Second, if you are puttin this in as the first element, then by definition it has no previous element.
Isn't this simpler?

Node *oldHead = Head;
Head = new Node(data);
Head->prev = NULL;
Head->next = oldHead;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100