Circolar double linked List

Hi, I wrote this code with Deitel&Deitel near me and other examples I found on the web, but It doesnt work. Im quite sure that the first problem is from destructor but i dont know the exact reason. The main should show that everything work properly so I didnt post it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  #ifndef LISTAC_H_
#define LISTAC_H_

#include <iostream>

using namespace std;

#include <iostream>
using namespace std;
#include "ListaC.h"

template<class t> class ListaC;
template<class t> class nodoL;

template<class t>
class nodoL{

friend class ListaC<t>;

public:
	nodoL(t &d) : nextPtr(0), prePtr(0), data(d) {}
    t getData() {return data;}
private:
	nodoL<t>* nextPtr;
	nodoL<t>* prePtr;
	t data;
};

//LISTA
template<class t>
class ListaC{

public:
    ListaC();
    ~ListaC();
    bool isEmpty();
    void insertA(t&);
    void insertO(t&);
    void stampaO();
    void stampaA();
private:
    nodoL<t>* firstPtr;
    nodoL<t>* lastPtr;
    nodoL<t>* getNewNode(t&);
};

template<class t>
ListaC<t>::ListaC(){
	firstPtr=0;
	lastPtr=0;
}

template<class t>
ListaC<t>::~ListaC(){
	if(!isEmpty()){
	nodoL<t> *temp, *current=firstPtr;
	cout<<"Destroying: ";
	 do{
		cout<< current->data<<" ";
	    temp=current;
		current=current->nextPtr;
		delete temp;
	 } while(current!=firstPtr);
	}
}

template<class t>
bool ListaC<t>::isEmpty(){
    if((firstPtr==lastPtr)==0)
    return true;
    else return false;
}

//Insert in front
template<class t>
void ListaC<t>::insertO(t& d){
	nodoL<t>* newPtr=getNewNode(d);

	if(isEmpty()){
	firstPtr=newPtr;
	}
	else {
		nodoL<t>* tempPtr=firstPtr;
		firstPtr=newPtr;

		newPtr->prePtr=lastPtr;
		newPtr->nextPtr=tempPtr;
	    lastPtr->nextPtr=newPtr;
	}
	cout<<"Inserted Orario."<<endl;
}

//insert in back
template<class t>
void ListaC<t>::insertA(t& d){
	nodoL<t>* newPtr=getNewNode(d);

	if(isEmpty()){

	lastPtr=newPtr;
	}
	else {
		nodoL<t>* tempPtr=lastPtr;
		lastPtr=newPtr;
		newPtr->prePtr=tempPtr;
		newPtr->nextPtr=firstPtr;
		tempPtr->nextPtr=newPtr;
		firstPtr->prePtr=newPtr;
	}
	cout<<"Inserted Antiorario."<<endl;
}

//print in clockwise
template<class t>
void ListaC<t>::stampaO(){
	if(!isEmpty()){
	nodoL<t>* currentPtr=firstPtr;

	cout<<"Lista in senso orario: "<<endl;
	 while(currentPtr!=lastPtr){
		cout<<currentPtr->getData()<<" ";
        currentPtr=currentPtr->nextPtr;
	 }
    cout<<lastPtr<<endl;
	}
}

//print in anticlockwise
template<class t>
void ListaC<t>::stampaA(){
	if(!isEmpty()){
	nodoL<t>* currentPtr=lastPtr;

	cout<<"Lista in senso antiorario: "<<endl;
	 while(currentPtr!=firstPtr){
		cout<<currentPtr->getData()<<" ";
        currentPtr=currentPtr->prePtr;
	 }
    cout<<firstPtr<<endl;
    }
}

template<class t>
nodoL<t>* ListaC<t>::getNewNode(t &n){
	nodoL<t>*newPtr=new nodoL<t>(n);
	return newPtr;
}


#endif /* LISTAC_H_ */ 
but It doesnt work.

Perhaps you could be a little more specific?
I fixed it...
I forgot to put equal firstPtr and lastPtr to the new node in insertO and insert A and I changed if((firstPtr==lastPtr)==0) in if(firstPtr==0 && lastPtr==0) although I cant see the difference ...
Sorry for the fake alarm but thanks for the answer :)
if((firstPtr==lastPtr)==0) vs if(firstPtr==0 && lastPtr==0)

The first evaluates to true anytime firstPtr and lastPtr don't contain the same value. It could be rewritten as:
if ( !(firstPtr == lastPtr) ) or if ( (firstPtr == lastPtr) == false). So if firstPtr and lastPtr are 0, then you have: if ( (0 == 0) == false ) or more simply: if ( true == false )

The second evaluates to true only when both firstPtr and lastPtr are null.
Last edited on
Topic archived. No new replies allowed.