Jump to content
  • Advertisement
Sign in to follow this  
BaldPython

Untitled

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am studying Data Structures and Algorithms, and I've been ordained by the instructor to make a recursive LinkList ADT. Well, I am almost there but still having certain problems, like I am inserting digits from 0 to 10 in the list and i am getting an extra zero at the end of traversal() function. Can anyone please help me out, here's the code:

#include<iostream>
#include<cstdlib>
#include<cstring>

typedef struct node
{
	int 	data;
	node*	next;
}*List;

void initialize( List X )
{
	X->data = 0;
	X->next = NULL;
}

void initialize( List X , List Y )
{
	X->data = Y->data;
	X->next = Y->next;
}

int count( List X )
{
	if( X == 0 ) return 0;
	return ( 1 + count( X->next ));
}

void traverse( List L )
{
	if( L == 0 )
		return;
	else
		traverse( L->next );
		std::cout << L->data << std::ends ;
}

void traverse_reverse( List L )
{
	if( L->next == 0 ) return;
	std::cout << L->data << std::ends ;
	traverse( L->next );
}

void insert( List L , int value )
{
	List Temp = new node;
	if( Temp == NULL )
		std::cerr << "Out of Space" << std::endl;
	if( L == NULL )
		L = new node;
		
	Temp->data	= value;
	Temp->next	= L->next;
	L->next		= Temp;
}

void remove( List& L , int value )
{
	while( L != 0 && L->data == value )
	{
		List Tmp = L;
		L	 = L->next;
		delete[] Tmp;
	}
	
	if( L != 0 ) remove( L->next , value );
}

int rFind( List L , int value )
{
	if( L->data != value || L->next == NULL ) return 0;
	else
	return 1 + rFind( L->next, value );
}

int main()
{
	List x = new node;
	initialize(x);
	for ( int i=1; i<=10;i++ )
		insert(x,i);
	std::cout << count(x) << std::endl;
	traverse(x);
	std::cout << std::endl;	
	traverse_reverse(x);
	std::cout << std::endl;	
	std::cout << rFind(x,11);
	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Think carefully about your insert() function and what it does when L is null.


Also, it isn't the source of your problem, but just an FYI: in your traverse() function your else clause is not formatted correctly. If an else block has multiple lines, they need to be surrounded by braces (the { and } characters). The code will work as it is currently, but only by pure luck.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!