Jump to content

Hello again guys, I was just given an assignment where I have to have a linear linked list of arrays implemented as classes instead of structs for the first time.  I am having trouble thinking of a way to implement this as I'm just used to struct node type of thing instead of classes. I have these classes right now:

Spoiler

class person//This is the common abstract base class
{
	public:
		person();
		~person();
		int copyPerson(const person & copyFrom);
		//Self Similar Funtions:
		virtual bool findStatus();
		virtual float findCost();
		virtual bool isBusy();
		virtual int lastRide();
		
	protected:
		char * name;
		//Other
};


class bus:public person
{
	public:
		bus();
		~bus();
		int copyBus();
		bool findStatus();
		float findCost();
		bool isBusy();
		int lastRide();

	protected:
		char * busName;
		float price;
};


class streetCar:public person
{
	public:
		streetCar();
		~streetCar();
		int copyCar(const streetCar & toAdd);
		bool findStatus();
		float findCost();
		bool isBusy();
		int lastRide();
	
	protected:
		float cost;

};


class Max:public person
{
	public:
		Max();
		~Max();
		int copyMax();
		int copyBus();
		bool findStatus();
		float findCost();
		bool isBusy();
		int lastRide();
	
	protected:
		float cost;
};

 

I am supposed to implement the data structure of a linear linked list of arrays,where the linked list 
nodes are a type of transport and the array is the persons most recent experience with that particular 
transportation method. I was thinking of having a class for the linked list and then create an array of objects from that but I have no idea how to turn that idea into code or if that is the best way to approach the problem, any help is much appreciated. 

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/737646-classes-as-linked-list-help/
Share on other sites

Link to post
Share on other sites

16 minutes ago, CJPowell27 said:

Hello again guys, I was just given an assignment where I have to have a linear linked list of arrays implemented as classes instead of structs for the first time.  I am having trouble thinking of a way to implement this as I'm just used to struct node type of thing instead of classes. I have these classes right now:

  Hide contents


class person//This is the common abstract base class
{
	public:
		person();
		~person();
		int copyPerson(const person & copyFrom);
		//Self Similar Funtions:
		virtual bool findStatus();
		virtual float findCost();
		virtual bool isBusy();
		virtual int lastRide();
		
	protected:
		char * name;
		//Other
};


class bus:public person
{
	public:
		bus();
		~bus();
		int copyBus();
		bool findStatus();
		float findCost();
		bool isBusy();
		int lastRide();

	protected:
		char * busName;
		float price;
};


class streetCar:public person
{
	public:
		streetCar();
		~streetCar();
		int copyCar(const streetCar & toAdd);
		bool findStatus();
		float findCost();
		bool isBusy();
		int lastRide();
	
	protected:
		float cost;

};


class Max:public person
{
	public:
		Max();
		~Max();
		int copyMax();
		int copyBus();
		bool findStatus();
		float findCost();
		bool isBusy();
		int lastRide();
	
	protected:
		float cost;
};

 

I am supposed to implement the data structure of a linear linked list of arrays,where the linked list 
nodes are a type of transport and the array is the persons most recent experience with that particular 
transportation method. I was thinking of having a class for the linked list and then create an array of objects from that but I have no idea how to turn that idea into code or if that is the best way to approach the problem, any help is much appreciated. 

So you need a linkedlist of array, where the array's are to be filled with T?

template <class T> 
class LinkedListElement {
	private:
  		T name[]; // If the interior lists are of known size.
        std::vector<T> name; // If the size is unknown.
  
  	protected:
  		LinkedListElement* next;
  		LinkedListElement* prev; // DoubleLinkedList now technically.
  
  	public:
  		LinkedListElement(T data) : (data);
  		// Getters and setters
}

// Make a LinkedList
LinkedListElement<SOMETHING-TYPE> root( SOMETHING );
root.add( SOMETHINGELSE ); // Dependent on your implementation. Either you first fill up the array, or you could "sort" based upon a key value (Kinda like a multimap). The key value would give you the LinkedListElement with an array. Then you just add it to an array.


Note: It's been 2 months since I last programmed in C++. This code may contain syntax errors and technical errors

This also uses functions and types from the C++11 standard library. (std::vector<>). Check with your teacher / assignment if C++11 is allowed. Also: No destructor provided here since it's also based on your specific implementation. Syntax for destructor: ~LinkedListElement() {... Delete things you allocated memory for ...}

 

 

More code: http://stackoverflow.com/questions/22141477/simple-linked-list-c

Just gotta change that 'int x' in that implementation to whatever type you want it to store.

Could be T if working with class templates, but could very well be your class 'person'. In the later case you should make this a pointer though, or inheritance will not apply. (Eg: Storing a Max object a in the LL. Retrieving that a would result in you getting a Person object. Performing a function on that will NOT make it so that C++ will look for the implementation in the Max class. It will bind to the function defined in Person.)

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to post
Share on other sites

22 minutes ago, CJPowell27 said:

