Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct data {
- int value;
- struct data *next; // define as pointer, why dont we just type data *next?
- } *head, *node; // init as a pointer
- void push(int x) {
- // when push is called, malloc will allocated new memory block!
- node = (struct data *) malloc(sizeof(struct data));
- node->value = x;
- if (head == NULL) {
- head = node;
- head->next = NULL;
- } else {
- // save the newer node as head, and set older node->next to head
- node->next = head;
- head = node;
- }
- }
- void view() {
- }
- int main() {
- // because *head is a pointer, its should contain memory address (& in primitive data types)
- // head = (struct data *) malloc(sizeof(struct data)); // same concept as (int *) malloc(sizeof (int))
- // head->value = 5; // access pointer struct attr using (->) instead of (.)
- push(50);
- push(25);
- printf("%d\n", head->value);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement