need some help!

This is a discussion on need some help! within the C Programming forums, part of the General Programming Boards category; Hi I need help with a queue I'm running VC 6 I have no errors i compiles right it can ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    7

    need some help!

    Hi
    I need help with a queue I'm running VC 6
    I have no errors i compiles right it can push but it can't pop. any help would be appreciated. Thanks ahead of time

    [CODE]
    #include <stdio.h>
    #include <stdlib.h>
    #define N 7

    typedef struct kwutemp
    {
    int *item;
    int size;
    int front;
    int end;
    }KWU;
    KWU *K;

    KWU *QueueCreate(int nItem);
    void QueueDestr(KWU *K);
    void QueueStart(KWU *k, int pos);
    void QueueEnd(KWU *K, int *pt);
    int QueueFull(KWU *K);
    int QueueEmpty(KWU *K);

    int main(void)
    {
    char s[16];
    int t;
    int pos;

    QueueCreate(N);

    printf("Type 'E' to enter a value and 'R' to remove a value and 'Q' to quit.\n");
    if (K == NULL) { perror("queue03"); exit (1); }

    for (;
    {
    fgets(s, sizeof(s), stdin);
    if (feof(stdin)) break;
    printf("Add, Remove or Quit: %s", s);

    if (s[0] == 'E' || s[0] == 'e') QueueStart(K, atoi(s + 1));
    if (s[0] == 'R' || s[0] == 'r') QueueEnd(K, &pos);
    if (s[0] == 'Q' || s[0] == 'q')
    {
    QueueDestr(K);
    break;
    }
    else
    {
    printf("Front = %d\n", K->front);
    printf("Last = %d\n", K->end);
    for (t=0; t < K->size; t++)
    {
    printf("Queue - [%d] -- %d", t, K->item[t]);
    if (K->front == t) printf(" <-- first");
    if (K->end == t) printf(" <-- end");
    printf("\n");
    }
    printf("\n");
    }
    }
    return 0;
    }

    KWU *QueueCreate(int nItem)
    {
    int t;
    int tmp = 0;
    K = (KWU *)malloc(sizeof(KWU));
    if (K == NULL) { perror("queue03"); exit (1); }
    K->item = (void *) malloc(nItem*sizeof(void*));
    if (K->item == NULL) { perror("queue03"); exit (1); }
    K->size = nItem;
    K->front = -1;
    K->end = 0;
    for (t=0; t < K->size; t++)
    K->item[t] = 0;
    return K;
    }

    void QueueDestr(KWU *K)
    {
    free(K->item);
    free(K);
    return;
    }

    void QueueStart(KWU *k, int pos) /* Queue push */
    {
    if (!QueueFull(K) )
    {
    K->front = 1 + K->front;
    K->item[K->front] = pos;
    }
    else puts ("No room in queue");
    return;
    }

    void QueueEnd(KWU *K, int *pt) /* Queue Pop */
    {
    int next;
    if (QueueEmpty(K))
    {
    puts("Nothing to remove");
    return ;
    }

    else
    {
    *pt = K->item[K->front];
    K->item[K->front] = 0;
    if (K->front == K->end) K->front = K->end = -1;
    else
    {
    next = K->front + 1;
    if (next >= K->size) next = 0;
    K->front = next;
    }
    }
    return ;
    }

    int QueueFull(KWU *K)
    {
    int next;
    next = 1 + K->front;
    if (next > -1 + K->size) return 1;
    else return 0;

    /*if (K->front < N)
    return 0;
    else
    return 1;*/
    }

    int QueueEmpty(KWU *K)
    {
    if (K->front - 1) return 0;
    else return 1;
    }

    [CODE]
    I must be doing something wrong I tried to insert the code otion and it isn't working Sorry in advance.

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > [CODE]
    You need a / before the C.
    Make sure to turn smileys off too.
    The world is waiting. I must leave you now.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    [code]
    ... insert code here ...
    [/code]

    I don't see a pop function in your code. That's probably why pop doesn't work.

    Pop is very simple:
    Code:
    <datatype> pop( <datatype> *stack )
    {
        return stack->data[stack->count--];
    }
    That assumes you use an array.

    Code:
    <datatype> pop( <datatype> *stack )
    {
        <datatype> *temp;
        temp = statck->data;
        if( stack->data != NULL )
        {
            stack->data = stack->data->next;
        }
        temp->next = NULL;
        return temp;
    }
    That assumes you use a linked list. All in all, very easy.

    Quzah.


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

Popular pages Recent additions subscribe to a feed

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