Problem using two stacks with following code

This is a discussion on Problem using two stacks with following code within the C Programming forums, part of the General Programming Boards category; Im getting crashes if I initialize more than one stack using this code. Code: struct stackNode{ int data; struct stackNode ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    23

    Problem using two stacks with following code

    Im getting crashes if I initialize more than one stack using this code.

    Code:
    struct stackNode{
    	
    	int data;
    	struct stackNode *nextptr;
    };
    
    int pop(struct stackNode **topptr)
    	{
    		
    		struct stackNode *tmp;
    		
    		if( *topptr != NULL)
    		{
    			tmp = *topptr;
    			*topptr = (struct stackNode *) tmp->nextptr;
    			int temp2;
    			temp2 = tmp->data;
    			free(tmp);
    			return temp2;
    		}
    		else{
    			printf("Pop function error.");
    			exit(0);
    		
    		}
    		
    	
    	}
    	
    	void push(struct stackNode **topptr, int info)
    	{
    		
    		struct stackNode *temp;
    		
    		temp = (struct stackNode *)malloc(sizeof(struct stackNode));
    		
    			temp->data = info;
    			temp->nextptr = *topptr;
    			*topptr = temp;
    			
    	}
    I am initializing in Main using
    Code:
    struct stackNode **xstack;
    struct stackNode **ystack;

    if I only initialize one I don't crash until I try to use the second initialized stack.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    Example:
    Code:
    struct stackNode **xstack;
    push(xstack, 5);
    push(xstack, 5);
    
    
    printf("%d %d", pop(xstack), pop(xstack));
    works fine.

    If I try
    Code:
    struct stackNode **xstack;
    struct stackNode **ystack
    
    push(xstack, 5);
    push(ystack, 5);
    
    printf("%d %d", pop(xstack), pop(ystack));
    I will crash.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    struct stackNode **xstack;
    push(xstack, 5);
    push(xstack, 5);
    
    
    printf("%d %d", pop(xstack), pop(xstack));

    u have just defined a stacknode pointer,that pointer needs to point somewhere, so use something like this:

    Code:
    struct stackNode xstack;
    struct stackNode* xstack_p;
    xstack_p = &xstack;
    
    
    push(&xstack_p, 5);
    push(&xstack_p, 5);
    
    
    printf("%d %d", pop(&xstack_p), pop(&xstack_p));
    }

    use this.




    for two stacks
    Code:
    struct stackNode xstack;
    struct stackNode* xstack_p;
    xstack_p = &xstack;
    
    struct stackNode ystack;
    struct stackNode* ystack_p;
    ystack_p = &ystack;
    Last edited by qqqqxxxx; 02-16-2006 at 11:25 PM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    thanks a bunch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM

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