Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

After I had my code for stack implementation by array code for stack implementation by array reviewed I wrote stack implementation by using pointers. Here's my code. Any suggestions for improvement are welcome.

I'll be adding updated code so please review the latest one. Obviously you are free to ignore this also. To skip reading this long question you can click here click here.

After I had my code for stack implementation by array reviewed I wrote stack implementation by using pointers. Here's my code. Any suggestions for improvement are welcome.

I'll be adding updated code so please review the latest one. Obviously you are free to ignore this also. To skip reading this long question you can click here.

After I had my code for stack implementation by array reviewed I wrote stack implementation by using pointers. Here's my code. Any suggestions for improvement are welcome.

I'll be adding updated code so please review the latest one. Obviously you are free to ignore this also. To skip reading this long question you can click here.

added 5343 characters in body
Source Link
Aseem Bansal
  • 2.3k
  • 3
  • 22
  • 37

I'll be adding updated code so please review the latest one. Obviously you are free to ignore this also. To skip reading this long question you can click here .

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum action {START, PUSH, POP, TOP, LENGTH, QUIT, END};
enum status {SUCCESS, FAILURE};
typedef struct node {
 void *data;
 struct node *lower;
} stack_node;
typedef struct stack {
 size_t elem_size;
 size_t stack_size;
 stack_node *top;
} stack_struct;
void clear_screen(void)
{
 system("cls");
}
static enum action get_user_action(void)
{
 int choice = START;
 do {
 clear_screen();
 printf("%d Push data\n"
 "%d Pop Data\n"
 "%d See the top of the stack\n"
 "%d See the length of the stack\n"
 "%d Exit\n\n"
 "Enter your choice -> ", PUSH, POP, TOP, LENGTH, QUIT);
 scanf("%d", &choice);
 } while (!(START < choice && choice < END));
 return (enum action) choice;
}
enum status stack_create(stack_struct **stack, size_t elem_size)
{
 (**stack).elem_size = elem_size;
 (**stack).stack_size = 0;
 (**stack).top = NULL;
 return SUCCESS;
}
enum status push(stack_struct **stack, void *data)
{
 stack_node *node = malloc(sizeof(node));
 if (node == NULL) {
 return FAILURE;
 }
 node->data = malloc(sizeof((**stack).elem_size));
 if (node->data == NULL) {
 return FAILURE;
 }
 memcpy(node->data, data, (**stack).elem_size);
 node->lower = (**stack).top;
 (**stack).top = node;
 (**stack).stack_size += 1;
 return SUCCESS;
}
enum status pop(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 stack_node *node = (**stack).top;
 memcpy(data, node->data, (**stack).elem_size);
 (**stack).top = node->lower;
 free(node->data);
 free(node);
 (**stack).stack_size -= 1;
 return SUCCESS;
}
enum status peek(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 memcpy(data, (**stack).top->data, (**stack).elem_size);
 return SUCCESS;
}
void stack_delete(stack_struct **stack)
{
 while ((**stack).top != NULL)
 {
 stack_node *node = (**stack).top;
 (**stack).top = (**stack).top->lower;
 free(node->data);
 free(node);
 }
 free(*stack);
}
int main(void)
{
 enum action choice;
 stack_struct *stack = malloc(sizeof(stack_struct));
 if (stack == NULL)
 {
 printf("Not enough memory\n");
 return 1;
 }
 stack_create(&stack, sizeof(int));
 while ((choice = get_user_action()) != QUIT) {
 clear_screen();
 int data;
 switch (choice) {
 case PUSH:
 printf("Enter data to be pushed -> ");
 scanf("%d", &data);
 if (push(&stack, &data) == SUCCESS) {
 printf("%d pushed onto the stack\n", *(int *)stack->top->data);
 } else {
 printf("Not enough memory\n");
 }
 break;
 case POP:
 if (pop(&stack, &data) == SUCCESS){
 printf("The data is %d\n", data);
 } else {
 printf("Stack underflow\n");
 }
 break;
 case TOP:
 if (peek(&stack, &data) == SUCCESS){
 printf("The data at top is %d\n", data);
 } else {
 printf("Nothing in the stack\n");
 }
 break;
 case LENGTH:
 printf("Length is %d\n", stack->stack_size);
 break;
 default:
 assert(!"You should not have reached this.\n");
 }
 getchar();
 getchar();
 }
 stack_delete(&stack);
}

Update 3

I added it after Corbin's review of my Update 2. So far I have just left the use of linked list for implementing the stack.

