hi
i make code for double linkedlist..
but i didn't have a syntax erroe
.. i don't know where the error .

#include <iostream.h>
struct dnode
{
	int info;
	dnode *next;
	dnode *prev;
};

class dlist {
	private :
	dnode *head;
	dnode *tail;
	dnode *current;	
	
	public :
	void addEnd(int y){
		dnode *q;
		current=new dnode;
		
		if(head==NULL)
			current->prev=current->next=NULL;
	
		else{
			for (q=head; q->next!=NULL;q=q->next)
				
				q->next=current;
			current->prev=q;
			current->next=NULL;
			tail=q;
			
		} 
}

void forwward_Traversal(){
	if(head==NULL)
		 cout<<"the list has no node.";
	
	else {
		dnode *p=head;
		while(p){
			 cout<< p->info ;
			p=p->next; 
		}
	}
}	



void reverce_Traversal(){
	if(head==NULL)
	 cout<<"the list has no node.";
	else {
		dnode *p=tail;
		while(p){
			 cout<< p->info ;
			p=p->prev; 
		}
	}
	
}		   

};

int main () {
	dlist obj1;
	int num;
	int s;		
	 cout << "Enter the size : \n";
     cin>>s;
	
 cout << "Enter the NUMBER : \n";
	for(int i=0;i<s;i++){
	 cin>> num;
		obj1.addEnd(num);
	}
	
	obj1.forwward_Traversal();
	obj1.reverce_Traversal();
	return 0;
	
}

Whenever you're having an error you can't track down, the first thing I would suggest is to remove all user input and hardcode values that you know the correct behavior of. You should then try using a debugger to step through the code while watching the variables to see if you can tell where something is going wrong. Finally, if you can't find it, tell us the expected output and the current output for the simple case that you have hard coded.

Also, please use code tags.

David

the output of mycode should insert vslue at end of linkelist
and print the linkedlist forward and backward .

also shoud dalate the tail ,, and delete a node with specific position
and delete node with specific value ..


.. *_*

In addEnd() assign address for the new node to q, not current.

Be sure to assign y to q and to make each pointer in q NULL before adding it to tail.

If head == NULL then assign q to head and q to tail and you're done for that entry.

If head != Null then assign q to tail->next and assign tail to q->prev and then assign tail->next to tail, thereby making q the new tail.

Since you are keeping track of tail and you will always add to the end of tail there is no need for current or for a loop in addEnd().

Edited 5 Years Ago by Lerner: n/a

This article has been dead for over six months. Start a new discussion instead.