3 Replies - 1004 Views - Last Post: 30 September 2011 - 06:31 AM Rate Topic: -----

#1 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

[C++] Linked list class

Posted 29 September 2011 - 09:00 PM

Task:

Quote

You can describe a simple list as follows:
• The simple list can hold zero or more items of some particular type.
• You can create an empty list.
• You can add items to the list.
• You can determine whether the list is empty.
• You can determine whether the list is full.
• You can visit each item in the list and perform some action on it.
As you can see, this list really is simple; it doesn’t allow insertion or deletion, for
example.
Design a List class to represent this abstract type. You should provide a list.h header
file with the class declaration and a list.cpp file with the class method implementations.
You should also create a short program that utilizes your design.
The main reason for keeping the list specification simple is to simplify this programming
exercise. You can implement the list as an array or, if you’re familiar with the data type, as a linked list. But the public interface should not depend on your choice. That is, the
public interface should not have array indices, pointers to nodes, and so on. It should be
expressed in the general concepts of creating a list, adding an item to the list, and so on.
The usual way to handle visiting each item and performing an action is to use a function
that takes a function pointer as an argument:
void visit(void (*pf)(Item &));
Here pf points to a function (not a member function) that takes a reference to Item
argument, where Item is the type for items in the list. The visit() function applies this
function to each item in the list.


Class Definition:

List.h
class List
{
public:
	struct Node
	{
	int m_Data;
	Node *m_Next;
	}*Head;
	//Functions
	List();
	void Add(int Data);
	int Empty();
	int Full();
	void Action(void (*CustomFunction) (Node &)); //Uses function pointer.
};



Class declerations:

ListDef.cpp
#include "List.h"
#include <iostream>

List::List()
{
	Head = NULL;
}

void List::Add(int Data)
{
	//Adds a new node, between head and the first node.
	Node *NewNode; //Create a new pointer. Have to use pointer so it doesn't die after function quits.
	NewNode = new Node; //Allocate memory for a node.
	NewNode->m_Data = Data; //Make m_data point to the data.
	NewNode->m_Next = Head; //Set next to where head is currently pointing (the first node).
	Head = NewNode; //Make head point to the new node.
}

int List::Empty()
{
	if(Head == NULL)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

int List::Full()
{
	return 0;
}

void List::Action(void (*CustomFunction) (Node &))
{
	Node *Current = Head;

	while(Current != NULL)
	{
		(*CustomFunction)(*Current); //Function Pointer being called.

		Current = Current->m_Next;
	}
}



Main.cpp - To test the class.

#include "List.h"
#include <iostream>

void ChangeFunction(List.Node & Item);

int main(void)
{
	List NewList;

	if(NewList.Empty())
	{
		std::cout << "Empty!\n";
	}

	NewList.Add(25);

	if(NewList.Empty())
	{
		std::cout << "Empty!\n";
	}
	else
	{
		std::cout << "Not empty!\n";
	}

	NewList.Action(&ChangeFunction);

	return 0;
}

void ChangeFunction(List.Node & Item)
{
	Item.m_Data += 5;
}



Errors:
1>  Main.cpp
1>c:\users\matt\documents\visual studio 2010\projects\c++\other\simplelist\simplelist\main.cpp(3): error C2143: syntax error : missing ')' before '.'
1>c:\users\matt\documents\visual studio 2010\projects\c++\other\simplelist\simplelist\main.cpp(3): error C3484: syntax error: expected '->' before the return type
1>c:\users\matt\documents\visual studio 2010\projects\c++\other\simplelist\simplelist\main.cpp(3): error C2061: syntax error : identifier 'Node'
1>c:\users\matt\documents\visual studio 2010\projects\c++\other\simplelist\simplelist\main.cpp(25): error C2065: 'ChangeFunction' : undeclared identifier
1>c:\users\matt\documents\visual studio 2010\projects\c++\other\simplelist\simplelist\main.cpp(30): error C2143: syntax error : missing ')' before '.'
1>c:\users\matt\documents\visual studio 2010\projects\c++\other\simplelist\simplelist\main.cpp(30): error C3484: syntax error: expected '->' before the return type
1>c:\users\matt\documents\visual studio 2010\projects\c++\other\simplelist\simplelist\main.cpp(30): error C2061: syntax error : identifier 'Node'
1>c:\users\matt\documents\visual studio 2010\projects\c++\other\simplelist\simplelist\main.cpp(31): error C2143: syntax error : missing ';' before '{'
1>c:\users\matt\documents\visual studio 2010\projects\c++\other\simplelist\simplelist\main.cpp(31): error C2447: '{' : missing function header (old-style formal list?)



It doesn't seem to like my ChangeFunction function? :(

This post has been edited by Matty919: 29 September 2011 - 09:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: [C++] Linked list class

#2 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: [C++] Linked list class

Posted 29 September 2011 - 10:18 PM

In the parameters of ChangeFunction, what you mean is
 void ChangeFunction(List::Node & Item) 
.
Was This Post Helpful? 1
  • +
  • -

#3 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: [C++] Linked list class

Posted 29 September 2011 - 10:48 PM

View Postnuclearfroggy, on 29 September 2011 - 11:18 PM, said:

In the parameters of ChangeFunction, what you mean is
 void ChangeFunction(List::Node & Item) 
.


Thank you very much. That works now! :D Any criticism or suggestions to improve my code are still welcome.

This post has been edited by Matty919: 29 September 2011 - 10:49 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,384
  • Joined: 25-December 09

Re: [C++] Linked list class

Posted 30 September 2011 - 06:31 AM

I would make your class data private. Also insure that you have include guards in your header file.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1