Thread: help again with stacks

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

    help again with stacks

    Before I start
    Thanks Sebastiani your suggestion was extremely helpful..

    But the program pushes the characters into the stack but it doesn't pop them:
    [code]

    /* 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);
    void StackPush(STACK *S, int Value);
    void StackPop(STACK *S, int Target);
    int StackIsFull(STACK *S);
    int StackIsEmpty(STACK *S);

    int main (void)
    {
    char line[80], revline[80];
    int 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 (i=0; i<N && line[i] != 0; i++)
    {
    StackPush(S, line[i]);
    }

    for (i=0;i<line[i];i++)
    {
    printf("array[%d]=%c\n",i,S->array[i]);
    }

    for (i=0; i<N && line[i] != 0; i++)
    {
    StackPop(S, line[i]);
    printf("line[i] is located at %p\n", 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;
    }
    void 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 ;
    }
    void StackPop(STACK *S, int Target)
    {
    if (!StackIsEmpty(S) )
    {
    printf("S->top - is located: %p\n", S->top);
    Target = S->array[S->top];
    S->array[S->top] = 0;
    S->top--;
    }
    return;

    }

    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;
    }
    [code]


    Enter a line to be reversed:
    hello
    Line input: hello
    array[0]=h
    array[1]=e
    array[2]=l
    array[3]=l
    array[4]=o
    S->top - sim at: 00000004
    line[i] is located at 00000068
    S->top - sim at: 00000003
    line[i] is located at 00000065
    S->top - sim at: 00000002
    line[i] is located at 0000006C
    S->top - sim at: 00000001
    line[i] is located at 0000006C
    S->top - sim at: 00000000
    line[i] is located at 0000006F
    Line Output: ¦¦¦¦¦
    Press any key to continue

    I also printed out the memory addresses maybe i'm wrong but aren't the memory addresses of the line[i] of the pop supposed to be at the same place as the S->top. I apologize for not using code tags I have to read up on them
    Code:
    courier

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I noticed that too, let me look at it a little closer...
    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;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Mostly just one-off errors....


    Code:
    
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <conio.h>
    #define NAX 81
    
    
    void Free( void *ptr )
    {
     if(ptr != NULL) free(ptr);
    }
    
    
    
    typedef struct stack 
    { 
    char *array;
    int size; 
    int top; 
    }STACK; 
    
    STACK *S; 
    
    
    STACK *StackCreate(int nSize); 
    void StackDestr(STACK *S); 
    char StackPush(STACK *S, char Value);
    char StackPop(STACK *S);
    int StackIsFull(STACK *S); 
    int StackIsEmpty(STACK *S); 
    int StackCount(STACK *S);
    
    int main (void) 
    { 
    char line[NAX-1], revline[NAX-1];
    
    int p, i, ch; 
    
    StackCreate(NAX);
    
    
    printf("Enter a line to be reversed: \n"); 
    
    fgets(line, NAX-1, stdin);
    
    
     
     for (p=0; p < strlen(line); p++)
     {
      StackPush(S, line[p]);
     }
    
    
     printf("# Of Chars: %i\n", StackCount(S));
    
     for (i=0; i < strlen(line); i++)
     {
      revline[i] = StackPop(S);
     }
     revline[i] = '\0';
    
     printf("Line Output: %s\n", revline);
    
     getch();
    
    
    
    return 0; 
    } 
    
    
    
    
    
    
    
    STACK *StackCreate(int nSize) 
    { 
    int k; 
    
    S = (STACK *)malloc(sizeof(STACK)); 
    if (S == NULL) {perror("reversChar"); exit(1);} 
    S->array = (char *)malloc(nSize*sizeof(char));
    if (S->array == NULL) { perror("reversChar"); exit(1);} 
    S->size = nSize; 
    S->top = 0;
    
    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, char Value)
    { 
    if (StackIsFull(S))
     {
      puts("No room in Stack");
      return 0;
     }
    S->array[S->top] = Value;
    S->top = 1 + S->top;
    return Value;
    } 
    
    
    
    
    char StackPop(STACK *S)
    {
    if(StackIsEmpty(S))return 0;
    char firstchar;
    S->top--;
    firstchar = S->array[S->top];
    S->array[S->top] = 0;
    return firstchar;
    } 
    
    
    
    
    int StackIsFull(STACK *S) 
    { 
    if (S->top >= S->size - 1)
     return 1;
    else
     return 0;
    } 
    
    
    
    
    int StackIsEmpty(STACK *S) 
    { 
    if (S->top <= 0)
     return 1;
    else
     return 0;
    } 
    
    
    
    int StackCount(STACK *S)
    {
     return S->top-1;
    }
    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. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 07:22 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  3. Avioding Stacks
    By LoafOfBread34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 06:20 AM
  4. Dumping singly linked list into 2 stacks.
    By strotee76 in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2004, 05:48 PM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 02:01 AM