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:
[syntax="cpp"]
#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;
}
[/syntax]
