1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include <stdio.h> #include <stdlib.h>
typedef struct node { int val; struct node *next; } node;
node *create(int data) { struct node *new_node = (node *)malloc(sizeof(node)); new_node->val = data; new_node->next = NULL;
return new_node; } void print_list(node *head) { node *current = head;
while (current != NULL) { printf("%d \n", current->val); current = current->next; } }
void push(node *head, int val) { node *current = head; while (current->next != NULL) { current = current->next; } current->next = (node *)malloc(sizeof(node)); current->next->val = val; current->next->next = NULL; }
node *insertFront(node *head, int data) { node *new_node = create(data); new_node->next = head;
return new_node; }
int pop(node *head, int val) { int retval = -1; if (head == NULL) { return retval; } retval = head->val; node *next_node = head->next; head->val = next_node->val; head->next = next_node->next;
return retval; } int main() { node *head = NULL; head = (node *)malloc(sizeof(node)); head->val = 1; head->next = NULL;
printf("%d", head->val);
return 0; }
|