SHARE
TWEET

Untitled

a guest Sep 16th, 2019 137 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. typedef struct {
  2.     int val;
  3.     int len;
  4.     struct MyLinkedList *next;
  5. } MyLinkedList;
  6.  
  7. /** Initialize your data structure here. */
  8.  
  9. MyLinkedList* myLinkedListCreate() {
  10.     MyLinkedList *head = (MyLinkedList*) malloc(sizeof(MyLinkedList));
  11.     head->next = NULL;
  12.     head->len = 0;
  13.     head->val = -1;
  14.     return head;
  15. }
  16.  
  17.  
  18. void display(MyLinkedList *obj) {
  19.     MyLinkedList *tmp;
  20.     tmp = obj;
  21.     /*while(tmp) {
  22.         printf("%d ", tmp->val);
  23.         tmp = tmp->next;
  24.     }*/
  25.     for(int i=0; i<obj->len; i++) {
  26.         printf("%d ", tmp->val);
  27.         tmp = tmp->next;
  28.     }
  29.     printf("\n");
  30. }
  31.  
  32. /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
  33. int myLinkedListGet(MyLinkedList* obj, int index) {
  34.     if(index >= obj->len)
  35.         return -1;
  36.     int i=0;
  37.     MyLinkedList* tmp = obj;
  38.     while(i!=index) {
  39.         tmp = tmp->next;
  40.         i++;
  41.     }
  42.     return tmp->val;
  43. }
  44.  
  45. /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
  46. void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
  47.     MyLinkedList *head = (MyLinkedList*) malloc(sizeof(MyLinkedList));
  48.     head = obj;
  49.     head->val = val;
  50.     obj->len++;
  51.     //obj = obj->next;
  52.     head->next = obj;
  53.  
  54. }
  55.  
  56. /** Append a node of value val to the last element of the linked list. */
  57. void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
  58.     if(obj->len == 0) {
  59.         myLinkedListAddAtHead(obj, val);
  60.         return;
  61.     }
  62.     MyLinkedList *new = (MyLinkedList*) malloc(sizeof(MyLinkedList));
  63.     new->val = val;
  64.     new->next = NULL;
  65.     MyLinkedList *tmp = obj;
  66.  
  67.     while(tmp->next) {
  68.         //tmp = tmp->next;
  69.         //printf("%d ", tmp->val);
  70.         tmp = tmp->next;
  71.     }
  72.     tmp->next = new;
  73.     obj->len++;
  74. }
  75.  
  76. /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
  77. void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
  78.     if(index > obj->len || index<0) {
  79.         return;
  80.     }
  81.     else if(index == 0) {
  82.         myLinkedListAddAtHead(obj, val);
  83.         return;
  84.     }
  85.     MyLinkedList *new = (MyLinkedList*) malloc(sizeof(MyLinkedList));
  86.     new->val = val;
  87.     MyLinkedList *tmp = obj;
  88.     int i=0;
  89.     while(i != index-1) {
  90.         i++;
  91.         tmp = tmp->next;
  92.     }
  93.     new->next = tmp->next;
  94.     tmp->next = new;
  95.     obj->len++;
  96.  
  97.     //display(obj);
  98. }
  99.  
  100. /** Delete the index-th node in the linked list, if the index is valid. */
  101. void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
  102.     MyLinkedList *p1 = obj;
  103.     MyLinkedList *p2 = p1->next;
  104.  
  105.     if(index >= obj->len || index<0 || obj->len==0) {
  106.         return;
  107.     }
  108.     else if(index == 0) {
  109.         obj->val = p2->val;
  110.         obj->next = p2->next;
  111.         free(p1);
  112.     }
  113.     else {
  114.         int i=0;
  115.         while(i != index-1) {
  116.             p1 = p1->next;
  117.             p2++;
  118.         }
  119.         p1->val = p2->val;
  120.         p1->next = p2->next;
  121.         free(p2);
  122.     }
  123.     obj->len--;
  124.     //display(obj);
  125. }
  126.  
  127. void myLinkedListFree(MyLinkedList* obj) {
  128.     MyLinkedList *curr = obj;
  129.     while(obj) {
  130.         obj = obj->next;
  131.         free(curr);
  132.         curr = obj;
  133.     }
  134. }
  135.  
  136.  
  137. int main()
  138. {
  139.     MyLinkedList *obj = myLinkedListCreate();
  140.     myLinkedListAddAtHead(obj,0);
  141.     display(obj);
  142.     myLinkedListAddAtIndex(obj,1, 9);
  143.     display(obj);
  144.  
  145.     myLinkedListAddAtIndex(obj,1,5);
  146.     display(obj);
  147.     myLinkedListAddAtTail(obj,7);
  148.     display(obj);
  149.     /*myLinkedListAddAtHead(obj,1);
  150.     display(obj);
  151.     myLinkedListAddAtIndex(obj,5,8);
  152.     display(obj);
  153.     /*myLinkedListAddAtIndex(obj,5,2);
  154.     display(obj);
  155.     myLinkedListAddAtIndex(obj,3,0);
  156.     display(obj);
  157.     myLinkedListAddAtTail(obj,1);
  158.     display(obj);
  159.     myLinkedListAddAtTail(obj,0);
  160.     display(obj);*/
  161.  
  162.     return 0;
  163. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top