Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct node {
- int value;
- struct node *next;
- } Node;
- int size(const Node *head);
- void print_stack(const Node* head);
- int top(const Node* head);
- int pop(Node **head);
- void push(Node **head, int value);
- int main(){
- int i;
- Node *head = NULL, *out;
- for (i = 0; i < 10; i++) {
- push(&head, i);
- }
- //out = head;
- print_stack(head);
- //printf("size = %d\n", out -> value);
- /*
- while (head) {
- printf("%d ", top(head));
- printf("%d ", pop(&head));
- }
- */
- return 0;
- }
- void push(Node **head, int value){
- Node *temp = malloc(sizeof(Node));
- temp -> next = *head;
- temp -> value = value;
- *head = temp;
- }
- int pop(Node **head){
- Node *out;
- int value;
- if ( *head == NULL ) return 0;
- out = *head;
- *head = (*head) -> next;
- value = out -> value;
- free(out);
- return value;
- }
- int top(const Node* head) {
- if ( head == NULL ) return 0;
- return head->value;
- }
- void print_stack(const Node* head) {
- printf("stack > ");
- Node *n;
- n = head;
- while (n != NULL){
- printf("%d ", n -> value);
- n = n -> next;
- }
- printf("\n");
- }
- int size(const Node *head) {
- int size = 0;
- while (head) {
- size++;
- head = head->next;
- }
- return size;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement