1 Replies - 3457 Views - Last Post: 04 March 2011 - 02:07 PM Rate Topic: -----

#1 nautica17   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Double Linked List Insertion

Posted 04 March 2011 - 10:58 AM

I have code for a single linked list that I am trying to modify into a double linked list. I'm working on the functions to insert a head node and to insert nodes in all other positions.
For the moment I've left the head insertion to work properly only if you do it once to start the linked list, (no link to previous node). Now for the normal insert function, it just doesn't seem to work. What I mean is it won't even link anything, which you can see using the print_list function. Any help much appreciated.

Edit: I'm using VC++ 2010.

main.cpp
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "node1.h"

void print_list(node* myNode)
{
	node* cursor = NULL;

	std::cout << "Data	Address	    Next	 Previous\n";

	for (cursor = myNode; cursor != NULL; cursor = cursor->next())
	{
		std::cout << cursor->data() << "       " << cursor << "    " << cursor->next() << "     " << cursor->prev() << "\n";
	}
}

int main()
{
	node* test = NULL;

	list_head_insert(test, 5);
	list_insert(test, 6);
	list_insert(test, 7);
	list_insert(test, 3);

	print_list(test);

	return EXIT_SUCCESS;
}




node1.h
#ifndef NODE1_H  
#define NODE1_H

#include <cstdlib>

    class node
    {
    public:
	// TYPEDEF
		typedef int value_type;
    
	// CONSTRUCTOR

		node(const value_type& init_data = value_type(), node* next_ptr = NULL, node* prev_ptr = NULL )
		{
			data_field = init_data;
			next_link = next_ptr;
			prev_link = prev_ptr;
		}

	// Member functions to set the data and link fields:
    		void set_data(const value_type& new_data) { data_field = new_data; }

		void set_next(node* new_link)             { link_field = next_link; }
	        void set_prev(node* new_link)             { link_field = prev_link; }

	// Constant member function to retrieve the current data:
		value_type data( ) const { return data_field; }

	// Two slightly different member functions each to retrieve
	// the current link:

		const node* next() const { return next_link; }
    	    node* next()		 { return next_link; }

		const node* prev() const { return prev_link; }
    	    node* prev()		 { return prev_link; }
	
    private:
		value_type data_field;
		node* link_field;

		node* next_link;
		node* prev_link;
    };

    // FUNCTIONS for the linked list
		void list_head_insert(node*& head_ptr, const node::value_type& entry); 
		void list_insert(node* previous_ptr, const node::value_type& entry);  


#endif




node1.cpp
#include "node1.h"
#include <iostream>
#include <cassert>
#include <cstdlib>

    void list_head_insert(node*& head_ptr, const node::value_type& entry)
	{
		head_ptr = new node(entry, head_ptr, NULL);		
	}
	
	void list_insert(node* current_node, const node::value_type& entry) 
	{
		if(current_node != NULL)
		{
			node *insert_ptr = new node( entry, current_node->next(), current_node );

			current_node->set_next(insert_ptr);

                // Commented code makes program get stuck. 
		//	node* tempNext = current_node->next();
		//	tempNext->set_prev(insert_ptr);
		}
	}


This post has been edited by nautica17: 04 March 2011 - 11:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Double Linked List Insertion

#2 nautica17   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 155
  • Joined: 28-August 09

Re: Double Linked List Insertion

Posted 04 March 2011 - 02:07 PM

Nevermind, I got it working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1