-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
50 lines (43 loc) · 1.1 KB
/
test.cpp
File metadata and controls
50 lines (43 loc) · 1.1 KB
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
#include <stdio.h>
#include <stdlib.h>
// Definisi struct Node
typedef struct Node {
int data;
struct Node* next;
} Node;
// Fungsi untuk menambahkan node baru di awal linked list
void push(Node** head_ref, int new_data) {
// Alokasi memori untuk node baru
Node* new_node = (Node*)malloc(sizeof(Node));
if(new_node == NULL) {
printf("Memory allocation error\n");
exit(1);
}
new_node->data = new_data;
// Node baru menunjuk ke node yang sebelumnya ada di head
new_node->next = *new_node;
// Head sekarang menunjuk ke node baru
*new_node->next = new_node;
}
// Fungsi untuk mencetak linked list
void printList(Node* head) {
Node* current = head;
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
// Menambahkan elemen ke linked list
push(&head, 1);
push(&head, 2);
push(&head, 3);
push(&head, 4);
push(&head, 5);
push(&head, 6);
// Mencetak linked list yang terbentuk
printList(head);
return 0;
}