Advertisement
Guest User

linked list v2

a guest
May 29th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. /*
  2.  * linkedlistv2.c
  3.  *
  4.  *  Created on: May 29, 2015
  5.  *      Author: trongkha
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. struct node {
  12.     int data;
  13.     struct node* next;
  14. };
  15. void show_list(struct node* head) {
  16.     struct node* current = head;
  17.     while (current != NULL) {
  18.         printf("data: %d \n", current->data);
  19.         current = current->next;
  20.     }
  21. }
  22. void Push(struct node** headRef, int data) {
  23.     struct node* newNode = malloc(sizeof(struct node));
  24.     newNode->data = data;
  25.     newNode->next = *headRef; // The '*' to dereferences back to the real head
  26.     *headRef = newNode;
  27. }
  28.  
  29.  
  30. void insert_after(struct node* head, struct node* inserted_node, int position) {
  31.     struct node* left = NULL;
  32.     struct node* current = head;
  33.     if (head == NULL)
  34.         return;
  35.     int i = 0;
  36.  
  37.     for (i = 0; i <= position; i++) {
  38.         left = current;
  39.         current = current->next;
  40.     }
  41.  
  42.     left->next = inserted_node;
  43.     inserted_node->next = current;
  44. }
  45.  
  46. void clone_node(struct node* head, int node_position, int times) {
  47.     int i = 0;
  48.     struct node* current = head;
  49.     if (head == NULL)
  50.         return;
  51.     // move to node clone
  52.  
  53.     for (i = 0; i < node_position; i++) {
  54.         current = current->next;
  55.     }
  56.     // clone times
  57.  
  58.     for (i = 0; i < times; i++) {
  59.         struct node* node_clone = malloc(sizeof(struct node));
  60.         memcpy(node_clone, current, sizeof(struct node));
  61.         insert_after(head, node_clone, node_position);
  62.  
  63.     }
  64.  
  65. }
  66.  
  67. struct node* build_node() {
  68.     struct node* head = NULL;
  69.     int i = 0;
  70.     for (i = 0; i < 3; i++) {
  71.         Push(&head, i);
  72.     }
  73.     return head;
  74. }
  75.  
  76. int main() {
  77.     struct node* head = build_node();
  78.     show_list(head);
  79.     printf("======================\n");
  80.  
  81.     int weight[3] = { 2, 1, 4 };
  82.     int i = 0;
  83.     for (i = 0; i < 3; i++) {
  84.         if (i == 0) {
  85.             clone_node(head, 0, weight[0]);
  86.         } else {
  87.             int j = 0;
  88.             int sum_weight = 0;
  89.             for (j = 0; j <= i - 1; j++) {
  90.                 sum_weight += weight[j];
  91.             }
  92.             clone_node(head, i + sum_weight, weight[i]);
  93.  
  94.         }
  95.     }
  96.     show_list(head);
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement