Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello~
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
C++
#ifndef _NODE_H_
#define _NODE_H_
#include <iostream>
using namespace std;
class Node{
private:
	int data;
	Node *next;
	Node *prev;
public:
	Node(int );
	~Node();
	void Set_next(Node *);
	void Set_prev(Node *);
	Node* Get_next();
	Node* Get_prev();
	void Set_data(int);
	int Get_data();
	void Print();
};
#endif

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

#endif

* Node.cpp
C++
#include "Node.h"

Node::Node(int data){
	this->data = data;
	this->next = NULL;
	this->prev = NULL;
}
Node::~Node(){
	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
C++
#include "List.h"

List::List(){
	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
Updated 30-Apr-13 4:06am
v3

1 solution

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!
 
Share this answer
 
Comments
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;
}
else
{
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.
So...
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900