I haven't made 2 files yet but I have placed stack implementation at the top of the file for showing the logical separation of implementation code and client code. As this isn't a very big implementation I think the logical separation is what is needed here.

Here is my updated code.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum status {STACK_SUCCESS, STACK_FAILURE};
typedef struct node {
 void *data;
 struct node *lower;
} stack_node;
typedef struct stack {
 size_t elem_size;
 size_t stack_size;
 stack_node *top;
} STACK;
enum status stack_init(STACK *stack, size_t elem_size)
{
 stack->elem_size = elem_size;
 stack->stack_size = 0;
 stack->top = NULL;
 return STACK_SUCCESS;
}
stack_node * stack_node_create(STACK *stack)
{
 stack_node *node = malloc(sizeof(stack_node));
 if (node == NULL) {
 return NULL;
 }
 node->data = malloc(stack->elem_size);
 if (node->data == NULL) {
 free(node);
 return NULL;
 }
 return node;
}
enum status stack_push(STACK *stack, void *data)
{
 stack_node *node = stack_node_create(stack);
 if (node == NULL) {
 return STACK_FAILURE;
 }
 memcpy(node->data, data, stack->elem_size);
 node->lower = stack->top;
 stack->top = node;
 stack->stack_size += 1;
 return STACK_SUCCESS;
}
enum status stack_pop(STACK *stack, void *data)
{
 if (stack->top == NULL) {
 return STACK_FAILURE;
 }
 stack_node *node = stack->top;
 memcpy(data, node->data, stack->elem_size);
 stack->top = node->lower;
 free(node->data);
 free(node);
 stack->stack_size -= 1;
 return STACK_SUCCESS;
}
enum status stack_peek(STACK *stack, void *data)
{
 if (stack->top == NULL) {
 return STACK_FAILURE;
 }
 memcpy(data, stack->top->data, stack->elem_size);
 return STACK_SUCCESS;
}
void stack_cleanup(STACK *stack)
{
 while (stack->top != NULL) {
 stack_node *node = stack->top;
 stack->top = stack->top->lower;
 free(node->data);
 free(node);
 }
}
//Stack Implementation before this.
//Things that are used to use the stack are placed after this.
enum action {START, PUSH, POP, TOP, LENGTH, QUIT, END};
void clear_screen(void)
{
 system("cls");
}
static enum action get_user_action(void)
{
 int choice = START;
 do {
 clear_screen();
 printf("%d Push data\n"
 "%d Pop Data\n"
 "%d See the top of the stack\n"
 "%d See the length of the stack\n"
 "%d Exit\n\n"
 "Enter your choice -> ", PUSH, POP, TOP, LENGTH, QUIT);
 scanf("%d", &choice);
 } while (!(START < choice && choice < END));
 return (enum action) choice;
}
int main(void)
{
 enum action choice;
 STACK stack;
 stack_init(&stack, sizeof(int));
 while ((choice = get_user_action()) != QUIT) {
 clear_screen();
 int data;
 switch (choice) {
 case PUSH:
 printf("Enter data to be pushed -> ");
 scanf("%d", &data);
 if (stack_push(&stack, &data) == STACK_SUCCESS) {
 printf("%d pushed onto the stack\n", *(int *)stack.top->data);
 } else {
 printf("Not enough memory\n");
 }
 break;
 case POP:
 if (stack_pop(&stack, &data) == STACK_SUCCESS){
 printf("The data is %d\n", data);
 } else {
 printf("Stack underflow\n");
 }
 break;
 case TOP:
 if (stack_peek(&stack, &data) == STACK_SUCCESS){
 printf("The data at top is %d\n", data);
 } else {
 printf("Nothing in the stack\n");
 }
 break;
 case LENGTH:
 printf("Length is %d\n", stack.stack_size);
 break;
 default:
 assert(!"You should not have reached this.\n");
 }
 getchar();
 getchar();
 }
 stack_cleanup(&stack);
}

