Click here to Skip to main content
15,913,152 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: What is the datatype for "this" pointer in C++ Pin
Emilio Garavaglia23-Feb-10 21:14
Emilio Garavaglia23-Feb-10 21:14 
AnswerRe: What is the datatype for "this" pointer in C++ Pin
Avi Berger23-Feb-10 21:31
Avi Berger23-Feb-10 21:31 
QuestionRe: What is the datatype for "this" pointer in C++ Pin
Avi Berger24-Feb-10 5:03
Avi Berger24-Feb-10 5:03 
AnswerRe: What is the datatype for "this" pointer in C++ Pin
KingsGambit23-Feb-10 22:43
KingsGambit23-Feb-10 22:43 
Questionasynchronouse thread function Pin
hrishi32123-Feb-10 18:14
hrishi32123-Feb-10 18:14 
AnswerRe: asynchronouse thread function Pin
CPallini23-Feb-10 20:30
mveCPallini23-Feb-10 20:30 
AnswerRe: asynchronouse thread function Pin
Rajesh R Subramanian23-Feb-10 20:40
professionalRajesh R Subramanian23-Feb-10 20:40 
QuestionNeed to Implement some functions using Linked List in C++ Pin
Shah Ravi23-Feb-10 17:49
Shah Ravi23-Feb-10 17:49 
Hi!
I have class called CSortedList. I need to implement their member functions using Linked List. I've tried implementing some functions. I am totally lost about the linked list. Can someone please help me out, it would be greatly appreciated. Please point out if I am doing something wrong. I have two files which I am going to post. The first file is CSortedListLLStarterKitSpring2010.cpp and the second file would be CSortedList.h

//This is a driver program to test the list ADT "CSortedList." It creates an 
//empty list by declaring a local variable of type CSortedList. Then a loop
//is entered that allows the user to manipulate the list until the user wishes 
//to quit.


#include	<iostream>
#include	<cstdlib>
#include	"CSortedList.h"

using namespace std;

//function prototype;

void	DisplayMenu(void);
void	DisplayList(const CSortedList &list);
void	FlushInstream(istream &inStream = cin);


int main(void)
{
	auto	bool			bLoop = true;
	auto	char			selection;
	auto	CSortedList		myList;
	auto	int				intVal;

	// let the user manipulate the list
	do 
	{
		DisplayMenu();
		cout << "Please enter a selection: ";

		cin >> selection;
		switch (toupper(selection))
		{
		case	'A':
			cout << "Please enter a value to add to the list: ";
			if (!(cin >> intVal))
			{
				cout << "Error reading int, please try again...\n";
				FlushInstream();
			}
			else
			{
				myList.InsertItem(intVal);
			}
			break;

		case	'R':
			cout << "Please enter a value to remove from the list: ";
			if (!(cin >> intVal))
			{
				cout << "Error reading int, please try again...\n";
				FlushInstream();
			}
			else
			{
				myList.RemoveItem(intVal);
			}
			break;

		case	'D':
			cout << "Here is the current list:\n";
			DisplayList(myList);
			cout << endl;
			break;

		case	'Q':
			bLoop = false;
			break;

		default:
			cout << "Unrecognized selection; please try again...\n";
			FlushInstream();
			break;
		}

		cout << endl;

	} while (true == bLoop);

	return 0;
}	//end of "main"

//DisplayMenu

void	DisplayMenu(void)
{
	cout << "A)dd an item\n";
	cout << "R)emove an item\n";
	cout << "D)isplay the list\n";
	cout << "Q)uit\n";
}

//DisplayList

void	DisplayList(const CSortedList &list)
{
	auto	int					index;
	auto	ListItemType		listname;

	for (index = 0; index < list.GetNumItems(); ++index)
	{
		list.GetListItem(index, listItem);
		cout << listItem << endl;
	}
}

//FlushInstream

void FlushInstream(istream &inStream)
{
	auto	int			inChar;

	inStream.clear();
	while (false == inStream.eof())
	{
		inStream.get(inChar);
		if ('\n' == inChar)
		{
			break;
		}
	}
}



//CSortedList.h
// I need to write all the functions using Linked List, NOT Dynamic Array.

#ifndef CSORTED_LINKED_LIST_HEADER
#define CSORTED_LINKED_LIST_HEADER

//enable this #define symbol to see debug output
#define DEBUG_LIST

//can easily use other basic type
typedef	int			ListItemType;

//structure for linked list nodes
struct ListItemNode
{
	ListItemType	value;
	ListItemNode	*next;
};

enum ListRetVal
{
	LIST_FULL
	, LIST_EMPTY
	,LIST_ERROR
	,LIST_SUCCESS
	,LIST_INVALID_INDEX
	,LIST_DUPLICATE
	,LIST_NOT_IN_LIST
};