Hello again guys, I was just given an assignment where I have to have a linear linked list of arrays implemented as classes instead of structs for the first time.  I am having trouble thinking of a way to implement this as I'm just used to struct node type of thing instead of classes. I have these classes right now:

  Reveal hidden contents


class person//This is the common abstract base class
{
	public:
		person();
		~person();
		int copyPerson(const person & copyFrom);
		//Self Similar Funtions:
		virtual bool findStatus();
		virtual float findCost();
		virtual bool isBusy();
		virtual int lastRide();
		
	protected:
		char * name;
		//Other
};


class bus:public person
{
	public:
		bus();
		~bus();
		int copyBus();
		bool findStatus();
		float findCost();
		bool isBusy();
		int lastRide();

	protected:
		char * busName;
		float price;
};


class streetCar:public person
{
	public:
		streetCar();
		~streetCar();
		int copyCar(const streetCar & toAdd);
		bool findStatus();
		float findCost();
		bool isBusy();
		int lastRide();
	
	protected:
		float cost;

};


class Max:public person
{
	public:
		Max();
		~Max();
		int copyMax();
		int copyBus();
		bool findStatus();
		float findCost();
		bool isBusy();
		int lastRide();
	
	protected:
		float cost;
};

 

I am supposed to implement the data structure of a linear linked list of arrays,where the linked list 
nodes are a type of transport and the array is the persons most recent experience with that particular 
transportation method. I was thinking of having a class for the linked list and then create an array of objects from that but I have no idea how to turn that idea into code or if that is the best way to approach the problem, any help is much appreciated. 

If you would like more help with the above code, feel free to quote / tag me. I check LTT forums about once or twice a day.

 

Qualifications

Education: University course on C, C++ and Objective C (12 weeks, last semester). With an advanced background in Java.

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to post
Share on other sites

13 minutes ago, MrKickkiller said:

If you would like more help with the above code, feel free to quote / tag me. I check LTT forums about once or twice a day.

 

Qualifications

Education: University course on C, C++ and Objective C (12 weeks, last semester). With an advanced background in Java.

I believe my teacher stated in her writeup that she did not want us to use vectors in this programming assignment unfortunately 

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to post
Share on other sites

2 minutes ago, CJPowell27 said:

I believe my teacher stated in her writeup that she did not want us to use vectors in this programming assignment unfortunately 

C++ is merely an extension to C. Many C methods are still available in C++. So you can fall back to the C way of just allocing a pointer of size N

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to post
Share on other sites

A struct and a class in C++ are very similar.

IIRC the only difference is that a classes members are private by default and structs members are public by default.

 

I guess your teacher wants you to use a more OO (Object Orientated) approach.

 

Here's a start:

template<typename T>
class LinkedList
{
public:
	LinkedList() : m_pHead(nullptr) { }

	void pushBack(T value)
	{
		m_pHead = new Element(m_pHead, value);
	}
private:
	Element* m_pHead;

	class Element
	{
		Element(Element* pNext, T item)
		{
			m_pNext = pNext;
			m_pPrev = nullptr;
			m_item = item;
		}

		Element* m_pNext, m_pPrev;
		T m_item;
	};
};

 

30 minutes ago, MrKickkiller said:

...

I would not store a vector/array inside the list element like that, it takes away abstraction.

Instead use std::vector<...> (or other type of collection) as template type:

LinkedList<std::vector<...>> list;

 

 

But I think your teacher wants you to use linked lists everywhere.

So that would be:

LinkedList<LinkedList<...>>

 

If you want to impress your teacher, look into iterators.

They're the C++ way of abstracting different types of containers.

 

And when you're done with this assignment, C++ includes linked lists: std::list

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to post
Share on other sites

3 minutes ago, mathijs727 said:

I would not store a vector/array inside the list element like that, it takes away abstraction.

Instead use std::vector<...> (or other type of collection) as template type:


LinkedList<std::vector<...>> list;

 

Our university teacher didn't have much of a problem with taking away abstraction.

But hey, for every problem, there are infinity solutions to the problem :)

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to post
Share on other sites

5 minutes ago, MrKickkiller said:

Our university teacher didn't have much of a problem with taking away abstraction.

But hey, for every problem, there are infinity solutions to the problem :)

I think "abstraction" is not the correct word, I should have used "generalization"

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to post
Share on other sites

17 minutes ago, MrKickkiller said:

Our university teacher didn't have much of a problem with taking away abstraction.

But hey, for every problem, there are infinity solutions to the problem :)

The main focus for our program is supposed to be dynamic binding and OOD

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to post
Share on other sites

3 minutes ago, CJPowell27 said:

The main focus for our program is supposed to be dynamic binding and OOD

Dynamic binding means using pointers to your element that you wanna store. \/ \/

 

52 minutes ago, MrKickkiller said:

In the later case you should make this a pointer though, or inheritance will not apply. (Eg: Storing a Max object a in the LL. Retrieving that a would result in you getting a Person object. Performing a function on that will NOT make it so that C++ will look for the implementation in the Max class. It will bind to the function defined in Person.)

OOD: Using classes and their advantages instead of the still valid structs

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×