Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define newline printf("\n")
- typedef struct node Node;
- struct node{
- int data;
- Node* next;
- };
- void print_linked_list(Node **head){
- Node *current;
- for(current=*head; current != NULL; current = current->next){
- printf("%d ", current->data);
- }
- newline;
- }
- void build_linked_list(Node **head_ptr)
- {
- int num;
- while(1)
- {
- // new_node->data = num;
- // new_node->next = NULL;
- // *head_ptr = new_node;
- scanf("%d",&num);
- if(num == -1)
- {
- break;
- }
- else
- {
- Node* new_node = (Node*)malloc(sizeof(Node));
- new_node->data = num;
- new_node->next = *head_ptr;
- *head_ptr = new_node;
- }
- }
- }
- int main()
- {
- Node* head = NULL;
- build_linked_list(&head);
- print_linked_list(&head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement