Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > doubly linked list

Reply
Thread Tools

doubly linked list

 
 
Defected
Guest
Posts: n/a
 
      02-25-2008
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 != ;

system("PAUSE");
return 0;
}

-----------------------------------------------------------------------------------
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
doubly linked list murali@pune Java 3 03-24-2006 09:30 AM
Warning when doubly linked list is defined gloablly chand Python 7 09-05-2005 07:28 PM
need for doubly linked list dssuresh6 C Programming 4 11-19-2004 03:22 AM
What's wrong in my function of reverse the doubly linked list with dummy node. Daniel C Programming 5 10-27-2004 10:16 PM
Does any one have heap sorting with doubly linked list in C? darth C Programming 0 04-30-2004 11:51 AM



Advertisments