Thread: input filestream to a doubly linked list

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    input filestream to a doubly linked list

    Coded
    Last edited by Undying Mirai; 06-26-2006 at 06:47 AM. Reason: Already done it

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    To append into a double linked list, you need to reset any links that change, that means the next link of the tail and the previous link of the new tail:
    Code:
    temp = new link;
    temp->next = 0;
    temp->prev = tail;
    tail->next = temp;
    tail = tail->next;
    To prepend, you basically do the same thing reversed:
    Code:
    temp = new link;
    temp->next = head;
    temp->prev = 0;
    head->prev = temp;
    head = temp;
    To insert, you have four links to fix:
    Code:
    temp = new link;
    temp->next = after;
    temp->prev = before;
    before->next = temp;
    after->prev = temp;
    Naturally, you don't always need unique pointers to each node that changes. A lot of the time you can use next or prev chains to avoid temporary variables:
    Code:
    tail->next = new link;
    tail->next->next = 0;
    tail->next->prev = tail;
    tail = tail->next;
    Code:
    head->prev = new link;
    head->prev->next = head;
    head->prev->prev = 0;
    head = temp;
    Code:
    temp = new link;
    temp->next = before->next;
    temp->prev = before;
    before->next->prev = temp;
    before->next = temp;
    
    // or
    
    temp = new link;
    temp->next = after;
    temp->prev = after->prev;
    after->prev->next = temp;
    after->prev = temp;
    p.s. Don't ever delete your post's content. Other people can learn from your question and any answers to it.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM