Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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
#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
#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
#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
#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 30-Apr-13 4:18am
Edited 30-Apr-13 5:06am
v3

1 solution

Rate this: bad
good
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!
  Permalink  
Comments
poifrand at 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 at 30-Apr-13 9:51am
   
So what have you tried?
poifrand at 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 at 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)

  Print Answers RSS
0 OriginalGriff 565
1 Maciej Los 319
2 Richard MacCutchan 220
3 BillWoodruff 164
4 Mathew Soji 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 30 Apr 2013
Copyright © CodeProject, 1999-2014
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