Thread: Double linked list

  1. #1
    Unregistered
    Guest

    Double linked list

    Can someone help me turn this little program I wrote (with the assistance of someone else on this board, thanks) into a program that creates a doubly linked list instead of singly? Thanks

    #include <iostream.h>
    #include <stddef.h>
    #include <fstream.h>

    struct Node
    {
    int ID;
    Node *Next;
    };

    void createlistKB(Node * &First);
    void createlistFL(Node * &First);
    void deletelistelement(Node* &First);
    void printlist(Node * &First);
    void InsertNode(Node* &First, Node* &toInsert);
    int DeleteNode(Node* &First, Node* &toDelete);
    int menu();




    void InsertNode(Node* &First, Node* &toInsert)
    {
    Node* Iterator = First;


    while(toInsert->ID>Iterator->ID&& Iterator->Next!=NULL)
    Iterator=Iterator->Next;

    if(toInsert->ID>Iterator->ID)
    {
    Node* Temp = Iterator->Next;
    Iterator->Next=toInsert;
    toInsert->Next=Temp;
    }
    else
    {
    if(Iterator==First)
    {
    Node* Temp = First;
    First=toInsert;
    toInsert->Next=Temp;
    return;
    }

    Node* Prev = First;

    while(Prev->Next!=Iterator)
    Prev=Prev->Next;

    Prev->Next=toInsert;
    toInsert->Next=Iterator;
    }


    }

    int DeleteNode(Node* &First, Node* &toDelete)
    {
    Node* Iterator = First;
    Node* DelayedIterator = Iterator;
    int found=0;

    if(Iterator->ID==toDelete->ID)
    First=First->Next;
    while(Iterator!=NULL)
    {
    if(Iterator->ID==toDelete->ID)
    {

    found=1;
    DelayedIterator->Next=Iterator->Next;
    }

    DelayedIterator=Iterator;
    Iterator=Iterator->Next;
    }
    return found;
    }

    void deletelistelement(Node* &First)
    {
    Node* toDelete;
    toDelete = new Node;
    int IDtoDelete, found=0;
    toDelete->Next=NULL;
    cout << "Enter ID to be deleted (-999 to cancel): ";
    cin >> IDtoDelete;
    if(IDtoDelete==-999)
    {
    cout << endl;
    return;
    }
    toDelete->ID=IDtoDelete;
    found = DeleteNode(First, toDelete);
    if(found==0)
    cout << "ID not located\n\n";
    if(found==1)
    cout << "ID deleted\n\n";
    }


    void createlistKB(Node * &First)
    {

    First = new Node;
    First->Next=NULL;
    int potentialfirst;
    cout << "Enter ID (-999 to quit): ";
    cin >> potentialfirst;
    if(potentialfirst==-999)
    {
    cout << "List was not created!";
    return;
    }
    First->ID=potentialfirst;
    Node* Temp;

    for(;
    {
    int input=0;
    cout << "Enter ID (-999 to quit): ";
    cin>>input;
    if(input==-999)break;

    Temp = new Node;
    Temp->ID=input;
    Temp->Next=NULL;

    InsertNode(First,Temp);
    }
    cout << endl;
    }

    void createlistFL(Node * &First)
    {

    First = new Node;
    First->Next=NULL;
    char filename[20];
    cout << "File name: ";
    cin >> filename;
    ifstream file(filename);
    if(!file.is_open())
    {
    cout << "ERROR: File not found!\n";
    return;
    }
    file >> First->ID;
    Node* Temp;

    for(;
    {
    int input=0;
    file>>input;
    if(file.eof())break;

    Temp = new Node;
    Temp->ID=input;
    Temp->Next=NULL;

    InsertNode(First,Temp);
    }
    cout << "File succesfully read!\n\n";
    file.close();
    }

    void printlist(Node * &First)
    {
    cout << "Here is your list!\n";
    cout << "------------------\n";
    Node * Temp;
    Temp = First;
    while(Temp!=NULL)
    {
    cout << Temp->ID << '\n';
    Temp = Temp->Next;
    }
    cout << "THANK YOU!!!\n\n";
    }

    int menu(int modeflag)
    {
    int Choice;
    if(modeflag==1)
    cout << "Linked List Assignment\n";
    if(modeflag==2)
    cout << "Invalid Choice\n";
    if(modeflag==3)
    cout << "Make Selection\n";
    cout << "-------------------------------\n";
    cout << "1) Create list: Key board input\n";
    cout << "2) Create list: File input\n";
    cout << "3) Delete an element\n";
    cout << "4) Print List\n";
    cout << "5) Quit\n";
    cout << "-------------------------------\n";
    cin >> Choice;
    cout << '\n';
    return Choice;
    }

    void main()
    {
    Node *First;
    First = new Node;
    int modeflag=1;
    First->ID=-999;
    int Choice = 0;
    do
    {
    Choice = menu(modeflag);
    if(modeflag==1)
    modeflag=0;
    if(Choice == 1)
    {
    modeflag=3;
    createlistKB(* &First);
    }
    if(Choice == 2)
    {
    modeflag=3;
    createlistFL(* &First);
    }
    if(Choice == 3)
    if(First->ID==-999)
    {
    modeflag=2;
    cout << "List does not exist.\nPlease create a list first.\n\n";
    }
    else
    {
    modeflag=3;
    deletelistelement(* &First);
    }
    if (Choice == 4)
    if(First->ID==-999)
    {
    modeflag=2;
    cout << "List does not exist.\nPlease create a list first.\n\n";
    }
    else
    {
    modeflag=3;
    printlist(* &First);
    }
    }while(Choice!=5);
    cout << "Thank you!\n";
    }




    sorry about the lack of indentions, I don't know how to write it on here so that it doesn't trash the indentions when I post to the board. Thanks for your help.

    This compiles with zero problems in Visual C++ 6.0, and Codewarrior (aka: Satan)

  2. #2
    Unregistered
    Guest
    Unfortunately I don't have time to redo your program for you, so i'll just give you a basic doubly linked list template.
    Code:
    #include <iostream.h>
    
    class Node{
      int data;
      Node *next;
      Node *prev;
    };
    
    int main(){
      Node root = new Node;
      Node counter = new Node;
      root->data=0;
      root->next=NULL;
      root->prev=NULL;
      counter=root;
      for(int i=1; i<10; i++){  //create the list
        counter->next = new Node;
        counter->next->data=i;
        counter->next->next = NULL;
        counter->next->prev = counter;
        counter=counter->next;
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked List Problem
    By Shiggins in forum C++ Programming
    Replies: 4
    Last Post: 03-10-2009, 07:15 AM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM