Skip to content

Instantly share code, notes, and snippets.

@cwchentw
Last active Apr 29, 2018
Embed
What would you like to do?
Stack Manipulations without Function
/*
Stack Manipulations without Function
Author: Michael Chen, 2018.
License: MIT.
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node Node;
struct node {
int data;
Node *next;
};
typedef struct stack Stack;
struct stack {
Node *top;
};
int main()
{
// Program state.
bool failed = false;
// Create a Stack object.
Stack *st = malloc(sizeof(Stack));
if (!st) {
return EXIT_FAILURE;
}
st->top = NULL;
// Insert an element.
{
// Create a new node.
Node *node = malloc(sizeof(Node));
if (!node) {
perror("Failed to allocate a node");
failed = true;
goto STACK_FREE;
}
node->data = 9;
node->next = NULL;
// Point st->top to node.
st->top = node;
}
// Peek top element.
if (st->top->data != 9) {
perror("It should be 9");
failed = true;
goto STACK_FREE;
}
// Insert another element.
{
// Create a new node.
Node *node = malloc(sizeof(Node));
if (!node) {
perror("Failed to allocate a node");
failed = true;
goto STACK_FREE;
}
node->data = 5;
node->next = NULL;
// Update st->top.
node->next = st->top;
st->top = node;
}
// Peek top element again.
if (st->top->data != 5) {
perror("It should be 5");
failed = true;
goto STACK_FREE;
}
// Pop top element.
do {
Node *curr = st->top;
if (!curr) {
break;
}
// Update st->top.
st->top = curr->next;
if (curr->data != 5) {
perror("It should be 5");
failed = true;
goto STACK_FREE;
}
free(curr);
} while (0);
// Peek top element again.
if (st->top->data != 9) {
perror("It should be 9");
failed = true;
goto STACK_FREE;
}
STACK_FREE:
{
Node *curr = st->top;
Node *temp;
while (curr) {
temp = curr;
curr = curr->next;
free(temp);
}
free(st);
}
if (failed) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment