ok, I keep messing with this and still not getting correct results.
Here's an example of a run of the program. ("st" is my executable file)
46 /accounts/student2/srbkpk/STACK/PJT5 st
Please enter any sentence :How are you doing?
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
1 deleted successfully.
It's as if it is popping off each letter of string pushed.
What the heck is the deal ???!!!???
Code:
/*******************************************/
/* reverse2.c */
/* */
/* purpose : ask user to input text and use stack */
/* functions to print output in reverse order */
/******************************************/
#include <stdio.h>
#include <stdlib.h>
#include "stack_h"
main()
{
int i;
printf ("Please enter any sentence :");
make_empty();
for ( ; i = getchar() != 100 ; ) {
if (is_full()) {
printf("Sentence is too short");
return 0;
}
else
push(i);
}
for ( ; !is_empty(); )
printf("%c",pop());
printf("\n");
return 0;
}
Here's my functions in a file labeled stack.c
BTW, I was told in class that I do not have to use char type funtions to make this stack work even when user inputs text. I don't understand how this is to work, but that is what instruction I was given.
Code:
/*******************************************/
/* stack.c */
/* */
/* purpose: all stack functions defined here */
/*******************************************/
#include "stack_h"
#define MAX_SIZE 6
struct node {
int data;
struct node * first;
};
int count;
void make_empty(void)
{
first = NULL;
count = 0;
}
int is_empty(void)
{
return count == 0;
}
int is_full(void)
{
return count == MAX_SIZE;
}
void push(int 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++;
}
int pop(void)
{
struct node * top_node;
char 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("Your count is %d\n",count);
printf("\n\nHere is your list: \n");
for (ptr=first;ptr;ptr=ptr->next)
printf("%d\n",ptr->data);
return;
}
}
And of course the header file with the prototypes of stack
functions (this is a necessity for this stack.c file and a similar file that will use an array instead of a struct pointer)
[code]
/********************/
/* stack.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
[code]