Thread: Whats wrong with my queue?

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    83

    Whats wrong with my queue?

    Hi all,

    I'm trying to write a program that handles a queue. It works when I add an element to the queue (push), but not when I use the command pop(). The program doesn't seem to go into the function. When I write pop, the program just jumps to the next row in the command prompt and seems to be waiting for some new input. If I write pop a second time it looks like its working. Anyone can help me out? Thanks a lot!
    Below is the code.

    Code:
    // A queue system
    // Enter push a, for example to put the new char a in queue
    // Enter pop to retrieve first element in queue
    
    #include <stdio.h>
    #define MAX 200
    
    void push(char c);       // Puts a new element last in queue
    char pop(void);            // Gets the first element in queue
    int compare_str(char a[], char b[]);
    
    static char s[MAX];
    int first = -1;
    int last = -1;
    
    int main(void)
    {
        char c, cpop, cont = 'y';
        char command[4];
        int result, i;
        
        printf("Enter push character to put a new element in in queue. Enter pop to retrieve firs element in queue:\n");
        
        while(cont == 'y')
        {
            scanf(" %s %c", command, &c);    // space between %s and %c to get rid of spaces
        
        
        
            // Get command
            result = compare_str(command, "push");
            printf("result: %d\n", result);
            if(result == 0)
                push(c);
        
            result = compare_str(command, "pop");
            printf("result: %d\n", result);
            if(result == 0)
            {    
                cpop = pop();
                printf("%c was withdrawn\n", cpop);
            }
            printf("Queue is now: \n");
            
            i = 0;
            while(s[i] != '\0')
            {
                printf("%c ", s[i]);
                i++;
            }
            
            printf("\nContinue? (y/n) ");
            scanf(" %c", &cont);
            printf("\ncontinue: %c\n", cont);
        }
        
            
        return 0;
        
    }
    
    int compare_str(char a[], char b[])
    {
        int c = 0;
        
        while(a[c] == b[c])
        {
            if(a[c] == '\0' || b[c] == '\0')
                break;
            c++;
        }
        if(a[c] == '\0' && b[c] == '\0')
            return 0;
        else
            return -1;
        
    }
    
    void push(char c)
    {
        last++;
        s[last] = c;
        
        printf("Inside push()\n");
    }
    
    char pop(void)
    {
        first++;
        return s[first];
        
        printf("Inside pop()\n");
    }
    Last edited by jjohan; 09-25-2014 at 03:41 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > return s[first];
    > printf("Inside pop()\n");
    It doesn't print anything because the print is AFTER the return statement.


    > char command[4];
    "push\0" takes 5 chars - which is a buffer overrun.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Hm I changed it now, but still have the same problem.

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Here's the new code. Still the same problem with pop. I have to write pop then return, then pop again and return for it to work.

    Code:
    // A queue system
    // Enter push a, for example to put the new char a in queue
    // Enter pop to retrieve first element in queue
    
    #include <stdio.h>
    #define MAX 200
    
    void push(char c);       // Puts a new element last in queue
    char pop(void);            // Gets the first element in queue
    int compare_str(char a[], char b[]);
    
    static char s[MAX];
    int first = 0;
    int last = 0;
    
    int main(void)
    {
        char c, cpop, cont = 'y';
        char command[10];
        int result, i;
        
        printf("Enter push character to put a new element in in queue. Enter pop to retrieve firs element in queue:\n");
        
        while(cont == 'y')
        {
            scanf(" %s %c", command, &c);    // space between %s and %c to get rid of spaces
        
        
        
            // Get command
            result = compare_str(command, "push");
            if(result == 0)
                push(c);
            
            result = compare_str(command, "pop");
            if(result == 0)
            {
                cpop = pop();
                printf("%c was withdrawn\n", cpop);
            }
            printf("Queue is now: \n");
            
            i = first;
            while(s[i] != '\0')
            {
                printf("%c ", s[i]);
                i++;
            }
            
            printf("\nContinue? (y/n) ");
            scanf(" %c", &cont);
            printf("\ncontinue: %c\n", cont);
        }
        
            
        return 0;
        
    }
    
    int compare_str(char a[], char b[])
    {
        int c = 0;
        
        while(a[c] == b[c])
        {
            if(a[c] == '\0' || b[c] == '\0')
                break;
            c++;
        }
        if(a[c] == '\0' && b[c] == '\0')
            return 0;
        else
            return -1;
        
    }
    
    void push(char c)
    {
        
        s[last] = c;
        last++;
        
        printf("Inside push()\n");
    }
    
    char pop(void)
    {
        printf("Inside pop()\n");
        return s[first++];
        
        
        
    }

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Its this line that's causing my problems I figured out:

    Code:
    scanf(" %s %c", command, &c);    // space between %s and %c to get rid of spaces
    I want the program to be able to read in a command + a char OR just a command.

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Solved it!

    Code:
    // A queue system
    // Enter push a, for example to put the new char a in queue
    // Enter pop to retrieve first element in queue
    
    #include <stdio.h>
    #define MAX 200
    
    void push(char c);       // Puts a new element last in queue
    char pop(void);            // Gets the first element in queue
    int compare_str(char a[], char b[]);
    
    static char s[MAX];
    int first = 0;
    int last = 0;
    
    int main(void)
    {
        char c, cpop, cont = 'y';
        char command[10];
        int result, i;
        
        printf("Enter push character to put a new element in in queue. Enter pop to retrieve firs element in queue:\n");
        
        while(cont == 'y')
        {
            scanf("%s", command);    // space between %s and %c to get rid of spaces
            if((compare_str(command, "push")) == 0)
            {
                scanf(" %c", &c);
                printf("Character: %c\n", c);
            }
            else;
        
        
            //Get command
            result = compare_str(command, "push");
            if(result == 0)
                push(c);
            
            result = compare_str(command, "pop");
            if(result == 0)
            {
                cpop = pop();
                printf("%c was withdrawn\n", cpop);
            }
            printf("Queue is now: \n");
            
            i = first;
            while(s[i] != '\0')
            {
                printf("%c ", s[i]);
                i++;
            }
            
            printf("\nContinue? (y/n) ");
            scanf(" %c", &cont);
            printf("\ncontinue: %c\n", cont);
        }
        
            
        return 0;
        
    }
    
    int compare_str(char a[], char b[])
    {
        int c = 0;
        
        while(a[c] == b[c])
        {
            if(a[c] == '\0' || b[c] == '\0')
                break;
            c++;
        }
        if(a[c] == '\0' && b[c] == '\0')
            return 0;
        else
            return -1;
        
    }
    
    void push(char c)
    {
        s[last] = c;
        last++;
    }
    
    char pop(void)
    {
        return s[first++];    
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Line 32 - You don't need an empty "else".

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Code:
            i = first;
            while(s[i] != '\0')
            {
                printf("%c ", s[i]);
                i++;
            }
    Your while-loop run until a terminator is reached, but you have never set a terminator.
    Your loop can be like this:
    Code:
    while(i <= last) …

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is wrong with my queue ADT?
    By slee8251 in forum C Programming
    Replies: 5
    Last Post: 10-08-2012, 02:33 PM
  2. whats wrong with this ????!!!
    By GSalah in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2006, 11:11 AM
  3. whats wrong with this
    By DeathDealer in forum C++ Programming
    Replies: 22
    Last Post: 02-17-2005, 12:11 AM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM