Skip to content

Instantly share code, notes, and snippets.

@dominicknguyen
Last active November 15, 2018 06:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominicknguyen/005cfb14a74ff02e648e to your computer and use it in GitHub Desktop.
Save dominicknguyen/005cfb14a74ff02e648e to your computer and use it in GitHub Desktop.
Linked List Stack in C
#include <stdio.h>
/*
Linked list implementation of Stack in C
I'm learning C so it's not that good!
Feel free to leave tips!
*/
typedef struct Node {
int value;
struct Node* next;
} Node;
Node* top;
void push(int value){
Node* temp = (Node*)malloc(sizeof(Node));
temp->value = value;
temp->next = top;
top = temp;
printStack();
}
void pop(){
Node* temp = top;
if(top == NULL) return;
top = temp->next;
free(temp);
printStack();
}
int peek(){
return top->value;
}
char* isEmpty(){
Node* temp = top;
int i = 0;
while(temp != NULL){
temp = temp->next;
i++;
}
return i == 0 ? "Empty" : "Not Empty";
}
void printStack(){
Node* temp = top;
while(temp != NULL){
printf("%d", temp->value);
temp = temp->next;
}
printf("\n");
}
int main(void){
printf("%s\n", isEmpty());
push(3);
push(4);
push(5);
pop();
printf("%s\n", isEmpty());
pop();
pop();
printf("%s\n", isEmpty());
return 0;
}
Copy link

ghost commented May 23, 2017

Thanks man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment