doubly linked list

Discussion in 'C++' started by Defected, Feb 25, 2008.

  1. Defected

    Defected Guest

    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;
    }

    -----------------------------------------------------------------------------------
    Defected, Feb 25, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. murali@pune

    doubly linked list

    murali@pune, Mar 23, 2006, in forum: Java
    Replies:
    3
    Views:
    913
    bugbear
    Mar 24, 2006
  2. darth
    Replies:
    0
    Views:
    446
    darth
    Apr 30, 2004
  3. chand
    Replies:
    7
    Views:
    311
    Terry Reedy
    Sep 5, 2005
  4. Daniel
    Replies:
    5
    Views:
    379
  5. dssuresh6

    need for doubly linked list

    dssuresh6, Nov 18, 2004, in forum: C Programming
    Replies:
    4
    Views:
    611
    J. J. Farrell
    Nov 19, 2004
Loading...

Share This Page