The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bash. convert mpeg adts to normal mp3 stahoo23 Shell Programming and Scripting 5 05-18-2008 02:26 AM
Questions on AWK stevefox UNIX for Dummies Questions & Answers 2 02-07-2008 08:41 PM
how i prepare a c++ code(c code) for implementing my own protocol format amitpansuria High Level Programming 1 09-06-2007 08:09 PM
SSH key code versus server key code Texan Security 1 04-12-2006 08:57 AM
Return code from PL/SQL Code Shaz UNIX for Advanced & Expert Users 7 06-03-2003 07:56 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-24-2008
eva eva is offline
Registered User
 

Join Date: Feb 2008
Posts: 2
Stumble this Post!
Question 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:
Code:
#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:
Code:
#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:
Code:
#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;

}
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-24-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 954
Stumble this Post!
Where are you declaring/defining itemType?
Reply With Quote
  #3 (permalink)  
Old 02-24-2008
eva eva is offline
Registered User
 

Join Date: Feb 2008
Posts: 2
Stumble this Post!
In itemADT.h -

Code:
typedef struct itemCDT *itemType;
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0