STACK problem part II

This is a discussion on STACK problem part II within the C Programming forums, part of the General Programming Boards category; Well, my last thread was getting too long and a bit confusing. So I decided to start a new thread ...

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    STACK problem part II

    Well, my last thread was getting too long and a bit confusing. So I decided to start a new thread and hopefully finalize my program.

    I still am not getting the file with main() function to work. I think the stack.c file and the stack.h files are correct HOWEVER my main function file is not working. And it all has to do with the the test to see if the stack is full.

    It maybe makes no sense to check this because as someone said (QuestionC) a linked list is never really full, but my instructor said that we are to let user input a string of text (however long) and the macro MAX_SIZE is to be set to 6 (why so short I haven't a clue -- go ask the stupid Professor with the PHd after his name !!) and then the result will be the text in reverse but only the last 6 characters, I guess. This is futile.

    So I changed the code five times and keep coming up with wrong output. What the heck I am messing up? Please don't yell too loudly Quzah. I am a newbie that really is trying hard to get this syntax. I'd send you something for Xmas or Hanukkah but I don't know how to get it to you.



    Code:
    /********************/
    /*  stack_main.c      */
    /********************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "stack.h"
    
    main()
    {
      char str[100];
      int i;
    
    
      make_empty();            /* call function to set 1st node to NULL */
    
      printf("Please enter a line of text to be printed in reverse:");
      
      /* getting character by character of input and then testing if
          stack is full and then if not pushing another character and
          incrementing through the text string till reach a carriage 
          return
      /*
    
      if (i<MAX_SIZE && (str[i] = getchar() != '\n'))   {
         if (!is_full())
         push (str[i]);
         i++;
      }
    
      /* while stack is not empty, pop out last character input, in
          theory writing the text in reverse order
      /*
    
      while (!is_empty()) {
         putc(pop(),stdout);
      }
    
      printf("\n");
    
      return 0;
    }
    Last edited by sballew; 11-30-2001 at 09:07 PM.
    Sue B.

    dazed and confused


  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Ohh... that MAX_SIZE thing makes things a lot clearer now... actually, it's still kinda confusing but... *shrug*

    Okay, errors first... these are really friggin' subtle errors...
    Code:
    if (i<MAX_SIZE && (str[i] = getchar() != '\n'))   {
         if (!is_full())
         push (str[i]);
         i++;
      }
    I actually wrote my own stack and compiled your code, and the first time I ran it, it actually gave me a happy face as its answer. Apparantly this is because
    str[i] = getchar() != '\n'
    is evaluated as...
    str[i] = (getchar() != '\n')
    Remember that assignment operators are evaluated after boolean operators? So, you'll have to change your code to explicitly evaluate the assignment first...
    Code:
    if (i<MAX_SIZE && ((str[i] = getchar()) != '\n'))   {
         if (!is_full())
         push (str[i]);
         i++;
      }
    When I ran this code, I only got the first letter of what I typed in. Looking at it again, I noticed that you're using an if statement here... Aha, so this stuff is only being evaluated once! Replacing the if with a while so it would loop untill the end of input...
    Code:
    while (i<MAX_SIZE && ((str[i] = getchar()) != '\n'))   {
         if (!is_full())
         push (str[i]);
         i++;
      }
    And it worked perfectly. Try makin' those changes, and see if your code works.

    Now, not wanting to be unneccisarily critical, I do think it's important that the statement is also clarafied a bit. Basically, the i < MAX_SIZE really shouldn't be there. I mean really, that bit of code is checking to see if it's overflowing the stack... but it's better to use your stack function is_full() to check for that, so...
    Code:
    while ((!is_full()) && ((str[i] = getchar()) != '\n'))   {
         if (!is_full())
         push (str[i]);
         i++;
      }
    Also, putting that check there makes the check inside the body not really all that usefull anymore... so we can scrap that too...
    Code:
    while ((!is_full()) && ((str[i] = getchar()) != '\n'))   {
         push (str[i]);
         i++;
      }
    Finally, what is str[i] actually doing? If your code needs to keep a record of the input in an array, then your code is an effective way to do it while going through the stack, but if you're just pushing the values to a stack, ou can just use one temporary value...
    Code:
    while (!is_full() && (i = getchar()) != '\n')   {
         push (i);
      }
    Notice that I just kinda abandoned the use of i as a count variable, I'm just using it as a temporary variable to hold a char now.

    I hope that makes the code more understandable. This part of the main function is really the only part that had problems. Feel free to ask if anything isn't understandable.
    Last edited by QuestionC; 11-30-2001 at 10:00 PM.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok, still muddling through.

    I changed my code snippet here

    Code:
    if (i<MAX_SIZE && (str[i] = getchar() != '\n'))   {
         if (!is_full())
         push (str[i]);
         i++;
      }
    to

    Code:
    while (i<MAX_SIZE && ((str[i] = getchar()) != '\n'))   {
         if (!is_full())
         push (str[i]);
         i++;
      }
    and got the following results



    Please enter a line of text to be printed in reverse:How are you?
    114 deleted successfully.
    r97 deleted successfully.
    a32 deleted successfully.
    119 deleted successfully.
    w111 deleted successfully.
    o72 deleted successfully.
    H


    Not quite what I wanted.

    Here's my stack.c file with all the stack functions.
    Wondering if there is anything wrong with the
    is_full now because I changed it when someone suggested it wasn't necessary. And something is wrong when it is popping off the nodes (characters) and not printing anything. Forgot what the heck causes that.

    Code:
    /*****************/
    /*  stack.c      */
    /****************/
    
    #include <stdlib.h>
    #include "stack.h"
    
    struct node {
    
       int data;
       struct node * next;
    };
    
    struct node *first;
    int count;
    
    void make_empty()
    {
       first = NULL;
       count = 0;
    }
    
    int is_empty(void)
    {
       return count == 0;
    }
    
    int is_full(void)
    {
       return 0;
    }
    
    void push(int insert_value)
    {
       struct node *new_node;
    
       new_node=malloc(sizeof(struct node));
       if (new_node == NULL)  {
          printf("Error in push:  stack is full.\n");
          exit (EXIT_FAILURE);
       }
       new_node->data = insert_value;
       new_node->next = first;
       first = new_node;
       count++;
    }
    
    int pop(void)
    {
      struct node * top_node;
      int i;
    
      if (is_empty())  {
         printf("Error in pop: stack is empty.\n");
         exit (EXIT_FAILURE);
      }
    
      printf("%d deleted successfully.\n",first->data);
      top_node=first;
      i = first->data;
      first = first->next;
      free(top_node);
      count--;
      return i;
    }
    
    void print_list()
    {
      struct node *ptr;
      if (first == NULL) {
        printf("List is empty.\n\n");
      }
    
      else  {
           printf("\n\nHere is your list:  \n");
           for (ptr=first;ptr;ptr=ptr->next)
               printf("%d\n",ptr->data);
           return;
      }
    }
    Sue B.

    dazed and confused


  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    That's just debugging output from your pop() function.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    I think I got this stack program to work. Finally.

    Now, if so, I also need to write the stack functions file based on arrays. The stack_main.c file should be able to run both the standard stack.c file and the stacka.c file (the one based on arrays).

    What parts do I change to make a stack function file based on arrays? hint from instructor was to use a variable named
    in str[MAX_SIZE]; and we still need our count (counter) variable.

    Here's the whole program as in my script file and a run of it.
    (bare in mind we are explicitly to have a macro that limits stack size to 6 --- so the whole text will not print out in reverse just the first 6 characters/spaces)

    Code:
    Script started on Sat Dec 01 17:23:42 2001
    1 /accounts/student2/srbkpk/STACK/PJT5>more stack.c
    /*****************/
    /*  stack.c      */
    /****************/
    
    #include <stdlib.h>
    #include "stack.h"
    
    struct node {
    
       int data;
       struct node * next;
    };
    
    struct node *first;
    int count;
    
    void make_empty()
    {
       first = NULL;
       count = 0;
    }
    
    int is_empty(void)
    {
       return count == 0;
    }
    
    int is_full(void)
    {
       return 0;
    }
    
    void push(int insert_value)
    {
       struct node *new_node;
    
       new_node=malloc(sizeof(struct node));
       if (new_node == NULL)  {
          printf("Error in push:  stack is full.\n");
          exit (EXIT_FAILURE);
       }
       new_node->data = insert_value;
       new_node->next = first;
    
       first = new_node;
       count++;
    }
    
    int pop(void)
    {
      struct node * top_node;
      int i;
    
      if (is_empty())  {
         printf("Error in pop: stack is empty.\n");
         exit (EXIT_FAILURE);
      }
    
      top_node=first;
      i = first->data;
      first = first->next;
      free(top_node);
      count--;
      return i;
    }
    
    void print_list()
    {
      struct node *ptr;
      if (first == NULL) {
        printf("List is empty.\n\n");
      }
    
      else  {
           printf("\n\nHere is your list:  \n");
           for (ptr=first;ptr;ptr=ptr->next)
               printf("%d\n",ptr->data);
           return;
      }
    }
    2 /accounts/student2/srbkpk/STACK/PJT5>more reverse.c
    /********************/
    /*  stack_main.c      */
    /********************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "stack.h"
    
    main()
    {
      char str[100];
      int i;
    
    
      make_empty();
    
      printf("Please enter a line of text to be printed in reverse:");
    
      while (i<MAX_SIZE && ((str[i] = getchar()) != '\n'))   {
         if (!is_full())
         push (str[i]);
         i++;
      }
    
      while (!is_empty()) {
         putc(pop(),stdout);
      }
    
      printf("\n");
    
      return 0;
    }
    
    
    3 /accounts/student2/srbkpk/STACK/PJT5>more stack.h
    /********************/
    /*  stack.h         */
    /********************/
    
    #ifndef STACK_H
    #define STACK_H
    
    #define MAX_SIZE 6
    
    void make_empty(void);
    int is_empty(void);
    int is_full(void);
    void push(int insert_value);
    int pop(void);
    void print_list();
    
    
    #endif
    
    4 /accounts/student2/srbkpk/STACK/PJT5>a.out
    Please enter a line of text to be printed in reverse:How are you doing?
    ra woH
    5 /accounts/student2/srbkpk/STACK/PJT5>^D
    script done on Sat Dec 01 17:24:12 2001
    Sue B.

    dazed and confused


  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Just a short post... before you get to changing over your stack to an array-based implementation.... look at the following code..
    Code:
    while (i<MAX_SIZE && ((str[i] = getchar()) != '\n'))   {
         if (!is_full())
         push (str[i]);
         i++;
      }
    
      while (!is_empty()) {
         putc(pop(),stdout);
      }
    The first routine is bad... it's complicated and much of it is unneccisary. The second routine is perfect. MAX_SIZE and str[] are features of your stack that should be internal to the stack. Just keep the code for pushing to the stack doing just what you want it to do, not really worring about the stack in any manner that doesn't work through a function...
    Code:
    while (!is_full() && ((i = getchar()) != '\n'))   {
         push (i);
      }
    
      while (!is_empty()) {
         putc(pop(),stdout);
      }
    Basically, you want your main() code to work no matter what implementation of stack you use. It's not main()'s job to worry about things like MAX_SIZE and whether or not the stack uses a buffer or linked list.
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Way Cool ! Thanks QuestionC. (i'd pay you money to be my tutor anytime)
    Sue B.

    dazed and confused


  8. #8
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Now what do I change to implement stack functions as arrays instead of struct node pointers ???

    Hints of anykind would help. I am trying different things, if I get something that looks good, I will post code.
    Sue B.

    dazed and confused


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A problem in part of array.
    By dirtydrummer in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 03:04 AM
  2. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 04:20 PM
  3. need help for stack problem again
    By bahareh in forum C++ Programming
    Replies: 20
    Last Post: 11-24-2006, 10:01 AM
  4. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM
  5. Array Stack Problem
    By Drew in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2001, 07:58 PM

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