Stack functions as arrays instead of node pointers
Here's my first attempt to change from node pointers to arrays in linked list of stack.
Please be kind when criticizing and giving help.
I just don't have any examples in texts or lecture notes to know how to do this.
Code:
/*****************/
/* stack.c */
/****************/
#include <stdlib.h>
#include "stack.h"
struct node {
int data;
struct node * next; /* int str[MAX_SIZE]; */
};
struct node *first; /* struct node first[]; */
int count;
void make_empty()
{
first = NULL; /* first[0]=NULL; */
count = 0;
}
int is_empty(void)
{
return count == 0;
}
int is_full(void)
{
return 0;
}
void push(int insert_value)
{
int i = 0;
struct node *new_node; /* struct node new[]; */
new_node=malloc(sizeof(struct node)); /* new[0]=malloc(sizeof(struct node)); */
if (new_node == NULL) { /* if (new[i] = NULL) {
printf("Error in push: stack is full.\n");
exit (EXIT_FAILURE);
}
new_node->data = insert_value; /* new[i]->data=insert_value; */
new_node->next = first; /* new[i]++=first[i]; */
first = new_node; /* first[i] = new[i]; */
count++;
}
int pop(void)
{
struct node * top_node; /* struct node top[MAX_SIZE]; */
int i;
if (is_empty()) {
printf("Error in pop: stack is empty.\n");
exit (EXIT_FAILURE);
}
top_node=first; /* top[0] = first [i]; */
i = first->data; /* i = first[i]; */
first = first->next; /* first[i++]; */
free(top_node); /* free(top); */
count--;
return i;
}
void print_list()
{
struct node *ptr; / * struct node list; */
if (first == NULL) { /* if (first[0]=NULL) { */
printf("List is empty.\n\n");
}
else {
printf("\n\nHere is your list: \n");
for (ptr=first;ptr;ptr=ptr->next) /* for (list=first;list[i]<MAX_SIZE;list[i]++) */
printf("%d\n",ptr->data); /* printf("%d\n",list[i]); */
return;
}
}