Thread: Need help with Qeueu Implementation

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    12

    Need help with Qeueu Implementation

    Hello,

    I'm trying to implment a qeueue using dynamic data structures. The code compiles and all, but the problem is that the pointer that is supposed to point to the head of the qeueue keeps on pointing to the end of the qeueue.... here's part of the code..

    oh yeah, ptr1 and ptr2 are declared in main and are initialized as NULL

    Code:
    struct data_node {
    	double price_1;
    	struct data_node *nextptr;
    };
    
    typedef struct data_node Data_1;
    typedef Data_1 *Data_1ptr;
    
    
    void Get( Data data_2[], int size, Data_1ptr *headptr, Data_1ptr *tailptr, int a, int b)
    {
    	int ctr =0;
    	Data_1ptr newptr;
    	
    	newptr = malloc(sizeof(Data_1));
    
    	for ( ctr = a; ctr <= b; ctr++) {
    
    		if ( newptr != NULL ) {
    			
    			newptr->price_1 = data_2[ctr].price;
    			newptr->nextptr = NULL;
    			
    			if ( *headptr==NULL  ) { 
    				*headptr = newptr; }
    			else { 
    				(*tailptr)->nextptr = newptr; }
    			
    			*tailptr = newptr;	
    			printf("headptr %10.2f  tailptr %10.2f\n", (*headptr)->price_1, (*tailptr)->price_1);
    			
    		}
    		else {
    			printf(" No memory available.\n");
    			 }
    		}
    Last edited by blork_98; 03-07-2006 at 09:55 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, you have crappy variable names. If one is supposed to be the 'head', call it that. If it's supposed to be the 'tail', call it that. It makes reading your code much easier. Second, why one earth are you typdefing pointers? God that's so ugly. Finally, 'ptr1' is the tail because you never change it to anything other than to assign it the first node you make. Everything else is prepended to that. Append if you don't like it, and update the tail pointer every time.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    oops... sorry for the confusion he he........

    anyway, ptr1 = headptr and ptr2 = tailptr....... my goal was to have the headptr point to the first input ( data_2[a]) and tailptr point to the last input ( data_2[b])

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're prepending. That's why you end up only pointing 'ptr1' at one (the first you enter) node. Just look at your code:
    Code:
    if ptr1 is null
        point ptr1 at the new node
    else
        newnode->next = ptr2
        ptr2 = newnode
    Thus, when you get to the end of all you've done, 'ptr2' is in fact the head, and 'ptr1' is really the tail.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    ic.. but when i try printing out which node ptr1 and ptr2 point to, they are both pointing at the same node
    Last edited by blork_98; 03-08-2006 at 01:44 PM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    12
    oops.. he he, got it.... sorry for da hassle

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 01:18 PM
  2. Replies: 1
    Last Post: 08-24-2008, 11:39 AM
  3. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 01:59 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 08:05 PM