Skip to content

Instantly share code, notes, and snippets.

@raiyansarker
Created February 22, 2026 09:10
Show Gist options
  • Select an option

  • Save raiyansarker/8eb91d9c0eae4c603dc69257e9d69e61 to your computer and use it in GitHub Desktop.

Select an option

Save raiyansarker/8eb91d9c0eae4c603dc69257e9d69e61 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
void push(Node **head, int data);
int pop(Node **head);
int peak(Node *head);
int main() {
Node *head = NULL;
push(&head, 1);
push(&head, 2);
push(&head, 5);
push(&head, 3);
printf("Peak %d\n", peak(head));
printf("%d\n", pop(&head));
printf("%d\n", pop(&head));
printf("Peak %d\n", peak(head));
printf("%d\n", pop(&head));
printf("%d\n", pop(&head));
printf("%d\n", pop(&head));
return 0;
}
void push(Node **head, int data) {
Node *new = (Node*)malloc(sizeof(Node));
new->data = data;
new->next = *head;
*head = new;
}
int pop(Node **head) {
if (*head == NULL) return -1;
Node *t = *head;
int val = t->data;
*head = t->next;
free(t);
return val;
}
int peak(Node *head) {
if (head == NULL) return -1;
return head->data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment