-
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]
-
Code:
for ( ; i = getchar() != 100 ; ) {
if (is_full()) {
printf("Sentence is too short");
return 0;
}
else
push(i);
}
for ( ; !is_empty(); )
printf("%c",pop());
No wonder you're dazed and confused! Look at the above code!
1) Why, if the stack is full, is the string "too short"? This makes no sense.
2) See the second loop? What that does, is once you're done pushing all of the characters, it loops through and pops them all back off. Of course it's going to act as though it's popping them off, because it is! That's what you've told it to do!
Your code is functioning exactly the way you want it to, according to the above snippet.
Quzah.
-
Hohum... the internet just ate a perfectly good post... okay, time to summarize.
1)I'm pretty sure this should be "stack.h" Both of your c files have this problem.
2) In file stack.h:
int count;
This is gonna give you problems since int count is also globally declares in stack.c, so delete that line in stack.h
3) For some reason, all the information in your stack is the int value 1... not the ascii character 1. When the stack is popped, it hits this line...
Code:
printf("%d deleted successfully.\n",first->data);
Which is the output we are seeing. The ASCII value for 1 is a non-printable character however, so the printf for each pop in main can't print anything.
The problem with this is... well, it doesn't make any sense. Your push function doesn't look like it's putting all 1s into the stack, so I suspect that you're using some old files here. Try recompiling all your files.
4) Other than that, your program should work fine, except for the fact that your isfull() function will return true after 6 elements have been put into the stack. Really, since your stack will never be full (since it's a linked list), it'd be best to look like this:
Code:
int is_full(void)
{
return 0; // 0 for false, since it's never full.
}