#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12

    Neater code in C, what is ADT or CDT?


    http://www.cs.bu.edu/teaching/c/tree/binary/
    This talks about ADT or CDT, It sounds like what I am look1ng for,

    This is what I need. If I am writing a binary tree, or linkedlist, and I want them in their own header/c file's. Then how would the user use them? I mean if they do #include "linkedlist.h" now.. it only allows 1 LinkedList, Because in linkedlist.h, I have the code with public variables, and it's not possible to have more than 1 LinkedList..

    How would I do something like to add a value, you need to specifiy what linkedlist to add it to, like "Add2List(NODE *node, int data);", this is my curren't FreeList() function to free up a linked list.. I want it to accept a NODE* (a list) to free up, how would I do that?

    Code:
    int FreeList()
    {
    	NODE *curptr=NULL; 
    	NODE *nextptr=NULL; 
    	CURRENT=HEAD; 
    	curptr=HEAD; 
    
    	while(curptr!=NULL)
    	{
    		nextptr=curptr->next; 
    		free(curptr); 
    		curptr = nextptr; 
    	}
    	return 1; 
    }
    I don't know if I am explaining it right, but in C++, I would use classes, and then I would do like "CLASSNAME OBJECT" and then lets say OBJECT is a LinkedList object, I have have more, and play around with as much linkedlist's as I want in my project, but since C doesn't have classes, how would I go about doing that?
  2. #2
  3. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    just a suggestion / guess: what about however you're planning on having one, two, three... linked lists co-existing (each contained in an array that can be dynamically adjusted?) also do the same with your global variables, in the same manor. so you'd have an array containing 1, 2, 3... linked list roots, and you'd have a corresponding global variable array containing 1, 2, 3... necessary groups of global variables. 2 arrays. then, you'd only need a small addition to your nodes - a small variable that tells the node which number linked list it is in, therefore which elements(s) of the global array it should use. this additional variable could be as small as 2 or 3 bits maybe, depending on what you envisage the maximum number of lists to be. i could easily be way off there - just a thought.
    Last edited by balance; July 26th, 2003 at 12:26 PM.
  4. #3
  5. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    But let's say one user wants 1 LinkedList in his program and another wants like 14. This is an example of what I want, I did this with stack, but can't seem to get it with LinkedList or binarytree.

    Code:
    #include "CStack.h" 
    
    int main()
    {
    	STACK d={0};            //2 separate stacks in 1 program 
    	STACK NextStack={0}; 
    
    	InitStack(&d, 10); 
    
    	InitStack(&NextStack,100); 
    
    	push   (&d,1016); 
    	push   (&d, 500); 
    	printf ("%d\n",pop(&d)); 
    	push   (&d, 1004); 
    	printf ("%d\n",pop(&d)); 
    
    	//Now let's play around with the second stack 
    	push(&NextStack,540); 
    	push(&NextStack,443); 
    	printf ("%d\n",pop(&NextStack)); 
                    return 0; 
    }
    That was easy because the stack is really just a struct, so you can declare as much as you want. But with a LinkedList, even tho the node is also a struct, I dunno how to have a head for each node and whatnot, and what to pass to the functions.

IMN logo majestic logo threadwatch logo seochat tools logo