Advertisement
Guest User

300 project

a guest
Jan 20th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C Compiler.
  4. Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10. struct Node
  11. {
  12. void* key;
  13. struct Node* next;
  14. struct Node* pre;
  15. };
  16.  
  17. struct List
  18. {
  19. struct Node* end;
  20. struct Node* current;
  21. struct Node* start;
  22. int start_index;
  23. int end_index;
  24. int current_index;
  25. };
  26. struct Node arr2[100];
  27. struct List arr1[5];
  28. int head_free_index=0;
  29. int node_free_index=0;;
  30. struct List* ListCreate()
  31. {
  32. struct List* ret;
  33. if(head_free_index+1>=5 || node_free_index+1>=100)
  34. return NULL;
  35. arr2[node_free_index].next=NULL;
  36. arr2[node_free_index].pre=NULL;
  37. arr2[node_free_index].key=NULL;
  38. arr1[head_free_index].start=&arr2[node_free_index];
  39. arr1[head_free_index].end=&arr2[node_free_index];
  40. arr1[head_free_index].current=&arr2[node_free_index];
  41. arr1[head_free_index].start_index=head_free_index;
  42. arr1[head_free_index].end_index=head_free_index;
  43. arr1[head_free_index].current_index=head_free_index;
  44. ret=&arr1[head_free_index];
  45. head_free_index++;
  46. node_free_index++;
  47. return ret;
  48. }
  49. int ListCount(struct List A)
  50. {
  51.  
  52. struct Node* temp=A.start;
  53. int count=1;
  54. while(temp->next!=NULL)
  55. {
  56. count++;
  57. temp=temp->next;
  58.  
  59. }
  60. return count;
  61. }
  62. void* ListFirst(struct List A)
  63. {
  64. A.current=A.start;
  65. return A.start;
  66. }
  67. void* ListLast(struct List A)
  68. {
  69. A.current=A.end;
  70. return A.end;
  71. }
  72. void* ListNext(struct List A)
  73. {
  74. if(A.current->next==NULL)
  75. return NULL;
  76. A.current=A.current->next;
  77. return A.current;
  78. }
  79. void* ListPrev(struct List A)
  80. {
  81. if(A.current->pre==NULL)
  82. return NULL;
  83. A.current=A.current->pre;
  84. return A.current;
  85. }
  86. void* ListCurr(struct List A)
  87. {
  88. return A.current;
  89. }
  90. int ListAdd(struct List A, void* item )
  91. {
  92. struct Node newNode;
  93. newNode.key=item;
  94. if(node_free_index>=100)
  95. return -1;
  96. if(A.current==NULL && A.current->next!=NULL)
  97. {
  98. newNode.next=A.start;
  99. A.start->pre=&newNode;
  100. newNode.pre=NULL;
  101. A.start=&newNode;
  102. A.current=&newNode;
  103.  
  104. }
  105. if(A.current==NULL && A.current->pre!=NULL)
  106. {
  107. newNode.pre=A.end;
  108. A.end->next=&newNode;
  109. newNode.next=NULL;
  110. A.end=&newNode;
  111. A.current=&newNode;
  112. }
  113. else
  114. {
  115. newNode.next=A.current->next;
  116. A.current->next=&newNode;
  117. newNode.pre=A.current;
  118. newNode.next->pre=&newNode;
  119. }
  120. for(int i=node_free_index;i>A.current_index;i--)
  121. {
  122. arr2[i].key=arr2[i-1].key;
  123. arr2[i].next=arr2[i-1].next;
  124. arr2[i].pre=arr2[i-1].pre;
  125. }
  126. for(int i=0;i<head_free_index;i++)
  127. {
  128. if(i!=0)
  129. arr1[i].start=arr1[i].start->next;
  130. arr1[i].end=arr1[i].end->next;
  131. arr1[i].current=arr1[i].current->next;
  132. }
  133.  
  134. }
  135. int main()
  136. {
  137. arr1[0]=*ListCreate();
  138. struct List C;
  139. C.start=arr1[0].start;
  140. C.end=arr1[0].end;
  141. C.current=arr1[0].current;
  142.  
  143. int a = ListCount(C);
  144. printf("%d", a);
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement