0

Don't think you can pass any parameter unless you create a point in your main.

template<class T>
	void my_list<T>::show_sllist(my_node<T>* p)
	{
		if(p!=0)
		{
			cout<<p->data;
			show_sllist(p->next);
		}
         }

Would i have to create a pointer of type my_node in main to show the list.

#include <iostream>
using namespace std;

#include "my_list.h"
#include "my_list.cpp"
using list::my_list;

int main()
{
	//Test 1
	cout<<"TEST1\n";
	my_list<double> a;
	a.push_back(1.3);
	a.push_back(2.4);
	cout<<"Object1:\n";
	a.show_sllist();
	my_list<double> b(a);
	cout<<"Object2:\n";
	b.show_sllist();
	cout<<endl;

	//Test 2
	/*cout<<"TEST2\n";
	my_list<int> c;
	c.push_back(1);
	c.push_back(2);
	cout<<"Object1:\n";
	c.show_list();
	my_list<int> d(c);
	cout<<"Object2:\n";
	d.show_list();
	my_list <int>e;
	e=d;
	e.push_back(10);
	cout<<"Object3:\n";
	e.show_list();*/
	return 0;
}
2
Contributors
9
Replies
12
Views
7 Years
Discussion Span
Last Post by dkalita
0
template<class T>
	void my_list<T>::show_sllist(my_node<T>* p)
	{
		
		if(p!=0)
		{
			cout<<p->data;
			show_sllist(p->next);
		}
		
		/*my_node<T>* temp=head;
		if(head==0&&tail==0)
		{
			cout<<"nothing to show\n";
		}
		else
		{
			while(temp->next!=0)
			{
				cout<<temp->data<<"\n";
				temp=temp->next;		
			}
			cout<<temp->data<<endl;
		}*/
	}

head can't be accessed in main because it is a private variable :(.

main

#include <iostream>
using namespace std;

#include "my_list.h"
#include "my_list.cpp"
using list::my_list;

int main()
{
	//Test 1
	cout<<"TEST1\n";
	my_list<double> a;
	a.push_back(1.3);
	a.push_back(2.4);
	cout<<"Object1:\n";
	a.show_sllist(a.head);
	//my_list<double> b(a);
	//cout<<"Object2:\n";
	//b.show_sllist();
	//cout<<endl;

Edited by lotrsimp12345: n/a

0

how do i do that? and their is still something wrong with the code. :(.

Edited by lotrsimp12345: n/a

0

how do i do that? and their is still something wrong with the code. :(.

that answer was for your last post before you edited that.

Write a third function without any argument which calls your recursive function inside it passing the head.
Call this third function from main().

0

my teacher doesn't want us to do that. He only wants one function to do all of it. No helper functions.

0

This is what he wants me to do.

Consider a singly-linked list sequence container of data items of type DT (named template class my_list). The
operations should include constructors, copy constructor, destructor, overloaded assignment operator (=), size,
push_back, pop_back, push_front, pop_front, (recursive) reverse and (recursive) show_sllist.

0

there is no way other than that or making the head public.

U can write the third function with the same name without having any parameters.

0

that's what i thought. Its crazy i don't think it is possible without helper functions and you probably want to keep head private.

0

that's what i thought. Its crazy i don't think it is possible without helper functions and you probably want to keep head private.

offcourse U shouldn't make head public because its of no meaning to others and it should not get modified from outside the class.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.