inputting line of text vs. integers in STACK

This is a discussion on inputting line of text vs. integers in STACK within the C Programming forums, part of the General Programming Boards category; I am quite confused and will get more info later if things are different than how I explain here.... but.... ...

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

    inputting line of text vs. integers in STACK

    I am quite confused and will get more info later if things are different than how I explain here....

    but....

    first I need a stack.c file that contains all stack processing functions and a node pointer (first) and an integer counter (count).

    second I need a stacka.c file that contains all the above functions but based on arrays instead of pointer.

    then I need to write program that uses either of these above created stack files and will allow user to input a line of text and uses a stack to print line in reverse order.

    Here's my questions:

    (1) how do I change the stack.c file below to allow text to be input instead of integer values??? I know I would change in the structure the int data; to a char variable, but I don't understand how to change the rest of the functions to work properly (guess: I have to name a char variable and everywhere the word data shows up, change to that variable name AND get rid of the switch statement and just ask user for input ,,, but how do I utilize the POP and PUSH functions then???)

    (2) how do you convert the structure pointer(s) to array(s)?? And have work just the same as with a pointer??

    (3) What are the make_empty, is_empty, and is_full functions for?? I am guessing they are for a fully running program to find if the array string is full and if so won't allow more text to be printed out or taken in AND find if the array string is empty AND make the string empty at first and then ask the user to input text to be evaluated in the stack functions.


    Here is my main function file
    Code:
    /********************/
    /*  stack_mn.c      */
    /********************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "stack.h"
    
    main()
    {
      int number, choice;
    
      for( ; ; )    {
    
        printf("\nChoose one of the following.\n\n"
               "0   To end program\n"
               "1   To add a value to list (Push)\n"
               "2   To remove top value from list (Pop)\n"
               "3   To print list \n"
               "4   To list # of nodes\n\n");
    
        printf("Enter your choice: ");
        scanf("%d", &choice);
        printf("\n");
    
        switch(choice)
        {
          case 0:
            printf("You have ended the program.\n"
                   "Good-bye !! \n\n");
            exit(0);
    
          case 1:
            printf("Enter a number to push: ");
            scanf("%d", &number);
            push(number);
            break;
    
          case 2:
            number = pop();
            printf("You just removed %d\n", number);
            break;
    
          case 3:
            print_list();
            break;
    
          case 4:
            printf("You have %d nodes in your list\n",count);
            break;
    
          default:
            printf("Illegal choice.\n");
        }
      }
      return 0;
    }

    Here is the stack.c file with all functions for stack implementation defined
    This is what I need help with ******

    Code:
    /*****************/
    /*  stack.c      */
    /****************/
    
    #include <stdlib.h>
    #include "stack.h"
    
    #define MAX 6
    
    struct node {
    
       int data;  
       struct node * next;
    };
    
    struct node *first = NULL;
    
    void make_empty(void)
    {
       first = NULL;
       count = 0;
    }
    
    int is_empty(void)
    {
       return count == 0;
    }
    
    int is_full(void)
    {
       return count == MAX;
    }
    
    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 * old_first;
      int i;
    
      if (is_empty())  {
         printf("Error in pop: stack is empty.\n");
         exit (EXIT_FAILURE);
      }
    
      printf("%d deleted successfully.\n",first->data);
      old_first=first;
      i = first->data;
      first = first->next;
      free(old_first);
      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;
      }
    }

    Here is the header file for the stack


    Code:
    /********************/
    /*  stack.h         */
    /********************/
    
    #include <stdlib.h>
    
    #ifndef STACK_H
    #define STACK_H
    
    void make_empty(void);
    int is_empty(void);
    int is_full(void);
    void push(int insert_value);
    int pop(void);
    void print_list();
    
    int count;
    
    #endif
    Sue B.

    dazed and confused


  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Let me summarize the changes neccisary to make this thing work for chars....
    Code:
    struct node {
    
       int data;  
       struct node * next;
    };
    becomes
    Code:
    struct node {
    
       char data;  
       struct node * next;
    };
    ...
    Code:
    void push(int insert_value)
    becomes
    Code:
    void push(char insert_value)
    And finally...
    Code:
    int pop(void)
    becomes
    Code:
    char pop(void)


    A char is pretty much the same thing as an int... except that it's smaller, and nothing inside the functions actually operates on these ints in a manner that would not work on chars, so all you need to change is how you pass them.

    In fact, you could probably even use the functions as they are now, since chars and ints can be used in such a similar fashion. (I said probably, no promises).

    The make_empty, is_empty, and is_full functions are just functions that every teacher likes to add to their definition of a stack. It just happens that these functions are usually quite easy to implement (although sometimes not so easy).

    Speaking of which, two functions in your stack.c program don't appear quite correct.
    1) make_empty doesn't actually free the memory that's allocated, so there's a memory leak.
    2) is_full, while it does seem to work, it doesn't seem neccisary. The stack there is being implemented with a linked list, so it shouldn't ever actually fill up. This function appears to be a leftover from an array implementation of a stack.
    (2) how do you convert the structure pointer(s) to array(s)?? And have work just the same as with a pointer??
    I'm afraid I don't understand this question, could you please explain?
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    To understand the problem you must first understand stacks.

    A stack is a data structure that has only one end. By that I mean you can only place on the top or take off from the top.

    This is commonly known as a LIFO structure. (Last In First Out)

    stacks always have a push(data) function.
    The job of this function is to check that the stack is not already full and place data on to the top of the stack. If the stack is full you can either reallocate a bigger stack or exit ungracefully.

    Stacks always have a data pop() function.
    The job of pop() is to remove the top item from the stack after checking whether or not the stack was empty.

    im sure now you see the use of isfull() and isempty()

    makeempty() is the function that creates the stack.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Ok, first, nix my 2nd question -- you helped me with that. (sorry for the confusion)

    I see I was close. So I change all my designation of int (with data type for the variable or the function) to char.

    I am not understanding how to utilize the POP and PUSH functions with the change. do I evaluate using getchar() and have a test for NULL (end of string)??
    Sue B.

    dazed and confused


  5. #5
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok here is what I got with changes from INTs to CHARs:



    Not sure best implementation of change needed to go from a switch statement to a prompt just for text string from user...(needing a hint, someone) And since it is not necessary to know the # of nodes (ie. characters) I could eliminate that call to see the value of the counter integer named "count". Right?

    But here's the file with main in it:

    Code:
    /********************/
    /*  stack_char_mn.c      */
    /********************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "stack_char.h"
    
    main()
    {
      char letter, choice;
    
      make_empty();
    
      for( ; ; )    {
    
        printf("\nChoose one of the following.\n\n"
               "0   To end program\n"
               "1   To add a value to list (Push)\n"
               "2   To remove top value from list (Pop)\n"
               "3   To print list \n"
               "4   To list # of nodes\n\n");
    
        printf("Enter your choice: ");
        scanf("%d", &choice);
        printf("\n");
    
        switch(choice)
        {
          case 0:
            printf("You have ended the program.\n"
                   "Good-bye !! \n\n");
            exit(0);
    
          case 1:
            printf("Enter a number to push: ");
            scanf("%c", &letter);
            push(letter);
            break;
    
          case 2:
            letter = pop();
            printf("You just removed %c\n", letter);
            break;
    
          case 3:
            print_list();
            break;
    
          case 4:
            printf("You have %d nodes in your list\n",count);
            break;
    
          default:
            printf("Illegal choice.\n");
        }
      }
      return 0;
    }

    Here's the stack file with functions a bit changed to CHARs
    now what's the problem with the make_empty function???
    Code:
    /*****************/
    /*  stack_char.c      */
    /****************/
    
    #include <stdlib.h>
    #include "stack_char.h"
    
    #define MAX 6
    
    struct node {
    
       char data;
       struct node * next;
    };
    
    struct node *first = NULL;
    
    void make_empty(void)
    {
       first = NULL;
       count = 0;
    }
    
    char is_empty(void)
    {
       return count == 0;
    }
    
    char is_full(void)
    {
       return count == MAX;
    }
    
    void push(char 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++;
    }
    
    char pop(void)
    {
      struct node * old_first;
      char i;
    
      if (is_empty())  {
         printf("Error in pop: stack is empty.\n");
         exit (EXIT_FAILURE);
      }
    
      printf("%d deleted successfully.\n",first->data);
      old_first=first;
      i = first->data;
      first = first->next;
      free(old_first);
      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;
      }
    }
    and the header file changes are shown here:

    Code:
    /********************/
    /*  stack_char.h         */
    /********************/
    
    #include <stdlib.h>
    
    #ifndef STACK_H
    #define STACK_H
    
    void make_empty(void);
    char is_empty(void);
    char is_full(void);
    void push(char insert_value);
    char pop(void);
    void print_list();
    
    int count;
    
    #endif
    Sue B.

    dazed and confused


  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    a small question but from your first question are you trying to do this....

    this is test string

    gnirts tset si siht

    or this...

    string test is this

    ???
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    the first output i believe is correct;
    we had an assignment a while ago needing the same type output but the program was not a stack.

    I have a sample file from someone else, but I don't get how to incorporate into my code. And I guess I would like a bit of explanation of what is going on....in laymen's terms

    admiral is a server name; so "admiral%" is UNIX prompt command line

    the code below shows
    (1) stack.c file
    (2) makefile
    (3) run of stack (executable is stack)
    (4) then output in reverse order
    (5) then makefile for the file stacka.c which is a stack file using array instead of pointer in structure
    (6) run of stacka (executable is stacka)
    (7) then output again in reverse order

    Code:
    admiral% more main.c
    /*PURPOSE: Test the stack implementation by reversing a string.*/
    
    
    #include "stack.h"
    
    int main(void)
    {
    {
       char     str[20];
       int      i;
       stack    top;
    
       make_empty(&top);
      printf("Please enter line to be reversed(20 char max.):");
      while(i<19 && (str[i] = getchar()) != '\n')
      {
       if (!Isfull(&top))
       push(str[i], &top);
       i++;
      }
    
      while(Isempty(&top) == 0)
       printf("%c", pop(&top));
    
    
      printf("\n");
    
      return 0;
    }         
      
    # makefile for stack 
    stack: main.o stack.o
            gcc main.o stack.o -o stack 
    main.o: main.c stack.h
            gcc -c main.c
    stack.o: stack.c stack.h
            gcc -c stack.c
    
    admiral% make 
    `stack' is up to date.
    
    admiral% stack
    Please enter line to be reversed(20 char max.):Hi, friendly grader
    
    redarg yldneirf ,iH 
    
    admiral% more makefile
    
    # makefile for stacka 
    stacka: main.o stacka.o
            gcc main.o stacka.o -o stacka 
    main.o: main.c stacka.h
            gcc -c main.c
    stacka.o: stacka.c stacka.h
            gcc -c stacka.c 
    
    admiral% make
    `stacka' is up to date.
    admiral% stacka
    
    Please enter line to be reversed(20 char max.):How are you today.
    
    .yadot uoy era woH
    Sue B.

    dazed and confused


  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I apologize, the problem that I had with the make_empty() was I was assuming it was meant to erase all the elements in the stack. I was incorrect however, please feel free to ignore me now.

    Although, it appears the you just want to know how to not just enter the letters one-at-a-time?
    Code:
    printf("Enter a number to push: ");
    for (;;)
    {
    // You do this part
    }
            break;
    And similarly for popping the characters. I hope that's not too vague...
    Callou collei we'll code the way
    Of prime numbers and pings!

  9. #9
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    the more I look at this stuff, the more confused I am getting.
    Someone else in my class did things a lot differently, it works, but there are things omitted that were expressedly mentioned in the instructions to be there. That is why I am confused.

    here are all my changes, but I am not getting any output after entering in a line of text when I run executable

    I think my problem is the call of the functions...all I did was change the int's to char's to accomodate text being input by user.

    Can someone give me a nudge in the right direction. I feel like I should have this already, but something is missing, obviously...

    Code:
    /**********************/
    /*  stack_char.c                 */
    /**********************/
    
    #include <stdlib.h>
    #include "stack_char.h"
    
    struct node {
    
       char data;
       struct node * next;
    };
    
    struct node *first = NULL;
    
    void make_empty(void)
    {
       first = NULL;
       count = 0;
    }
    
    char is_empty(void)
    {
       return count == 0;
    }
    
    char is_full(void)
    {
       return count == MAX;
    }
    
    void push(char c)
    {
       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 = c;
       new_node->next = first;
       first = new_node;
       count++;
    }
    
    char pop(void)
    {
      struct node * old_first;
      char i;
    
      if (is_empty())  {
         printf("Error in pop: stack is empty.\n");
         exit (EXIT_FAILURE);
      }
    
      printf("%d deleted successfully.\n",first->data);
      old_first=first;
      i = first->data;
      first = first->next;
      free(old_first);
      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;
      }
    }
    
    
    
    /********************/
    /*  stack_char.h         */
    /********************/
    
    #include <stdlib.h>
    
    #ifndef STACK_H
    #define STACK_H
    
    #define MAX 100
    
    void make_empty(void);
    char is_empty(void);
    char is_full(void);
    void push(char c);
    char pop(void);
    void print_list();
    
    int count;
    
    #endif
    
    
    
    
    
    /********************/
    /*  stack_main.c      */
    /********************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "stack_char.h"
    
    main()
    {
      char str[100];
      int i;
    
    
      make_empty();
    
      printf("Please enter a line of text to be printed in reverse:");
    
      while (i<MAX && (str[i] = getchar() != '\n'))   {
         if (is_full())
         push (str[i]);
         i++;
      }
    
      while (is_empty() == 0)
         printf("%c",pop());
    
      printf("\n");
    
      return 0;
    }
    
    
    Sue B.

    dazed and confused


  10. #10
    quah
    Guest
    Code:
    while (i<MAX && (str[i] = getchar() != '\n'))   {
         if (is_full())
         push (str[i]);
         i++;
      }
    Shouldn't that be:
    Code:
    if( !is_full() )
       push( str[i] );
    After all, why push to a full stack?

    Quzah.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Dammit! I typed my lame wrong. LMAO.

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

  12. #12
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Quzah, I guess you are right on that code bit,

    but how do I fix my output problem???
    Sue B.

    dazed and confused


  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, that probably will fix it. After all, if your stack is empty because that "if()" check was incorrect, resulting in nothing being pushed onto the stack, then nothing is going to print because your stack will remain empty.

    while(!is_empty()) putc( pop( ), stdout );

    Should work fine.

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

  14. #14
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    Still getting nothing to print after input of text string

    What's the significant difference between using
    printf ("%c", my_variable);
    putchar
    and
    putc

    new code:

    Code:
        
     
    /********************/
    /*  stack_main.c      */
    /********************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "stack_char.h"
    
    main()
    {
      char str[100];
      int i;
    
    
      make_empty();
    
      printf("Please enter a line of text to be printed in reverse:");
    
      while (i<MAX && (str[i] = getchar() != '\n'))   {
         if (is_full())
         push (str[i]);
         i++;
      }
    
      while (!is_empty()) {
         putc(pop(),stdout);
      }
    
      printf("\n");
    
      return 0;
    }

    Just in case, here's the stack file and stack header file

    Code:
     
    /**********************/
    /*  stack_char.c      */
    /**********************/
    
    #include <stdlib.h>
    #include "stack_char.h"
    
    struct node {
    
       char data;
       struct node * next;
    };
    
    struct node *first = NULL;
    
    void make_empty(void)
    {
       first = NULL;
       count = 0;
    }
    
    char is_empty(void)
    {
       return count == 0;
    }
    
    char is_full(void)
    {
       return count == MAX;
    }
    
    void push(char c)
    {
       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 = c;
       new_node->next = first;
       first = new_node;
       count++;
    }
    
    char pop(void)
    {
      struct node * old_first;
      char i;
    
      if (is_empty())  {
         printf("Error in pop: stack is empty.\n");
         exit (EXIT_FAILURE);
      }
    
      printf("%d deleted successfully.\n",first->data);
      old_first=first;
      i = first->data;
      first = first->next;
      free(old_first);
      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;
      }
    }
    
    
    
    
    
    
    /********************/
    /*  stack_char.h         */
    /********************/
    
    #include <stdlib.h>
    
    #ifndef STACK_H
    #define STACK_H
    
    #define MAX 100
    
    void make_empty(void);
    char is_empty(void);
    char is_full(void);
    void push(char c);
    char pop(void);
    void print_list();
    
    int count;
    
    #endif
    Sue B.

    dazed and confused


  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
      while (i<MAX && (str[i] = getchar() != '\n'))   {
         if (is_full())
         push (str[i]);
         i++;
      }
    You still haven't fixed this "if" check. It still will only use "push" if "is_full()" returns true. Fix that, and your problem is solved.

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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. how to reversed a line of text using stack??
    By yuki in forum C++ Programming
    Replies: 3
    Last Post: 11-10-2005, 02:35 PM
  3. reversed a line of text using stack??
    By yuki in forum C Programming
    Replies: 10
    Last Post: 11-10-2005, 10:37 AM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 03:12 PM
  5. text line termination
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-09-2001, 05:39 AM

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