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


 
Thread Tools Search this Thread
Top Forums Programming New to C... questions about this code... ADTs...
# 1  
Old 02-24-2008
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;

}

# 2  
Old 02-24-2008
Where are you declaring/defining itemType?
# 3  
Old 02-24-2008
In itemADT.h -

Code:
typedef struct itemCDT *itemType;

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Just had a few questions

1) The lpr and sort utilities accept input either from a file named on the command line or from standard input. a)Name two other utilities that function in a similar manner. b)Name a utility that accepts its input only from standard input. 2) Explain the following error message. What... (10 Replies)
Discussion started by: youngyou
10 Replies

2. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

3. Shell Programming and Scripting

More ps questions.

Hey all, Thanks for all the help you have given me. Two more things I am trying to figure out. I need to issue a command..example ps -ef | grep <process> This would return about 10-15 running processes. I need to verify that there are x amount of processes running. What is... (7 Replies)
Discussion started by: jeffs42885
7 Replies

4. Programming

Hi Folks please see the code and respond the questions!

Hi All. I have a package discreibed down. CREATE OR REPLACE PACKAGE BODY IRISCOS_REFRESH_pkg AS PROCEDURE IRISCOS IS CURSOR cur_pci(c_myear number) IS Select cstc.cid, cstc.elid, p.first_name,p.last_name,p.email_id ,pl.plan_name, ptc.chair_id from person p, person_to_chair ptc,... (7 Replies)
Discussion started by: Haque123
7 Replies

5. Shell Programming and Scripting

bash. convert mpeg adts to normal mp3

I need script to convert many files of the format MPEG ADTS to a normal mp3. (because my net radio can't play ADTS, for example: file plik.mp3 plik.mp3: MPEG ADTS, layer III, v1, 128 kBits, 44.1 kHz, JntStereo not play file beautiful.mp3 beautiful.mp3: Audio file with ID3 version 23.0 tag,... (6 Replies)
Discussion started by: stahoo23
6 Replies

6. Programming

Host to IP Source Code in C - Questions! Please Help

Hello, First of all, i want to say Hi! Glad to join your forum and be your member. I'm a newbie towards C/C++ especially on network programming. What is the definition of getaddrinfo()? int getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, ... (4 Replies)
Discussion started by: f.ben.isaac
4 Replies

7. Programming

A few C questions

Hi guys. I'm writing a basic script in C# and there's a few areas where I am stuck, and I was wondering if you may be able to help me? 1) Can you embed an image into the source code so it will show in the windows GUI? 2) Can you change the background of the GUI? 3) How do you add an .ico to... (5 Replies)
Discussion started by: JayC89
5 Replies

8. Programming

C questions

What does "extern" do? ex. extern int x; and another question, what about using static in functions? like: static void foo(), why? (2 Replies)
Discussion started by: Esaia
2 Replies
Login or Register to Ask a Question