Like Tree2Likes
  • 2 Post By laserlight

circular queue using DLL with globaly declared pointers not being properly initialize

This is a discussion on circular queue using DLL with globaly declared pointers not being properly initialize within the C Programming forums, part of the General Programming Boards category; I have a circular queue using DLL which is using globally declared pointers. the problem now is that it is ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Location
    cebu
    Posts
    4

    Question circular queue using DLL with globaly declared pointers not being properly initialize

    I have a circular queue using DLL which is using globally declared pointers.


    the problem now is that it is not being initialize properly or being cleared thus my code is not working as expected.


    in my code you will be asked how many nodes do you wish to enter "i made 2 default for now", after that you may then add or delete the node. add only works now since delete is still on progress.


    when you add the nodes "2 nodes by default" the program will only record the latest input so if i were to input 1 and 2, only 2 will be displayed. I know that this maybe because of my *first and *last variables not being initialize properly.


    how should i really work with global pointers?


    also im really new to project file programming and not a fan of pointers or linked list at all.


    any help would be really appreciated and explanations.


    main.c



    Code:
        void main(){
            int ch, number, numdum = 0;
            n *new, *ptr, *prev, *first, *last;
            first = NULL;
            last = NULL;
            clrscr();
            printf("Enter number of nodes: ");
            scanf("%d", &number);
            while (1){
                printf("\n\n[1] Insert Node \n[2] Delete Node\n[3] Exit\n");
                printf("\nEnter your choice: ");
                scanf("%d", &ch);
                switch (ch){
                case 1:
                    if(number != numdum){
                        add_node();
                        /* display(&first, &last); */
                        numdum++;
                    }else{
                        printf("Number of nodes achieved!");
                    }
                    break;
                case 2:
                    delete_node();
                    display();
                    break;
                case 3:
                        exit(0);
                default:
                    printf("\ninvalid choice");                
                }
            }
        }


    circ.h


    Code:
        #ifndef CIRC_H
        #define CIRC_H
        
        struct node{
            int val;
            struct node *next;
            struct node *prev;    
        };
        
        typedef struct node n;
        extern struct node *first, *last, *prev, *ptr;
        extern int number;
        
        
        int add_node();
        int delete_node();
        int display();
        
        n* create_node(int data);
        
        #endif

    add.c


    Code:
        int add_node(){
         
            int info,i, number = 2;
            n *new, *ptr, *prev, *first = NULL, *last = NULL;
        
            printf("\nEnter the value you would like to add: ");
            scanf("%d", &info);
            new = create_node(info);
         
            if (first == last && first == NULL){
                printf("\nfirst\n");
                first = last = new;
                first->next = last->next = NULL;
                first->prev = last->prev = NULL;
            }else{
                printf("\nsecond\n");
                last->next = new;
                new->prev = last;
                last = new;
                last->next = first;
                first->prev = last;
            }
        
            if (first == last && first == NULL)
                printf("\nlist is empty no elements to print");
            else{    
                printf("\n%d number of nodes are there", number);
                for (ptr = first, i = 0;i < number;i++,ptr = ptr->next){
                        printf("\n --- %d", ptr->val);
                }
            }
            return 1;
        }

    create.c


    Code:
      n* create_node(int info){
            n *new;
            new = (n *)malloc(sizeof(n));
            new->val = info;
            new->next = NULL;
            new->prev = NULL;
            return new;
        }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're expecting those variables you've declared in main (which should be int main, not void main) to be visible to other functions, well, you're going to be disappointed. If you have a global declaration of those variables somewhere, it is being hidden (in main) by the local declaration, so you're looking at two different sets of variables. In general, making these things global is a bad idea, because the whole point (generally) of putting these functions into their own file is to make a sort of library out of them, but since you're using global variables they are tied to that one particular list and cannot be re-used. Generally you just want to pass the head of the linked list to your list functions.

    Is there a reason you're limiting the number of nodes? The whole point of a list is to be "infinitely" extendible (where "infinitely" means "up to the limits of available memory").

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    By the way, instead of writing "DLL", I recommend writing "Doubly Linked List" since "DLL" can also mean "Dynamic Link Library", and in fact I believe that to be the more common usage.
    Elkvis and stahta01 like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2014
    Location
    cebu
    Posts
    4
    Quote Originally Posted by tabstop View Post
    If you're expecting those variables you've declared in main (which should be int main, not void main) to be visible to other functions, well, you're going to be disappointed. If you have a global declaration of those variables somewhere, it is being hidden (in main) by the local declaration, so you're looking at two different sets of variables. In general, making these things global is a bad idea, because the whole point (generally) of putting these functions into their own file is to make a sort of library out of them, but since you're using global variables they are tied to that one particular list and cannot be re-used. Generally you just want to pass the head of the linked list to your list functions.

    Is there a reason you're limiting the number of nodes? The whole point of a list is to be "infinitely" extendible (where "infinitely" means "up to the limits of available memory").
    i just limited it for testing purposes, also i was able to solve the problem by using double pointers and adding them as parameter to my add_node function, though i have encountered a different problem now for the delete part. I will edit my post now to provide more details

  5. #5
    Registered User
    Join Date
    Jan 2014
    Location
    cebu
    Posts
    4
    I will do that now, sorry if it confused anyone

  6. #6
    Registered User
    Join Date
    Jan 2014
    Location
    cebu
    Posts
    4

    updated code

    header:

    Code:
    struct node{
        int val;
        struct node *next;
        struct node *prev;    
    };
    
    
    typedef struct node n;
    extern int number;
    
    
    
    
    int add_node(n **first, n **last, int numdum);
    int delete_node(n **first, n **last, int number);
    
    
    n* create_node(int data);
    main:

    Code:
    int main(){
    	int ch, number, numdum = 0;
    	n *new, *ptr, *prev, *first, *last;
    	first = NULL;
    	last = NULL;
    	
    	clrscr();
    	printf("Enter number of nodes: ");
    	scanf("%d", &number);
        while (1){
    		printf("\n\n[1] Insert Node \n[2] Delete Node\n[3] Exit\n");
            printf("\nEnter your choice: ");
            scanf("%d", &ch);
            switch (ch){
            case 1:
    			if(number != numdum){
    				numdum++;
    				add_node(&first,&last,numdum);
    			}else{
    				printf("Number of nodes achieved!");
    			}
                break;
            case 2:
    			number--;
                delete_node(&first,&last,number);
                break;
            case 3:
    				exit(0);
            default:
                printf("\ninvalid choice");                
            }
        }
    }
    add:

    Code:
    int add_node(n **first, n **last, int numdum){
     
        int info,i;
    	n *new, *ptr, *t1, *t2, *prev;
    	t1 = *first;
    	t2 = *last;
    	
        printf("\nEnter node value: ");
        scanf("%d", &info);
        new = create_node(info);
    
    
        if (t1 == NULL){
            t1 = t2 = new;
            t1->next = t2->next = NULL;
            t1->prev = t2->prev = NULL;
    		*first = t1;
    		*last = t2;
        }else{
            t2->next = new;
            new->prev = t2;
            t2 = new;
            t2->next = t1;
            t1->prev = t2;
    		*last = t2;
        }
    
    
        if (t1 == t2 && t1 == NULL)
            printf("\nlist is empty!");
        else{
            for (ptr = t2, i = 0;i < numdum;i++,ptr = ptr->prev){
    			if(ptr->val != NULL){
    				if(i == 0){
    					if(numdum > 1){
    						printf("\n R -> %d", ptr->val);
    					}else{
    						printf("\n FR -> %d", ptr->val);
    					}
    				}else if(numdum == (i + 1)){
    					printf("\n F -> %d", ptr->val);
    				}else{
    					if(numdum != 1){
    						printf("\n      %d", ptr->val);
    					}
    					
    				}
    			}
    		}
        }
    	return 1;
    }
    delete:

    Code:
    int delete_node(n **first, n **last, int numdum){
    	int i;
    	n *temp, *prevnode, *ptr, *t1, *t2;
    	t1 = *first;
    	t2 = *last;
    	
    	ptr = t1;
    	prevnode = ptr; /* first */
    	ptr = ptr->next;
    	if(t1 != t2){
    		t2->next = prevnode->next;
    		ptr->prev = prevnode->prev;
    		t1 = ptr;
    		printf("%d is deleted", prevnode->val);
    		free(*first);		
    	}else{
    		free(*first);
    		free(*last);
    		t1 = NULL;
    		t2 = NULL;
    	}
    	*first = t1;
    	*last = t2;
    	
    	if (t1 == t2 && t1 == NULL)
            printf("\nlist is empty!");
        else{
            for (ptr = t2, i = 0;i < numdum;i++,ptr = ptr->prev){
    			if(ptr->val != NULL){
    				if(i == 0){
    					if(numdum > 1){
    						printf("\n R -> %d", ptr->val);
    					}else{
    						printf("\n FR -> %d", ptr->val);
    					}
    				}else if(numdum == (i + 1)){
    					printf("\n F -> %d", ptr->val);
    				}else{
    					if(numdum != 1){
    						printf("\n      %d", ptr->val);
    					}
    					
    				}
    			}
    		}
        }	
    	return 1;
    }
    My new problem here now is that when i try to add nodes and delete them afterwards it will work fine, but if i were to add some nodes after deleting one the program will not work properly.

    example:

    i will enter 3,4 and 5 as nodes.

    Head = H, tail = T

    H -> 5
    4
    T -> 3

    now if i were to pop the head

    H -> 4
    T -> 3

    but if i add another node example 2

    this happens:

    F -> 2
    4
    3
    T -> 2

    also if i were to delete all the contents of the list and add a new node there are still some values left even though it should have been empty.

    any reason as to why this is happening?

    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help in Circular Queue implementation
    By MARCELO in forum C Programming
    Replies: 7
    Last Post: 04-19-2013, 07:47 AM
  2. Circular Queue
    By Giridhar Sanjay in forum C++ Programming
    Replies: 6
    Last Post: 07-02-2011, 03:22 PM
  3. how to initialize declared 2d character array
    By Marksman in forum C++ Programming
    Replies: 7
    Last Post: 03-13-2008, 10:41 PM
  4. Replies: 6
    Last Post: 12-06-2004, 06:03 AM
  5. circular queue
    By strotee76 in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2004, 10:55 AM

Tags for this Thread


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