mfc >> Structures and linked lists

by Jon Evans » Mon, 24 Nov 2003 18:41:13 GMT

Hi,

I want to do a structure that will be used as a linked
list :-

typedef struct
{
HISTORY_DATA *pPrevious ;
HISTORY_DATA *pNext ;
// + More variables ....
} HISTORY_DATA ;

Fair enough, this won't compile as it stands but how do I
achieve this ? Linked lists are common enough so how do
you do it in 'C' (using 'void' pointers and plenty of
casting in the code sounds like a nightmare) ?

And before anyone asks, yes I'd MUCH rather use a class
instead of a struct but in this particular instance I'm
not allowed.

TTFN,
Jon


mfc >> Structures and linked lists

by Jon Evans » Mon, 24 Nov 2003 21:41:04 GMT


Hi Dave,

I thought of that but it results in a compiler error :-

"error C2371: 'HISTORY_DATA' : redefinition; different
basic types"

//////////
struct HISTORY_DATA ;

typedef struct
{
HISTORY_DATA *pPrevious ;
HISTORY_DATA *pNext ;
// + More variables ....
} HISTORY_DATA ; <--- Error indicated as this line
////////

Refering to Kernighan & Ritchie, this works :-

struct HISTORY_DATA
{
struct HISTORY_DATA *pPrevious ;
struct HISTORY_DATA *pNext ;
// + More variables ....
} ;

but no way to convert it to a typedef :-(

TTFN,
Jon

I



mfc >> Structures and linked lists

by Bjarne Nielsen » Mon, 24 Nov 2003 21:53:00 GMT

Then, try something like this

typedef struct TAG_HISTORY_DATA
{
TAG_HISTORY_DATA *pPrevious ;
TAG_HISTORY_DATA *pNext ;
// + More variables ....
} HISTORY_DATA ;

--
Bjarne Nielsen

"Jon Evans" < XXXX@XXXXX.COM > skrev i en meddelelse





Structures and linked lists

by Paul Perkins » Mon, 24 Nov 2003 22:01:11 GMT





Jon,

You need to do it like:

typedef struct _HISTORY_DATA
{
struct _HISTORY_DATA* pPrevious;
struct _HISTORY_DATA* pNext;
} HISTORY_DATA;

HTH,

Paul.



Structures and linked lists

by Alexander Grigoriev » Tue, 25 Nov 2003 00:30:01 GMT

Folks, does anybody of you know that the structure tag and the typedef does
NOT have to be different?

typedef struct HISTORY_DATA
{
struct HISTORY_DATA* pPrevious;
struct HISTORY_DATA* pNext;
} HISTORY_DATA;







Structures and linked lists

by Doug Harrison [MVP] » Tue, 25 Nov 2003 00:33:29 GMT





The only difference between a class and a struct is the default
accessibility of members and base classes. If classes were an option, you
would be in C++, and in C++ you could say:

struct HISTORY_DATA
{
HISTORY_DATA *pPrevious ;
HISTORY_DATA *pNext ;
};

In C, you'll have to use something like this:

typedef struct HISTORY_DATA
{
struct HISTORY_DATA *pPrevious ;
struct HISTORY_DATA *pNext ;
} HISTORY_DATA;

There's no reason to give the struct tag a different name than the typedef,
and unless you're very sure of what you're doing, don't begin your own
identifiers with the underscore, because such names are by and large
reserved.

--
Doug Harrison
Microsoft MVP - Visual C++


Structures and linked lists

by Joseph M. Newcomer » Tue, 25 Nov 2003 09:32:09 GMT

Yep. This is not valid C. You could write it as

class HISTORY_DATA {
public:
HISTORY_DATA *Previous;
HISTORY_DATA * Next;
//... etc.
};

or

typedef struct _historydata {
struct _historydata * Previous;
struct _historydata * Next;
// ...etc.
} HISTORY_DATA;

Sounds like a class assignment, since there is rarely any justification for forcing such
low-level implementations for production development.
joe


On Mon, 24 Nov 2003 02:41:13 -0800, "Jon Evans" < XXXX@XXXXX.COM >



Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Structures and linked lists

by Jon Evans » Tue, 25 Nov 2003 16:53:31 GMT

Thanks All,

Sorted :-)

TTFN,
Jon




Structures and linked lists

by Jon Evans » Tue, 25 Nov 2003 17:12:43 GMT

Joe,

Absolutely agree that using a class instead of a struct
would make a lot more sense, particularly as in this case
the struct contains pointers to variable sized blocks of
memory that will need creating and destroying.

