New to C... questions about this code... ADTs...

Hi,

In a file called itemADT.c I have specified the itemType data type to contain a listADT (having already written this library) and an integer, which will eventually represent the no. of times the word that is stored in the listADT has occurred (when I do this, and write code to insert itemTypes into a binary tree).

I know my listADT.c code and listFuns.c code works fine because I have tested this separately, but when running the program below (itemADTtest.c), that calls functions from itemADT.c (also shown below), I'm not getting the correct results - I think the problem must lie within the buildItem method, but I'm not sure. buildItem calls the stringToList method from the listFuns library, which creates and returns a listADT containing the letters in the str[] array passed into it. It is then supposed to assign the tempList created in the method to the 'word' part of the itemType, and assign a value of 1 to the count integer of the itemType - then return the itemType.

When I run the test program, both of the test itemTypes are shown to contain the same word (when one should contain 'rat' and one 'rag', although these would be the wrong way around as I've not included a 'reverse' method), but instead, when I try to print item1 and item2 to screen they are both shown as containing the word 'rag'? (or 'gar' to be exact as it's backwards)...

I've included a call to displayList within the buildItem method to show what the temporary lists are created contain, and this shows they are creating the correct words, so I'm thinking the problem must be either in the last few lines of the buildItem method within itemADT.c or in my test program...

Sorry for the long post, I'm still a beginner to C... but any help would be much appreciated!

itemADT.c:

#include <stdlib.h>
#include <stdio.h>
#include "listADT.h"
#include "listFuns.h"
#include "boole.h"
#include "itemADT.h"

struct itemCDT
{
	listADT word;
	int count;
};

itemType buildItem(char str[])
{
	itemType tempItem;
	listADT tempList;
	tempList = createList();

	tempList = stringToList(str);

	displayList(tempList); //

	//tempItem->list = tempList;
	tempItem->word = tempList;
	tempItem->count = 1;

	return tempItem;
}

itemType incItemCount(itemType item)
{
	item->count++;
	return item;
}

void displayItem(itemType item)
{
	listADT temp;

	temp = item->word;
	displayList(temp);

	printf("\n%d",item->count);
}

int noOfOccurences(itemType item)
{
	return item->count;
}

boolean itemLess(itemType item1, itemType item2)
{
	listADT temp1;
	listADT temp2;

	temp1 = item1->word;
	temp2 = item2->word;

	return precedesList(temp1,temp2);
}

boolean itemEqual(itemType item1, itemType item2)
{
	listADT temp1;
	listADT temp2;

	temp1 = item1->word;
	temp2 = item2->word;

	return equalsList(temp1,temp2);

}

itemADTtest.c:

#include "listADT.h"
#include "listFuns.h"
#include "itemADT.h"
#include "boole.h"
#include <stdlib.h>
#include <stdio.h>

main()
{
	itemType item1;
	itemType item2;

	char word1[] = "rat";
	char word2[] = "rag";

	item1 = buildItem(word1);
	item2 = buildItem(word2);

	displayItem(item1);
	displayItem(item2);

	if(itemLess(item1, item2))
		printf("\nThe word in item1 is alphabetically before that in item2\n");
	else
		printf("\nThe word in list2 is alphabetically before that in list1\n");

	if(itemEqual(item1, item2))
		printf("\nThe words in both items are equal\n");
	else
		printf("\nThe words in both items are not equal\n");

	displayItem(item1);
	displayItem(item2);

}

listADT.c:

#include <stdlib.h>
#include <stdio.h>
#include "listADT.h"
#include "boole.h"

struct listCDT
{
	char data;
	listADT next;
};


listADT createList()
{
	return NULL;
}

listADT consList(char item, listADT list)
{
	listADT temp;
	temp = (listADT)malloc(sizeof(listADT));
	temp->data = item;
	temp->next = list;
	return temp;
}

char headList(listADT list)
{
	if(isEmptyList(list))
		printf(" ERROR : list empty for headList!");
	else
		return list->data;
}

listADT tailList(listADT list)
{
	if(isEmptyList(list))
		printf(" ERROR : list empty for tailList!");
	else
		return list->next;
}

boolean isEmptyList(listADT list)
{

	if(list==NULL)
		return TRUE;
	else
		return FALSE;

}

Where are you declaring/defining itemType?

In itemADT.h -

typedef struct itemCDT *itemType;