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

This is a discussion on New to C... questions about this code...ADTs within the C Programming forums, part of the General Programming Boards category; Hi, In a file called itemADT.c I have specified the itemType data type to contain a listADT (having already written ...

  1. #1
    eva
    eva is offline
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Unhappy 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. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    What does stringToList do?

  3. #3
    eva
    eva is offline
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    stringToList takes an array of char and converts it to a listADT containing the letters in the array:

    Code:
    listADT stringToList(char str[])
    {
    	listADT list = createList();
    	int index = 0;
    	char c = str[index];
    
    	while(c!='\0')
    	{
    		list = consList(c, list);
    		index++;
    		c = str[index];
    
    	}
    	return list;
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Is listADT a typedef for a pointer to something? What is in listADT.h?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 11:40 AM
  2. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21