ClavinJune

Double LinkList

Apr 23rd, 2017
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "malloc.h"
  3.  
  4. struct Node{
  5.     int value;
  6.     Node *next, *prev;
  7. }*head, *tail, *current;
  8.  
  9. Node* createNode(int value){
  10.     Node* temp  = (Node*)malloc(sizeof(Node));
  11.     temp->value = value;
  12.     temp->prev = temp->next  = NULL;
  13.  
  14.     return temp;
  15. }
  16.  
  17. int TOTAL_DATA = 0;
  18.  
  19. void pushHead(int value){
  20.     current = createNode(value);
  21.     if( !head ){
  22.         head = tail = current;
  23.     }else{
  24.         current->next = head;
  25.         head->prev = current;
  26.         head = current;
  27.     }
  28.     ++TOTAL_DATA;
  29. }
  30.  
  31. void pushTail(int value){
  32.     current = createNode(value);
  33.     if( !head ){
  34.         head = tail = current;
  35.     }else{
  36.         tail->next = current;
  37.         current->prev = tail;
  38.         tail = current;
  39.     }
  40.     ++TOTAL_DATA;
  41. }
  42.  
  43. void pushMidByIndex(int index, int value){
  44.     if( index < 0 || index > TOTAL_DATA ) return;
  45.     if( index == 0 ) pushHead(value);
  46.     else if( index == TOTAL_DATA ) pushTail(value);
  47.     else{
  48.         Node* find = head;
  49.         current = createNode(value);
  50.         for(int i=1;i<index;++i) find = find->next;
  51.  
  52.         current->next = find->next;
  53.         find->next->prev = current;
  54.         find->next = current;
  55.         current->prev = find;
  56.         ++TOTAL_DATA;
  57.     }
  58. }
  59.  
  60. void popHead(){
  61.     if( !head ) return;
  62.     if( head == tail ){
  63.         free(head);
  64.         head = tail = NULL;
  65.         --TOTAL_DATA;
  66.         return;
  67.     }
  68.     current = head;
  69.     head = head->next;
  70.     free(current);
  71.     head->prev = current = NULL;
  72.     --TOTAL_DATA;;
  73. }
  74.  
  75. void popTail(){
  76.     if( !head ) return;
  77.     if( head == tail ){
  78.         free(head);
  79.         head = tail = NULL;
  80.         --TOTAL_DATA;
  81.         return;
  82.     }
  83.     current = tail;
  84.     tail = tail->prev;
  85.     free(current);
  86.     tail->next = current = NULL;
  87.     --TOTAL_DATA;
  88. }
  89.  
  90. void popMidByIndex(int index){
  91.     if( index < 0 || index > TOTAL_DATA-1 ) return;
  92.     if( head == tail ){
  93.         free(head);
  94.         head = tail = NULL;
  95.         --TOTAL_DATA;
  96.         return;
  97.     }
  98.  
  99.     Node* find = head;
  100.     for(int i = 0 ; i < index; ++i) find = find->next;
  101.  
  102.     find->prev->next = find->next;
  103.     find->next->prev = find->prev;
  104.     free(find);
  105.     find = NULL;
  106.     --TOTAL_DATA;
  107. }
  108.  
  109. void popAll(){
  110.     while( head ) popHead();
  111. }
  112.  
  113. int viewAll(){
  114.     current = head;
  115.     while( current ){
  116.         printf("%d ", current->value);
  117.         current = current->next;
  118.     }
  119. }
  120.  
  121. int main(){
  122.     pushMidByIndex(0,1);
  123.     pushMidByIndex(0,2);
  124.     pushMidByIndex(0,3);
  125.     pushMidByIndex(0,4);
  126.     pushMidByIndex(0,5);
  127.     popMidByIndex(2);
  128.     pushMidByIndex(2,100);
  129.     viewAll();
  130.     getchar();
  131.     return 0;
  132. }
Add Comment
Please, Sign In to add comment