I'll be adding updated code so please review the latest one. Obviously you are free to ignore this also.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum action {START, PUSH, POP, TOP, LENGTH, QUIT, END};
enum status {SUCCESS, FAILURE};
typedef struct node {
 void *data;
 struct node *lower;
} stack_node;
typedef struct stack {
 size_t elem_size;
 size_t stack_size;
 stack_node *top;
} stack_struct;
void clear_screen(void)
{
 system("cls");
}
static enum action get_user_action(void)
{
 int choice = START;
 do {
 clear_screen();
 printf("%d Push data\n"
 "%d Pop Data\n"
 "%d See the top of the stack\n"
 "%d See the length of the stack\n"
 "%d Exit\n\n"
 "Enter your choice -> ", PUSH, POP, TOP, LENGTH, QUIT);
 scanf("%d", &choice);
 } while (!(START < choice && choice < END));
 return (enum action) choice;
}
enum status stack_create(stack_struct **stack, size_t elem_size)
{
 (**stack).elem_size = elem_size;
 (**stack).stack_size = 0;
 (**stack).top = NULL;
 return SUCCESS;
}
enum status push(stack_struct **stack, void *data)
{
 stack_node *node = malloc(sizeof(node));
 if (node == NULL) {
 return FAILURE;
 }
 node->data = malloc(sizeof((**stack).elem_size));
 if (node->data == NULL) {
 return FAILURE;
 }
 memcpy(node->data, data, (**stack).elem_size);
 node->lower = (**stack).top;
 (**stack).top = node;
 (**stack).stack_size += 1;
 return SUCCESS;
}
enum status pop(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 stack_node *node = (**stack).top;
 memcpy(data, node->data, (**stack).elem_size);
 (**stack).top = node->lower;
 free(node->data);
 free(node);
 (**stack).stack_size -= 1;
 return SUCCESS;
}
enum status peek(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 memcpy(data, (**stack).top->data, (**stack).elem_size);
 return SUCCESS;
}
void stack_delete(stack_struct **stack)
{
 while ((**stack).top != NULL)
 {
 stack_node *node = (**stack).top;
 (**stack).top = (**stack).top->lower;
 free(node->data);
 free(node);
 }
 free(*stack);
}
int main(void)
{
 enum action choice;
 stack_struct *stack = malloc(sizeof(stack_struct));
 if (stack == NULL)
 {
 printf("Not enough memory\n");
 return 1;
 }
 stack_create(&stack, sizeof(int));
 while ((choice = get_user_action()) != QUIT) {
 clear_screen();
 int data;
 switch (choice) {
 case PUSH:
 printf("Enter data to be pushed -> ");
 scanf("%d", &data);
 if (push(&stack, &data) == SUCCESS) {
 printf("%d pushed onto the stack\n", *(int *)stack->top->data);
 } else {
 printf("Not enough memory\n");
 }
 break;
 case POP:
 if (pop(&stack, &data) == SUCCESS){
 printf("The data is %d\n", data);
 } else {
 printf("Stack underflow\n");
 }
 break;
 case TOP:
 if (peek(&stack, &data) == SUCCESS){
 printf("The data at top is %d\n", data);
 } else {
 printf("Nothing in the stack\n");
 }
 break;
 case LENGTH:
 printf("Length is %d\n", stack->stack_size);
 break;
 default:
 assert(!"You should not have reached this.\n");
 }
 getchar();
 getchar();
 }
 stack_delete(&stack);
}

