Thread: Help with doubly linked list

  1. #1
    Registered User
    Join Date
    Apr 2008

    Help with doubly linked list

    OK, so im fairly new to pointers and Node types, etc... I'm trying to use a function to insert an item (first item) in a list, but my program crashes whenever i try to set the front pointer to that.

    I'm using a test driver that the first thing that does is call the function with the string "this is a test." So the list is empty before it calls the function.

    PS: don't mind that it says sorted... i also cut a lot of the functions out of the code to not take up space... and only put the one im having troubles with at the moment.

    template<class ItemType>
    struct NodeType
    	ItemType			info;
    	NodeType<ItemType>*	next;
    	NodeType<ItemType>*	back;
    template <class ItemType>
    class SortedType
    		SortedType();     // Class constructor	
    		SortedType(const SortedType& list);
    		// Constructor
    		// Destructor
    		const SortedType& operator=(const SortedType& rhs);
    		bool IsFull() const;
    		int  GetLength() const;
    		void MakeEmpty();
    		void RetrieveItem(ItemType& item, bool& found);
    		void ResetList();
    		void GetNextItem(ItemType&);
    		ItemType CurrentItem();
    		void Delete();
    		void InsertAfter(ItemType item);
    		void InsertBefore(ItemType item);
    		void GoToFront();
    		void GoToRear();
    		void GoForward();
    		void GoBack();
    		void	Copy(const SortedType& list);		
    		void	FindItem(ItemType item, NodeType<ItemType>*& location, bool& found);
    		NodeType<ItemType>* front;
    		NodeType<ItemType>* rear;
    		int					length;
    		NodeType<ItemType>* currentPos;
    template<class ItemType>
    SortedType<ItemType>::SortedType()  // Class constructor
    	length = 0;
    	front = NULL;
    	rear = NULL;
    	currentPos = NULL;
    template<class ItemType>
    void SortedType<ItemType>::InsertAfter(ItemType item)
    	NodeType<ItemType>* location;
    	location = new NodeType<ItemType>;
    	location->info = item;
    	if(currentPos == NULL)
    		currentPos = front;
    	if(currentPos == front)
    		location->next = NULL;
    		location->back = front;
    		front->next = location; //Right here is where it crashes.
    		front = location;
    		location->next = currentPos->next;
    		location->back = currentPos;
    		currentPos->next = location;

  2. #2
    The larch
    Join Date
    May 2006
    front is NULL at that point, and therefore you cannot access it's next pointer. For some reason you set front to a value after you try to use it. Also, shouldn't you do something about the rear member as well?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Thanks anon, i added an extra if statement for when the list is empty and it seems to be working now. Well except for some run time errors but that will get fixed later =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM