Advertisement
mbah_bejo

DLinkList Sort/Del

Mar 3rd, 2020
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. /* Struktur Node */
  6.  
  7. typedef struct dnode_t {
  8.     int data;
  9.     struct dnode_t      \
  10.         *next,
  11.         *prev;
  12. } DListNode;
  13.  
  14. /* Struktur ADT List */
  15.  
  16. typedef struct dlist_t {
  17.     DListNode           \
  18.         *_head,
  19.         *_tail;
  20.     unsigned _size;
  21. } List;
  22.  
  23. /* DAFTAR FUNGSI YANG TERSEDIA */
  24.  
  25. DListNode* __dlist_createNode(int value);
  26. void dlist_init(List *list);
  27. bool dlist_isEmpty(List *list);
  28. void dlist_pushFront(List *list, int value);
  29. void dlist_pushBack(List *list, int value);
  30. void dlist_insertAt(List *list, int value);
  31. void dlist_popFront(List *list);
  32. void dlist_popBack(List *list);
  33. void dlist_remove(List *list, int element);
  34. void dlist_removeAt(List *list, int index);
  35.  
  36. /* Function definition */
  37.  
  38. DListNode* __dlist_createNode(int value)
  39. {
  40.     DListNode *newNode = \
  41.         (DListNode*) malloc (sizeof(DListNode));
  42.    
  43.     if (!newNode) return NULL;
  44.     newNode->data = value;
  45.     newNode->next = NULL;
  46.     newNode->prev = NULL;
  47.  
  48.     return (DListNode*) newNode;
  49. }
  50.  
  51. void dlist_init(List *list)
  52. {
  53.     list->_head = list->_tail = NULL;
  54.     list->_size = (unsigned) 0;
  55. }
  56.  
  57. bool dlist_isEmpty(List *list) {
  58.     return (list->_head == NULL && \
  59.             list->_tail == NULL);
  60. }
  61.  
  62. void dlist_pushFront(List *list, int value)
  63. {
  64.     DListNode *newNode = __dlist_createNode(value);
  65.     if (newNode) {
  66.         list->_size++;
  67.         if (dlist_isEmpty(list)) {
  68.             list->_head = newNode;
  69.             list->_tail = newNode;
  70.             return;
  71.         }
  72.  
  73.         newNode->next = list->_head;
  74.         list->_head->prev = newNode;
  75.         list->_head = newNode;
  76.     }
  77. }
  78.  
  79. void dlist_pushBack(List *list, int value)
  80. {
  81.     DListNode *newNode = __dlist_createNode(value);
  82.     if (newNode) {
  83.         list->_size++;
  84.         if (dlist_isEmpty(list)) {
  85.             list->_head = newNode;
  86.             list->_tail = newNode;
  87.             return;
  88.         }
  89.  
  90.         list->_tail->next = newNode;
  91.         newNode->prev = list->_tail;
  92.         list->_tail = newNode;
  93.     }
  94. }
  95.  
  96. void dlist_insertAt(List *list,  int value)
  97. {
  98.     if(list->_tail==NULL)
  99.     {
  100.         dlist_init(list);
  101.         dlist_pushBack(list, value);
  102.         return;
  103.     }
  104.        if (value < list->_head->data ) {
  105.         dlist_pushFront(list, value);
  106.         return;
  107.     }
  108.     else if (value >= list->_tail->data|| list->_head==NULL) {
  109.         dlist_pushBack(list, value);
  110.         return;
  111.     }
  112.     DListNode *newNode = __dlist_createNode(value);
  113.     if (newNode) {
  114.         if (dlist_isEmpty(list)) {
  115.             list->_head = newNode;
  116.             list->_tail = newNode;
  117.             return;
  118.         }
  119.  
  120.         DListNode *temp = list->_head;
  121.         DListNode *temp1 = list->_head;
  122.         unsigned _i = 1;
  123.         while (temp->data < value && temp->next != NULL) {
  124.             _i++;
  125.             temp = temp->next;
  126.         }
  127.        // printf("%d%d",_i,list->_size);
  128.         if(_i == list->_size)
  129.         {
  130.             newNode->next == NULL;
  131.             newNode->prev= temp;
  132.             temp->next= newNode;
  133.            
  134.             list->_tail->next= newNode;
  135.             newNode->prev = list->_tail;
  136.             list->_tail=newNode;
  137.             //list->_head=newNode;
  138.             //printf("%d",temp->next->data);
  139.         } else if(_i==1){
  140.             dlist_pushFront(list, value);
  141.         return;
  142.     }
  143.         else{    
  144.         newNode->next = temp;
  145.         newNode->prev = temp->prev;
  146.         temp->prev->next=newNode;
  147.         list->_size++;
  148.     }
  149.     }
  150. }
  151.  
  152. void dlist_popFront(List *list)
  153. {
  154.     if (!dlist_isEmpty(list)) {
  155.         DListNode *temp = list->_head;
  156.         if (list->_head == list->_tail) {
  157.             list->_head = NULL;
  158.             list->_tail = NULL;
  159.             free(temp);
  160.         }
  161.         else {
  162.             list->_head = list->_head->next;
  163.             list->_head->prev = NULL;
  164.             free(temp);
  165.         }
  166.         list->_size--;
  167.     }
  168. }
  169.  
  170. void dlist_popBack(List *list)
  171. {
  172.     if (!dlist_isEmpty(list)) {
  173.         DListNode *temp;
  174.         if (list->_head == list->_tail) {
  175.             temp = list->_head;
  176.             list->_head = NULL;
  177.             list->_tail = NULL;
  178.             free(temp);
  179.         }
  180.         else {
  181.             temp = list->_tail;
  182.             list->_tail = list->_tail->prev;
  183.             list->_tail->next = NULL;
  184.             free(temp);
  185.         }
  186.         list->_size--;
  187.     }
  188. }
  189.  
  190. void printlist(List *list)
  191. {
  192.     DListNode *temp= list->_head;
  193.     while(temp)
  194.     {
  195.         printf("%d ",temp->data);
  196.         temp=temp->next;
  197.     }
  198.     printf("\n");
  199. }
  200.  
  201. void dlist_removeAt(List *list, int index)
  202. {
  203.     if(index== 0 )return;
  204.    
  205. //  if (dlist_isEmpty(list))
  206. //      dlist_init(list);
  207.    
  208.         /* Kasus apabila posisinya melebihi batas */
  209.         if (index >= list->_size-1 ) {
  210.             //printf("1");
  211.             dlist_popBack(list);
  212.             return;    
  213.         }
  214.         else if (index == 1 || index < 0) {
  215.             dlist_popFront(list);
  216.             //printf("2");
  217.             return;
  218.         }
  219.        
  220.         int _i = 0;
  221.         if(index < list->_size/2)
  222.         {
  223.         DListNode *tempH = list->_head;
  224.         while (tempH->next != NULL && _i < index) {
  225.             tempH = tempH->next;
  226.             _i++;
  227.         }
  228.         DListNode *nextTo = tempH->next->next;
  229.         free(tempH->next);
  230.         nextTo->prev=tempH;
  231.         tempH->next = nextTo;  
  232.         list->_size--; 
  233.         } else
  234.         {
  235.         _i=list->_size;
  236.         DListNode *tempT = list->_tail;
  237.         while (tempT->prev != NULL && _i >= index) {
  238.             tempT = tempT->prev;
  239.             _i--;
  240.         }
  241.         DListNode *nextTo = tempT->next->next;
  242.         free(tempT->next);
  243.         nextTo->prev=tempT;
  244.         tempT->next = nextTo;  
  245.         list->_size--; 
  246.    
  247.         }
  248.     }
  249.  
  250. void dlist_remove(List *list, int value)
  251. {
  252.     if (!dlist_isEmpty(list)) {
  253.         DListNode *temp, *prev;
  254.         temp = list->_head;
  255.  
  256.         if (temp->data == value) {
  257.             dlist_popFront(list);
  258.             return;
  259.         }
  260.         while (temp != NULL && temp->data != value) {
  261.             prev = temp;
  262.             temp = temp->next;
  263.         }
  264.  
  265.         if (temp == NULL) return;
  266.         prev->next = temp->next;
  267.         free(temp);
  268.         list->_size--;
  269.     }
  270. }
  271.  
  272. int main(int argc, char const *argv[])
  273. {  
  274.     char com[8];
  275.     int n=0;
  276.     // Buat objek List
  277.     List myList;
  278.  
  279.     // PENTING! Jangan lupa diinisialisasi
  280.     dlist_init(&myList);
  281.  
  282.     // Gunakan operasi linked list
  283.     // untuk mengisi data linked list
  284. //    dlist_pushBack(&myList, 10);
  285. //    dlist_pushBack(&myList, 22);
  286. //    dlist_pushBack(&myList, 41);
  287. //    dlist_pushBack(&myList, 59);
  288.    
  289.     printf("DATA DALAM LINKED LIST:\n");
  290.     printlist(&myList);
  291.  
  292.     printf("COMANND (ins / del / deLNode), 0 untuk berhenti:\n");
  293.     scanf("%s",com);
  294.     while(strcmp(com,"0"))
  295.     {
  296.        
  297.     if(!strcmp(com,"ins"))
  298.     {   printf("Input number: ");
  299.         scanf("%d",&n);
  300.         dlist_insertAt(&myList, n);
  301.         printf("DATA DALAM LINKED LIST:\n");
  302.         printlist(&myList);
  303.         //printf("%d*",myList._size);
  304.             printf("COMANND (ins / del / deNode), 0 untuk berhenti:\n");
  305.     scanf("%s",com);
  306.     }
  307.        
  308.     else if(!strcmp(com,"delNode"))
  309.     {
  310.         printf("Input loc. Number (start form 1): ");
  311.         scanf("%d",&n);
  312.         dlist_removeAt(&myList, n);
  313.        
  314.         printf("DATA DALAM LINKED LIST:\n");
  315.         printlist(&myList);
  316.         //("%d*",myList._size);
  317.         printf("COMANND (ins / del / deLNode), 0 untuk berhenti:\n");
  318.         scanf("%s",com);
  319.     }else if(!strcmp(com,"del"))
  320.     {
  321.         printf("Input Number: ");
  322.         scanf("%d",&n);
  323.         dlist_remove(&myList, n);
  324.        
  325.         printf("DATA DALAM LINKED LIST:\n");
  326.         printlist(&myList);
  327.         //("%d*",myList._size);
  328.         printf("COMANND (ins / del / deLNode), 0 untuk berhenti:\n");
  329.         scanf("%s",com);
  330.     }
  331.     else
  332.     {
  333.         printf("WRONG COMMAND\nCOMANND (ins / del / deLNode), 0 untuk berhenti:\n");
  334.     scanf("%s",com);
  335.     }
  336.  
  337. }
  338.     return 0;
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement