doubly linked list

D

Defected

Hello

How I can change the list, so that it can not enter duplicate value ?

and this list is correct ?

Thanks.

----------------------------------------------------------------------

/* Double linked list */

#include <iostream>

using namespace std;

class Nodo {
private:
int value;
Nodo* prev;
Nodo* next;
public:
Nodo();
Nodo(int val, Nodo* pr, Nodo* ne);
friend class Lista;
};

Nodo::Nodo() {
value = 0;
prev = NULL;
next = NULL;
}

Nodo::Nodo(int val, Nodo* pr, Nodo* ne) {
value = val;
prev = pr;
next = ne;
}

class Lista {
private:
Nodo* head;
Nodo* tail;
public:
Lista();
~Lista();
void inserisciLista(int val);
void cancLista(int val);
void stampaLista();
void stampaRevList();
};

Lista::Lista() {
head = NULL;
tail = NULL;
}

Lista::~Lista() {
Nodo *ptr, *temp;
ptr = head;
// go thru the list and delete all nodes
while (ptr != NULL) {
temp = ptr->next;
delete(ptr);
ptr = temp;
}
}

void Lista::inserisciLista(int val) {
// sorted inserisciLista. from smallest to largest number
Nodo *ptr;
ptr = head;

if (ptr == NULL) { // 1st: inserisciListaing first element
head = new Nodo(val, NULL, NULL);
tail = head;
return;
}

// traverse to the right place
while (ptr->next != NULL && ptr->value <= val) {
ptr = ptr->next;
}

if (ptr == head && ptr->value > val) { // 2nd: if inserisciListaing
// to head
head = new Nodo(val, NULL, head);
head->next->prev = head;
}
else if (ptr == tail && ptr->value <= val) { // 3rd: inserisciListaing
// to end
tail = new Nodo(val, tail, NULL);
tail->prev->next = tail;
}
else { // 4th: inserisciListaing to middle
ptr->prev = new Nodo(val, ptr->prev, ptr);
ptr->prev->prev->next = ptr->prev;
}
}

void Lista::cancLista(int val) {
Nodo *ptr;
ptr = head;
// this function cancListas the first node found of this value

while (ptr != NULL && ptr->value != val) {
ptr = ptr->next;
}

if (ptr != NULL) {
// if ptr went to NULL, it went thru the list and didnt find value
// in that case, ignore. only do the cancLista
// when theres something to.
if (ptr == head) {
// if removing head node
head = head->next;
head->prev = NULL;
delete(ptr);
} else if (ptr == tail) {
// if removing tail node
tail = tail->prev;
tail->next = NULL;
delete(ptr);
} else {
// if removing in the middle
ptr->prev->next = ptr->next;
ptr->next->prev = ptr->prev;
delete(ptr);
}
}
}

void Lista::stampaLista() {
Nodo *ptr;
ptr = head;
while (ptr != NULL) {
cout<<ptr->value<<" ";
ptr = ptr->next;
}
cout<<endl;
}

void Lista::stampaRevList() {
Nodo *ptr;
ptr = tail;
while (ptr != NULL) {
cout<<ptr->value<<" ";
ptr = ptr->prev;
}
cout<<endl;
}


int main() {
Lista mylist;
int input;
int scelta;
char select;

do{
cout << "-------- MENU --------" << endl;
cout << "1. Inserisci valore nella lista." << endl;
cout << "2. Stampa la lista." << endl;
cout << "3. Rimuovi valore dalla lista." << endl;
cout << "4. Rovescia la pila." << endl;
cout << "5. Stampa il numero degli elementi." << endl;
cout << "8. Esci." << endl;
cout << ": ";
cin >> scelta;
switch(scelta){
case 1:
cout << "Inserisci un valore nella lista: ";
cin >> input;
mylist.inserisciLista(input);
cout << "Vuoi inserire un altro valore ? S/N: ";
cin >> select;
while(select != 'n' && select != 'N'){
cout << "Inserisci un valore nella lista: ";
cin >> input;
mylist.inserisciLista(input);
cout << "Vuoi inserire un altro valore ? S/N: ";
cin >> select;
}
break;

case 2:
cout << "Lista: " << endl;
mylist.stampaLista();
break;

case 3:
cout << "Inserisci valore da rimuovere: ";
cin >> input;
mylist.cancLista(input);
cout << "Vuoi rimuovere un altro valore ? S/N: ";
cin >> select;
while(select != 'n' && select != 'N'){
cout << "Inserisci valore da rimuovere: ";
cin >> input;
mylist.cancLista(input);
cout << "Vuoi rimuovere un altro valore ? S/N: ";
cin >> select;
}
break;

case 8:

break;

default:
cout << "!*!*!*!* SCELTa NON VALIDA *!*!*!*!" << endl <<
endl;
}
}
while(scelta != 8);

system("PAUSE");
return 0;
}

-----------------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top