The actual struct is part of some core code that has to be
able to run on a wide range of processors, some of which
could be comparitively dumb and even if a C++ compiler
were available the overheads might not be tolerable.

TTFN,
Jon

justification for forcing such
< XXXX@XXXXX.COM >

I


Similar Threads

1. SImple question about structure and linked list

2. Generating a char* from a linked list of linked lists

Hmmm I might scare people away from this one just by the title, or
draw people in with a chalange :)

I'm writting this program in c++, however I'm using char* instead of
the string class, I am ordered by my instructor and she does have her
reasons so I have to use char*. So there is alot of c in the code as
well

Anyways, I have a linked list of linked lists of a class we defined, I
need to make all this into a char*, I know that I need to allocate
them into one continuous chunk of data, ie remove all the pointers.
The way that I'm currently doing this is by manually placing the data
into the cahr* at appropriate placed using sizeof on the objects I'm
placing into the the char*.
I tried looking up people that were working with dynamic memory and
char*'s so see if they were using tokens in their char* then searching
for the tokens instead of just accessing the memory locations directly
based on the sizeof operator. I am experiencing some mind boggling
problems with the way I have it implemented, so I was wondering how
other people are implementing a similiar situation. Would it be better
to use tokens to seperate the data fields inside the char* or just
leave it to pointer arithmatic...

This is my first time converting anytype of data structure to a char*,
infact I didn't know you could do just cast another pointerto a char*
before I started working on this...  sooo I'm basically looking for
any advice on teh above. Thanks in advacne for any suggestions that
are posted.

3. Linked list within a linked list

4. REQ: Help With List Class - Linked List Based

below is what I have for my list class implementation - array based exercise from my book; it works and all.
what I was wondering is if someone could help me out and code a 'link list based' solution to this problem. (adding two numbers and outputing the sum).

thanks very much.


____________________________________
#include <iostream.h>

void reportResults(int, int);

class CharacterPair {
public:
	int numberOfQuestionMarks() { return ((ch1 == '?'?1:0) + (ch2 == '?'?1:0) ); }
	bool pairMatch() { return ( ch1 == ch2 || numberOfQuestionMarks() > 0); }
	bool sentinelPair() { return (ch1 == '*');}
	void getPair() {cin >> ch1 >> ch2; }
private:
	char ch1;
	char ch2;
};

int main ()
{
	CharacterPair cp;
	int qmCnt = 0, mpCnt = 0;

	cp.getPair();
	while ( !cp.sentinelPair() ) 
	{
		qmCnt += cp.numberOfQuestionMarks();
		if ( cp.pairMatch() ) mpCnt++;
		cp.getPair();
	}

	reportResults(qmCnt, mpCnt);
	return 0;
}

void reportResults (int qCnt, int mCnt)
{
	cout << "Number of Question Marks is: " << qCnt << endl;
	cout << "Number of Matched Pairs is:  " << mCnt << endl;
}
/*
int CharacterPair::numberOfQuestionMarks()
{
	int temp = 0;
	if (ch1 == '?') temp++;
	if (ch2 == '?') temp++;
	return temp;
}

bool CharacterPair::pairMatch()
{
	return ( ch1 == ch2 || numberOfQuestionMarks() > 0);
}

bool CharacterPair::sentinelPair()
{
	return (ch1 == '*');
}

void CharacterPair::getPair()
{
	cin >> ch1 >> ch2;
}

*/
____________________________________

5. Help With List Class - Linked List Based

6. Design question - linked list or std::list

Hello

I am modelling a telephone system and I have created a call and a
party class.  A call can have one to many parties.  For example in a
conference call there can be 3 or more parties.  In a call transfer
there can be 3 parties and one of the parties disconnects to transfer
the call.

I need to be able to traverse the list of parties on each call.  for
example if each party to the call is disconnected I can remove the
call object.

I create the call object as a pointer and so I delete when the each
party to the call has disconnected.  The call object has a std::list
of the party objects.  Currently, the list is of party objects, not
pointers to party objects.

I was wondering if it would be better to change the design so that the
std::list is of pointers to party.  Or even to have the call object
create party* 's and the party object to be a linked list - ie a means
to SetNextParty(party* next).

But if I implement as a linked list I have the hassle of having to
delete each party.  I would do this when the call* was being deleted.

Anyone got any comments on the best approach?

A

7. What are best faq for C,C++ and data structures in C links for interviews