Skip to content

Instantly share code, notes, and snippets.

@kriss-u
Last active August 29, 2019 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriss-u/2b2617f66d574ee3bb408e73f6411cf7 to your computer and use it in GitHub Desktop.
Save kriss-u/2b2617f66d574ee3bb408e73f6411cf7 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
// Self-referential structure
struct stackNode {
int data; // A integer data in the stack
struct stackNode *nextPtr; // Pointer to next element of stack
}; // end struct stackNode
typedef struct stackNode StackNode; // alias for the struct
typedef StackNode *StackNodePtr; // alias for StackNode*
// Declarations
// Pointer to the top plays the main role
void push(StackNodePtr *topPtr, int info);
int pop(StackNodePtr *topPtr);
int isEmpty(StackNodePtr topPtr);
void showStack(StackNodePtr currentPtr);
void showMenu(void);
int main() {
// Points to stack top
StackNodePtr stackPtr = NULL;
// User's choice
unsigned int ch;
// User's entered character
int item;
// Display the menu
showMenu();
printf("? ");
scanf("%u", &ch);
// Loop unless user chooses 3
while(ch != 3) {
switch(ch) {
case 1:
printf("Enter an integer: ");
scanf("%d", &item);
// insert the item in the top of stack
push(&stackPtr, item);
showStack(stackPtr);
break;
case 2:
// if stack is not empty
if(!isEmpty(stackPtr)) {
printf("The popped integer is %d.\n", pop(&stackPtr));
} // end if
showStack(stackPtr);
break;
default:
printf("Invalid choice.\n");
showMenu();
break;
}
showMenu();
printf("? ");
scanf("%u", &ch);
}
printf("Thank You!\n");
return 0;
}
// Display Menu Function
void showMenu(void) {
printf("Enter your choice:\n"
" 1 to push integer into the stack.\n"
" 2 to pop integer from the stack.\n"
" 3 to end" );
}
// insert a node to the top of stack
void push(StackNodePtr *topPtr, int value) {
StackNodePtr newPtr; // pointer to new node
newPtr = malloc(sizeof(StackNode)); // Allocate memory for StackNode
// if allocated
if (newPtr != NULL) {
newPtr->data = value; // put value in the node
newPtr->nextPtr = *topPtr;
*topPtr = newPtr; // new node is top
} // end if
else {
printf("%d is not inserted. Memory error!\n", value);
}
}
// pop from the stack
int pop(StackNodePtr *topPtr) {
StackNodePtr tmpPtr; // temporary node pointer
int popValue; // node value
tmpPtr = *topPtr;
popValue = tmpPtr->data;
*topPtr = tmpPtr->nextPtr;
free(tmpPtr);
return popValue;
}
// return 1 if the stack is empty, otherwise 0
int isEmpty(StackNodePtr topPtr) {
return topPtr == NULL;
}
// show the list
void showStack(StackNodePtr currentPtr) {
// if stack is empty
if (isEmpty(currentPtr)) {
printf("The stack is empty!\n");
}
else {
printf("The stack is: \n");
// while not the end of stack
while(currentPtr != NULL) {
printf("%d ==> ", currentPtr->data);
currentPtr = currentPtr->nextPtr;
}
printf("NULL\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment