Need help understanding this question

S1ght

Expert Member
Joined
Jan 23, 2006
Messages
3,301
Reaction score
27
Location
Berlin
Hi,

K we get our weekly pracs to do in c++ but the question is a little vague and confusing me :o

"Create a Node Struct which contains EmployeeDetails member.

Create a singly linked List class based on this node data type.

Use the List class to implement both a Stack and Queue data structure.

All these data structures should adhere to the principles of modular object orientated programming and abstract data types known to you.

Create a suitable main program to demonstrate your classes."

Now I'm confused that they say only 1 class in the beginning and then "classes" at the end, I dunno if they want 1 or 3 classes

Maybe I'm just missing something really obvious...

Thanx in advance
S1ght
 
My guess would be that they want:

A node class that contains 1 piece of info and comes with a read+write function. Each node must have 2 pointers, one pointing forward, one pointing backwards. A List class which utilizes the read+write functions of the node class to implement adding/deleting of nodes. The List classes uses the forward/backward pointers to implement LIFO and FIFO.
 
So kinda like have all the functions needed for every data structure in the class and then just make an object of class List and just use the functions accordingly to use whatever data structure?
 
Wow, where are you studying? That sounds suspiciously like the subject "Data Structures in C++" I had at varsity... :p I might even have a prac-solution that's almost identical to that!
 
My guess would be that they want:

Each node must have 2 pointers, one pointing forward, one pointing backwards. .

a "singly linked List class" only has one pointer forward, I'm not sure how anal the teacher will be, he may lose marks for using a doubly link list.
 
Hi,

K we get our weekly pracs to do in c++ but the question is a little vague and confusing me :o

"Create a Node Struct which contains EmployeeDetails member.

Create a singly linked List class based on this node data type.

Use the List class to implement both a Stack and Queue data structure.

All these data structures should adhere to the principles of modular object orientated programming and abstract data types known to you.

Create a suitable main program to demonstrate your classes."

Now I'm confused that they say only 1 class in the beginning and then "classes" at the end, I dunno if they want 1 or 3 classes

Maybe I'm just missing something really obvious...

Thanx in advance
S1ght

I'd think you'd need:

an EmployeeDetails class, the list class and 2 other control class, one to demonstrate using the list as a stack and the other a queue.

That make any sense?
 
struct Node
{
public:
string EmployeeDetails;
Node *Next;
}

then create the list container doing the list logic..
then inherit from the list and override the push and pop methods accordingly or use composition.
 
I implemented a stack that is very similar to what you have to do except I only have a push operation where you'd want 2 insert operations, namely insertbefore and insertafter for inserting before and after the head to enable it to be used for a queue and a stack.

I wrote it for a quick test a while back so it has no error checking, throw's no exceptions, etc. so you can just use it as example.

It compiles in both Visual Studio 2005 and Borland C++ Builder (the command line tools 9mb download)

Also if you find bugs please remember this was a 10 minute project so I didn't spend much time testing or looking at the code :D

main.cpp
Code:
#include <iostream>
#include <string>
#include "MyStack.cpp"
using namespace std;

int main()
{
	MyStack<std::string> tmp = MyStack<std::string>();
	tmp.push("5");
	tmp.push("4");
	tmp.push("3");
	tmp.push("2");
	tmp.push("1");
	std::cout << tmp.pop() << std::endl;
	std::cout << tmp.pop() << std::endl;
	std::cout << tmp.pop() << std::endl;
	std::cout << tmp.pop() << std::endl;
	std::cout << tmp.pop() << std::endl;
	return 0;
}

MyStack.cpp
Code:
#include "MyStack.h"

template <class AnyType>
MyStack<AnyType>::MyStack()
{
	this->head = 0;
	this->size = 0;
}

template <class AnyType>
MyStack<AnyType>::~MyStack()
{
	this->clear();
}

template <class AnyType>
void MyStack<AnyType>::clear()
{
	while(this->head != 0)
	{
		Node<AnyType> *tmp = this->head;
		this->head = this->head->next;
		delete tmp;
	}

	//if (this->size != 0) throw new Exception
	this->size = 0;
}

template <class AnyType>
unsigned int MyStack<AnyType>::getSize()
{
	return this->size;
}

template <class AnyType>
void MyStack<AnyType>::push(AnyType _data)
{
	if (this->size == 0)
	{
		this->head = new Node<AnyType>();
		this->head->next = 0;
	}
	else
	{
		Node<AnyType> *tmp = new Node<AnyType>();
		tmp->next = this->head;
		this->head = tmp;
	}

		this->head->nodeData = _data;
		this->size++;
}

template <class AnyType>
AnyType MyStack<AnyType>::peek()
{
	if (this->size != 0)
	{
		return this->head->nodeData;
	}
	else
	{
		//there should actually be some kind of exception thrown in this case
		//but this is just a example.
		return 0;
	}
}

template <class AnyType>
AnyType MyStack<AnyType>::pop()
{
	if (this->size != 0)
	{
		AnyType returnData = this->head->nodeData;
		Node<AnyType> *currentHead = this->head;
		this->head = this->head->next;
		delete currentHead;
		this->size--;
		return returnData;
	}
	else
	{
		//see MyStack<AnyType>::peek()
		return 0;
	}
}

MyStack.h
Code:
template <class T>
struct Node
{
	T nodeData;
	Node *next;
};

template <class AnyType>
class MyStack
{
public:
	MyStack();
	virtual ~MyStack();
	virtual void clear();
	virtual unsigned int getSize();
	virtual void push(AnyType _data);
	virtual AnyType pop();
	virtual AnyType peek();

private:
	unsigned int size;
	Node<AnyType> *head;

};
 
a "singly linked List class" only has one pointer forward, I'm not sure how anal the teacher will be, he may lose marks for using a doubly link list.
Well then either LIFO or FIFO is going to be mad inefficient...but your right in that if the teacher wants that then thats the way it is.
 
Well then either LIFO or FIFO is going to be mad inefficient...but your right in that if the teacher wants that then thats the way it is.

Nah you can get past that by inserting before the current head node or after the current head node in the linkedlist.

EDIT: Err, nevermind, I wasn't thinking, you're right, the guy who set up this prac wasn't thinking properly, like me 5 minutes ago :p
 
Last edited:
Woah, forgot about this thread :) thanx for the help though, managed to improvise a lot and got it done. In the end they wanted a List Class and then 2 other classes but in those classes they had a private variable "l" of the class List so composition...
 
Top
Sign up to the MyBroadband newsletter
X