I'll be adding updated code so please review the latest one. Obviously you are free to ignore this also. To skip reading this long question you can click here .

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum action {START, PUSH, POP, TOP, LENGTH, QUIT, END};
enum status {SUCCESS, FAILURE};
typedef struct node {
 void *data;
 struct node *lower;
} stack_node;
typedef struct stack {
 size_t elem_size;
 size_t stack_size;
 stack_node *top;
} stack_struct;
void clear_screen(void)
{
 system("cls");
}
static enum action get_user_action(void)
{
 int choice = START;
 do {
 clear_screen();
 printf("%d Push data\n"
 "%d Pop Data\n"
 "%d See the top of the stack\n"
 "%d See the length of the stack\n"
 "%d Exit\n\n"
 "Enter your choice -> ", PUSH, POP, TOP, LENGTH, QUIT);
 scanf("%d", &choice);
 } while (!(START < choice && choice < END));
 return (enum action) choice;
}
enum status stack_create(stack_struct **stack, size_t elem_size)
{
 (**stack).elem_size = elem_size;
 (**stack).stack_size = 0;
 (**stack).top = NULL;
 return SUCCESS;
}
enum status push(stack_struct **stack, void *data)
{
 stack_node *node = malloc(sizeof(node));
 if (node == NULL) {
 return FAILURE;
 }
 node->data = malloc(sizeof((**stack).elem_size));
 if (node->data == NULL) {
 return FAILURE;
 }
 memcpy(node->data, data, (**stack).elem_size);
 node->lower = (**stack).top;
 (**stack).top = node;
 (**stack).stack_size += 1;
 return SUCCESS;
}
enum status pop(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 stack_node *node = (**stack).top;
 memcpy(data, node->data, (**stack).elem_size);
 (**stack).top = node->lower;
 free(node->data);
 free(node);
 (**stack).stack_size -= 1;
 return SUCCESS;
}
enum status peek(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 memcpy(data, (**stack).top->data, (**stack).elem_size);
 return SUCCESS;
}
void stack_delete(stack_struct **stack)
{
 while ((**stack).top != NULL)
 {
 stack_node *node = (**stack).top;
 (**stack).top = (**stack).top->lower;
 free(node->data);
 free(node);
 }
 free(*stack);
}
int main(void)
{
 enum action choice;
 stack_struct *stack = malloc(sizeof(stack_struct));
 if (stack == NULL)
 {
 printf("Not enough memory\n");
 return 1;
 }
 stack_create(&stack, sizeof(int));
 while ((choice = get_user_action()) != QUIT) {
 clear_screen();
 int data;
 switch (choice) {
 case PUSH:
 printf("Enter data to be pushed -> ");
 scanf("%d", &data);
 if (push(&stack, &data) == SUCCESS) {
 printf("%d pushed onto the stack\n", *(int *)stack->top->data);
 } else {
 printf("Not enough memory\n");
 }
 break;
 case POP:
 if (pop(&stack, &data) == SUCCESS){
 printf("The data is %d\n", data);
 } else {
 printf("Stack underflow\n");
 }
 break;
 case TOP:
 if (peek(&stack, &data) == SUCCESS){
 printf("The data at top is %d\n", data);
 } else {
 printf("Nothing in the stack\n");
 }
 break;
 case LENGTH:
 printf("Length is %d\n", stack->stack_size);
 break;
 default:
 assert(!"You should not have reached this.\n");
 }
 getchar();
 getchar();
 }
 stack_delete(&stack);
}

Update 3

I added it after Corbin's review of my Update 2. So far I have just left the use of linked list for implementing the stack.

I haven't made 2 files yet but I have placed stack implementation at the top of the file for showing the logical separation of implementation code and client code. As this isn't a very big implementation I think the logical separation is what is needed here.

Here is my updated code.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum status {STACK_SUCCESS, STACK_FAILURE};
typedef struct node {
 void *data;
 struct node *lower;
} stack_node;
typedef struct stack {
 size_t elem_size;
 size_t stack_size;
 stack_node *top;
} STACK;
enum status stack_init(STACK *stack, size_t elem_size)
{
 stack->elem_size = elem_size;
 stack->stack_size = 0;
 stack->top = NULL;
 return STACK_SUCCESS;
}
stack_node * stack_node_create(STACK *stack)
{
 stack_node *node = malloc(sizeof(stack_node));
 if (node == NULL) {
 return NULL;
 }
 node->data = malloc(stack->elem_size);
 if (node->data == NULL) {
 free(node);
 return NULL;
 }
 return node;
}
enum status stack_push(STACK *stack, void *data)
{
 stack_node *node = stack_node_create(stack);
 if (node == NULL) {
 return STACK_FAILURE;
 }
 memcpy(node->data, data, stack->elem_size);
 node->lower = stack->top;
 stack->top = node;
 stack->stack_size += 1;
 return STACK_SUCCESS;
}
enum status stack_pop(STACK *stack, void *data)
{
 if (stack->top == NULL) {
 return STACK_FAILURE;
 }
 stack_node *node = stack->top;
 memcpy(data, node->data, stack->elem_size);
 stack->top = node->lower;
 free(node->data);
 free(node);
 stack->stack_size -= 1;
 return STACK_SUCCESS;
}
enum status stack_peek(STACK *stack, void *data)
{
 if (stack->top == NULL) {
 return STACK_FAILURE;
 }
 memcpy(data, stack->top->data, stack->elem_size);
 return STACK_SUCCESS;
}
void stack_cleanup(STACK *stack)
{
 while (stack->top != NULL) {
 stack_node *node = stack->top;
 stack->top = stack->top->lower;
 free(node->data);
 free(node);
 }
}
//Stack Implementation before this.
//Things that are used to use the stack are placed after this.
enum action {START, PUSH, POP, TOP, LENGTH, QUIT, END};
void clear_screen(void)
{
 system("cls");
}
static enum action get_user_action(void)
{
 int choice = START;
 do {
 clear_screen();
 printf("%d Push data\n"
 "%d Pop Data\n"
 "%d See the top of the stack\n"
 "%d See the length of the stack\n"
 "%d Exit\n\n"
 "Enter your choice -> ", PUSH, POP, TOP, LENGTH, QUIT);
 scanf("%d", &choice);
 } while (!(START < choice && choice < END));
 return (enum action) choice;
}
int main(void)
{
 enum action choice;
 STACK stack;
 stack_init(&stack, sizeof(int));
 while ((choice = get_user_action()) != QUIT) {
 clear_screen();
 int data;
 switch (choice) {
 case PUSH:
 printf("Enter data to be pushed -> ");
 scanf("%d", &data);
 if (stack_push(&stack, &data) == STACK_SUCCESS) {
 printf("%d pushed onto the stack\n", *(int *)stack.top->data);
 } else {
 printf("Not enough memory\n");
 }
 break;
 case POP:
 if (stack_pop(&stack, &data) == STACK_SUCCESS){
 printf("The data is %d\n", data);
 } else {
 printf("Stack underflow\n");
 }
 break;
 case TOP:
 if (stack_peek(&stack, &data) == STACK_SUCCESS){
 printf("The data at top is %d\n", data);
 } else {
 printf("Nothing in the stack\n");
 }
 break;
 case LENGTH:
 printf("Length is %d\n", stack.stack_size);
 break;
 default:
 assert(!"You should not have reached this.\n");
 }
 getchar();
 getchar();
 }
 stack_cleanup(&stack);
}
added 4723 characters in body
Source Link
Aseem Bansal
  • 2.3k
  • 3
  • 22
  • 37