class CSortedList
{
public:
	CSortedList(void) { m_headPtr = NULL; m_numItems = 0; } // constructure, don't need to implement function
	CSortedList(const CSortedList &object); // need to implement this function using linked list
	~CSortedList(void) { DestroyList(); } // don't need to implement function

	//member functions
	ListRetVal		CreateList(void); // implement this function to create a new list
	void			DestroyList(void); // implement this function to destroy the list
	void			DispRetVal(const char* szMessage, ListRetVal value) const; // i tried to write this function
	ListRetVal		GetListItem(int index, ListItemType &item) const; // i think this function returns the m_numItem
	int			GetNumItems(void) const { return (m_numItems); } // don't need to implement this function
	ListRetVal		InsertItem(const ListItemType &newItem); // Insert new items 
	bool			IsListFull(void) const { return (NULL == m_headPtr); } // don't need to implement this function
	bool			RemoveItem(const ListItemType &targetItem); // remove items from the list

	//overload operators
	CSortedList&	operator = (const CSortedList &rhs); // i have no idea what to do with this function

private:
	int				CopyList(const CSortedList &otherList); // i think this is going to copy the list

	ListItemNode	*m_headPtr;
	int				m_numItems;
};

#endif //CSORTED_LINKED_LIST_HEADER


void			DispRetVal(const char* szMessage, ListRetVal value) const
{
	#ifdef DEBUG_LIST
	cerr << szMessage << ": ";
	switch (value)
	{
	case LIST_Full:
		cerr << "LIST_FULL\n";
		break;

	case LIST_EMPTY:
		cerr << "LIST_EMPTY\n";
		break;

	case LIST_ERROR:
		cerr << "LIST_ERROR\n";
		break;

	case LIST_SUCCESS:
		cerr << "LIST_SUCCESS\n";
		break;

	case LIST_INVALID_INDEX:
		cerr << "LIST_INVALID_INDEX\n";
		break;

	default:
		cerr << "Unrecognized error code\n";
		break;
	}
#endif
}

AnswerRe: Need to Implement some functions using Linked List in C++ Pin
«_Superman_»23-Feb-10 17:59
professional«_Superman_»23-Feb-10 17:59 
GeneralRe: Need to Implement some functions using Linked List in C++ Pin
Shah Ravi23-Feb-10 18:03
Shah Ravi23-Feb-10 18:03 
AnswerRe: Need to Implement some functions using Linked List in C++ Pin
Avi Berger23-Feb-10 19:06
Avi Berger23-Feb-10 19:06 
QuestionPROBLEM WITH MATRIX OPENGL Pin
tuan111123-Feb-10 15:37
tuan111123-Feb-10 15:37 
AnswerRe: PROBLEM WITH MATRIX OPENGL Pin
Saurabh.Garg23-Feb-10 17:19
Saurabh.Garg23-Feb-10 17:19 
AnswerRe: PROBLEM WITH MATRIX OPENGL Pin
Tim Craig23-Feb-10 18:29
Tim Craig23-Feb-10 18:29 
GeneralRe: PROBLEM WITH MATRIX OPENGL Pin
LunaticFringe23-Feb-10 18:55
LunaticFringe23-Feb-10 18:55 
GeneralRe: PROBLEM WITH MATRIX OPENGL Pin
Tim Craig23-Feb-10 22:11
Tim Craig23-Feb-10 22:11 
GeneralRe: PROBLEM WITH MATRIX OPENGL Pin
LunaticFringe24-Feb-10 7:30
LunaticFringe24-Feb-10 7:30 
AnswerRe: PROBLEM WITH MATRIX OPENGL Pin
Cedric Moonen23-Feb-10 20:40
Cedric Moonen23-Feb-10 20:40 
Questionmultiple run Pin
zakria8123-Feb-10 13:46
zakria8123-Feb-10 13:46 
AnswerRe: multiple run Pin
Avi Berger23-Feb-10 14:16
Avi Berger23-Feb-10 14:16 
GeneralRe: multiple run Pin
Richard Andrew x6423-Feb-10 14:47
professionalRichard Andrew x6423-Feb-10 14:47 
QuestionHow to disable auto sorting for CComboBox Pin
TechAvtar23-Feb-10 4:08
TechAvtar23-Feb-10 4:08 
AnswerRe: How to disable auto sorting for CComboBox Pin
TechAvtar23-Feb-10 4:11
TechAvtar23-Feb-10 4:11 
AnswerRe: How to disable auto sorting for CComboBox Pin
David Crow23-Feb-10 4:11
David Crow23-Feb-10 4:11 
QuestionHow to dispatch message to ole control in windowless richedit? Pin
xp52423-Feb-10 1:47
xp52423-Feb-10 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.