Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Node{
- int val;
- struct Node *next;
- };
- void insert(struct Node* head, int value);
- void print(struct Node *head);
- int main(){
- struct Node *head;
- head = (struct Node*)malloc(sizeof(struct Node));
- int i = 0;
- for(i = 0; i<10; i++)
- insert(head, i*2);
- print(head);
- return 0;
- }
- void insert(struct Node* head, int value){
- if(head == NULL){
- printf("NULL");
- head->val = value;
- head->next = NULL;
- }
- else {
- printf("nULL2\n");
- struct Node *tmp = (struct Node*)malloc(sizeof(struct Node));
- tmp->val = value;
- tmp->next = NULL;
- while(head->next != NULL){
- head = head->next;
- }
- head->next = tmp;
- }
- }
- void print(struct Node *head){
- struct Node *current = head;
- while(current != NULL){
- printf("%d\n", current->val);
- current = current->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement