Thread: help with queues

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

    help with queues

    I wrote out the code the diagnostics have no errors but i keep getting this logic error. the program is supposed to read in a line, put the line in a stack and reverse the line as it comes out of the stack.
    I would highly appreciate it if anyone can give a general direction in solving this problem thanks in advance. I really appreciate it.

    /* reversChar.c */
    #include <stdio.h>
    #include <stdlib.h>

    #define N 81

    typedef struct stack
    {
    int *array;
    int size;
    int top;
    }STACK;

    STACK *S;


    STACK *StackCreate(int nSize);
    void StackDestr(STACK *S);
    char StackPush(STACK *S, int Value);
    char StackPop(STACK *S, int Target);
    int StackIsFull(STACK *S);
    int StackIsEmpty(STACK *S);

    int main (void)
    {
    char line[80], revline[80];
    int p, i, ch;

    StackCreate(N);
    printf("Enter a line to be reversed: \n");

    for (i = 0; (i <80 ) && ((ch = getchar()) !=EOF) && (ch != '\n'); i++ )
    line [i] = (char)ch;
    line[i] = '\0';
    printf("Line input: %s\n", line);

    for (p=0; p<N; p++)
    {
    putchar(StackPush(S, atoi(line+1))); /* My error seems to be here */

    }
    for (p=0; p<N; p++)
    {
    StackPop(S, line[i]);
    }
    revline[i] = '\0';

    printf("Line Output: %s\n", revline);

    return 0;
    }

    STACK *StackCreate(int nSize)
    {
    int k;

    S = (STACK *)malloc(sizeof(STACK));
    if (S == NULL) {perror("reversChar"); exit(1);}
    S->array = (int *)malloc(nSize*sizeof(int));
    if (S->array == NULL) { perror("reversChar"); exit(1);}
    S->size = nSize;
    S->top = -1;

    for (k=0; k<S->size; k++)
    S->array[k] = 0;
    return S;
    }

    void StackDestr(STACK *S)
    {
    free(S->array);
    free(S);
    return;
    }

    char StackPush(STACK *S, int Value)
    {
    if (!StackIsFull(S) )
    {
    S->top = 1 + S->top;
    S->array[S->top] = Value;
    }
    else puts("No room in Stack");
    return 0;
    }

    char StackPop(STACK *S, int Target)
    {
    int firstchar;
    firstchar = S->array[S->top];
    S->array[S->top] = 0;
    S->top--;
    return firstchar;
    }

    int StackIsFull(STACK *S)
    {
    int next;
    next = 1 + S->top;
    if (next > -1 + S->size) return 1;
    else return 0;
    }

    int StackIsEmpty(STACK *S)
    {
    if (S->top < 0) return 1;
    else return 0;
    }


    Enter a line to be reversed:
    pedro
    Line input: pedro

    Line Output: ¦¦¦¦¦
    Press any key to continue

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    This seems to be a common question.
    If you're going to post 110+ lines of code, use code tags.
    The world is waiting. I must leave you now.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    putchar(StackPush(S, atoi(line+1))); /* My error seems to be here */

    atoi() is for converting an entire string (char[]) to a single number.

    try:
    for (p=0; p<N && line[p] != 0; p++)
    {
    StackPush(S, line[p]);
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating two static queues?
    By difficult.name in forum C Programming
    Replies: 2
    Last Post: 10-18-2004, 11:19 PM
  2. stacks and queues
    By j0hnb in forum C Programming
    Replies: 4
    Last Post: 04-16-2003, 09:44 PM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. queues
    By jamesb in forum C Programming
    Replies: 1
    Last Post: 04-21-2002, 08:57 PM
  5. queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-11-2001, 05:19 PM