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