5 Replies - 637 Views - Last Post: 21 May 2010 - 07:26 PM Rate Topic: -----

#1 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Data structure code fail in output

Posted 21 May 2010 - 01:40 PM

      
      //insert in order list using dummynode & double linked list


#include<iostream>
using namespace std;

struct node
{
	node *next;
	node *prev;
	int data;
};
class List
{
	public:
		List();
		void insertInOrder(int);
		void insertAfter(node *prev,int);
		void print();

	private:
		node *head;
		node *tail;
		node *current;
};

List::List()
{
	head=new node;
	head->next=0;
	current =0;
	tail=0;
}

void List::insertAfter(node *prev,int elem)
{
	node* addednode= new node;
	addednode->data=elem;
	addednode->next=prev->next;
	prev->next=addednode;

	if(prev==tail)

		tail=addednode;

}
void List::insertInOrder(int elem)
{

	node* addednode= new node;
	addednode->data=elem;
	addednode->next=0;
	addednode->prev=0;
	node* prev=head;
	while(prev !=0 && prev->next !=0)
	{
		if(prev->next->data > elem)
		{
			insertAfter(prev,elem);
			break;
		}
		prev=prev->next;
	}
	if(prev==tail)
	  insertAfter(tail,elem);
}
void List::print()
{

	node* current=new node;
	current=head ;
	while(current->next!=0)
	{
		cout<<current->data<<"\n";
		current=current->next;
	}
}
int main()
{
	List l;
	l.insertInOrder(5);
	l.insertInOrder(6);
	l.insertInOrder(1);
	l.insertInOrder(2);
	l.print();
system("pause");
return 0;
}    

this code not give me any output and i can't determine where fault.

Is This A Good Question/Topic? 0
  • +

Replies To: Data structure code fail in output

#2 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: Data structure code fail in output

Posted 21 May 2010 - 02:31 PM

if anyone can tell me where i make fault
and i can correct it.
Was This Post Helpful? 0
  • +
  • -

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Data structure code fail in output

Posted 21 May 2010 - 02:37 PM

why would you need to allocate more memory when your just printing the data?

void List::print() 
{
        node* temp = head;
        while(temp->next!=NULL) 
        { 
                cout<<temp->data<<"\n"; 
                temp = temp->next;
        } 
} 


Was This Post Helpful? 0
  • +
  • -

#4 mon mon  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 128
  • Joined: 31-December 09

Re: Data structure code fail in output

Posted 21 May 2010 - 02:47 PM

View PostImaSexy, on 21 May 2010 - 01:37 PM, said:

why would you need to allocate more memory when your just printing the data?

void List::print() 
{
        node* temp = head;
        while(temp->next!=NULL) 
        { 
                cout<<temp->data<<"\n"; 
                temp = temp->next;
        } 
} 



this not make any change
i make this when i remove give me error

This post has been edited by mon mon: 21 May 2010 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 makneltek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 19-May 09

Re: Data structure code fail in output

Posted 21 May 2010 - 03:13 PM

This is quite easy to follow and debug, get a pencil and paper, and draw out how its suppposed to look like. and debug step by step. if something doesnt look like the drawing youve found your error.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Data structure code fail in output

Posted 21 May 2010 - 07:26 PM

Ill repeat myself, Why are you creating memory when you are just printing out the linked list???

void List::print()
{

        node* current=new node; //<---- HERE, WHY??
        current=head ;
        while(current->next!=0)
        {
                cout<<current->data<<"\n";
                current=current->next;
        }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1