Update 1

I think I have made the stack able to use any data type. This is what I have made. I also added a function for deleting the stack stack_delete.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum action {START, PUSH, POP, TOP, LENGTH, QUIT, END};
enum status {SUCCESS, FAILURE};
typedef struct node {
 void *data;
 struct node *lower;
} stack_node;
typedef struct stack {
 size_t elem_size;
 size_t stack_size;
 stack_node *top;
} stack_struct;
void clear_screen(void)
{
 system("cls");
}
static enum action get_user_action(void)
{
 int choice = START;
 do {
 clear_screen();
 printf("%d Push data\n"
 "%d Pop Data\n"
 "%d See the top of the stack\n"
 "%d See the length of the stack\n"
 "%d Exit\n\n"
 "Enter your choice -> ", PUSH, POP, TOP, LENGTH, QUIT);
 scanf("%d", &choice);
 } while (!(START < choice && choice < END));
 return (enum action) choice;
}
enum status stack_create(stack_struct **stack, size_t elem_size)
{
 (**stack).elem_size = elem_size;
 (**stack).stack_size = 0;
 (**stack).top = NULL;
 return SUCCESS;
}
enum status push(stack_struct **stack, void *data)
{
 stack_node *node = malloc(sizeof(node));
 if (node == NULL) {
 return FAILURE;
 }
 node->data = malloc(sizeof((**stack).elem_size));
 if (node->data == NULL) {
 return FAILURE;
 }
 memcpy(node->data, data, (**stack).elem_size);
 node->lower = (**stack).top;
 (**stack).top = node;
 (**stack).stack_size += 1;
 return SUCCESS;
}
enum status pop(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 stack_node *node = (**stack).top;
 memcpy(data, node->data, (**stack).elem_size);
 (**stack).top = node->lower;
 free(node->data);
 free(node);
 (**stack).stack_size -= 1;
 return SUCCESS;
}
enum status peek(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 memcpy(data, (**stack).top->data, (**stack).elem_size);
 return SUCCESS;
}
void stack_delete(stack_struct **stack)
{
 while ((**stack).top != NULL)
 {
 stack_node *node = (**stack).top;
 (**stack).top = (**stack).top->lower;
 free(node->data);
 free(node);
 }
 free(*stack);
}
int main(void)
{
 enum action choice;
 stack_struct *stack = malloc(sizeof(stack_struct));
 if (stack == NULL)
 {
 printf("Not enough memory\n");
 return 1;
 }
 stack_create(&stack, sizeof(int));
 while ((choice = get_user_action()) != QUIT) {
 clear_screen();
 int data;
 switch (choice) {
 case PUSH:
 printf("Enter data to be pushed -> ");
 scanf("%d", &data);
 if (push(&stack, &data) == SUCCESS) {
 printf("%d pushed onto the stack\n", *(int *)stack->top->data);
 } else {
 printf("Not enough memory\n");
 }
 break;
 case POP:
 if (pop(&stack, &data) == SUCCESS){
 printf("The data is %d\n", data);
 } else {
 printf("Stack underflow\n");
 }
 break;
 case TOP:
 if (peek(&stack, &data) == SUCCESS){
 printf("The data at top is %d\n", data);
 } else {
 printf("Nothing in the stack\n");
 }
 break;
 case LENGTH:
 printf("Length is %d\n", stack->stack_size);
 break;
 default:
 assert(!"You should not have reached this.\n");
 }
 getchar();
 getchar();
 }
 stack_delete(&stack);
}

Update 1

I think I have made the stack able to use any data type. This is what I have made. I also added a function for deleting the stack stack_delete.

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum action {START, PUSH, POP, TOP, LENGTH, QUIT, END};
enum status {SUCCESS, FAILURE};
typedef struct node {
 void *data;
 struct node *lower;
} stack_node;
typedef struct stack {
 size_t elem_size;
 size_t stack_size;
 stack_node *top;
} stack_struct;
void clear_screen(void)
{
 system("cls");
}
static enum action get_user_action(void)
{
 int choice = START;
 do {
 clear_screen();
 printf("%d Push data\n"
 "%d Pop Data\n"
 "%d See the top of the stack\n"
 "%d See the length of the stack\n"
 "%d Exit\n\n"
 "Enter your choice -> ", PUSH, POP, TOP, LENGTH, QUIT);
 scanf("%d", &choice);
 } while (!(START < choice && choice < END));
 return (enum action) choice;
}
enum status stack_create(stack_struct **stack, size_t elem_size)
{
 (**stack).elem_size = elem_size;
 (**stack).stack_size = 0;
 (**stack).top = NULL;
 return SUCCESS;
}
enum status push(stack_struct **stack, void *data)
{
 stack_node *node = malloc(sizeof(node));
 if (node == NULL) {
 return FAILURE;
 }
 node->data = malloc(sizeof((**stack).elem_size));
 if (node->data == NULL) {
 return FAILURE;
 }
 memcpy(node->data, data, (**stack).elem_size);
 node->lower = (**stack).top;
 (**stack).top = node;
 (**stack).stack_size += 1;
 return SUCCESS;
}
enum status pop(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 stack_node *node = (**stack).top;
 memcpy(data, node->data, (**stack).elem_size);
 (**stack).top = node->lower;
 free(node->data);
 free(node);
 (**stack).stack_size -= 1;
 return SUCCESS;
}
enum status peek(stack_struct **stack, void *data)
{
 if ((**stack).top == NULL) {
 return FAILURE;
 }
 memcpy(data, (**stack).top->data, (**stack).elem_size);
 return SUCCESS;
}
void stack_delete(stack_struct **stack)
{
 while ((**stack).top != NULL)
 {
 stack_node *node = (**stack).top;
 (**stack).top = (**stack).top->lower;
 free(node->data);
 free(node);
 }
 free(*stack);
}
int main(void)
{
 enum action choice;
 stack_struct *stack = malloc(sizeof(stack_struct));
 if (stack == NULL)
 {
 printf("Not enough memory\n");
 return 1;
 }
 stack_create(&stack, sizeof(int));
 while ((choice = get_user_action()) != QUIT) {
 clear_screen();
 int data;
 switch (choice) {
 case PUSH:
 printf("Enter data to be pushed -> ");
 scanf("%d", &data);
 if (push(&stack, &data) == SUCCESS) {
 printf("%d pushed onto the stack\n", *(int *)stack->top->data);
 } else {
 printf("Not enough memory\n");
 }
 break;
 case POP:
 if (pop(&stack, &data) == SUCCESS){
 printf("The data is %d\n", data);
 } else {
 printf("Stack underflow\n");
 }
 break;
 case TOP:
 if (peek(&stack, &data) == SUCCESS){
 printf("The data at top is %d\n", data);
 } else {
 printf("Nothing in the stack\n");
 }
 break;
 case LENGTH:
 printf("Length is %d\n", stack->stack_size);
 break;
 default:
 assert(!"You should not have reached this.\n");
 }
 getchar();
 getchar();
 }
 stack_delete(&stack);
}
Tweeted twitter.com/#!/StackCodeReview/status/362675714350260224
Source Link
Aseem Bansal
  • 2.3k
  • 3
  • 22
  • 37
Loading
lang-c

AltStyle によって変換されたページ (->オリジナル) /