prtptr

Untitled

Apr 24th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define flush getchar();
  5.  
  6. struct data{
  7. char nama[50];
  8. int umur;
  9. struct data *next, *prev;
  10. }*head, *tail, *curr;
  11.  
  12. void AddFront(char nama[], int umur){
  13. curr=(struct data*)malloc(sizeof(struct data));
  14. strcpy(curr->nama, nama);
  15. curr->umur=umur;
  16. curr->next=curr->prev=NULL;
  17.  
  18. if(head==NULL)
  19. head=tail=curr;
  20.  
  21. else{
  22. curr->next=head;
  23. head->prev=curr;
  24. head=curr;
  25. }
  26. }
  27.  
  28. void AddBack(char nama[], int umur){
  29. curr=(struct data*)malloc(sizeof(struct data));
  30. strcpy(curr->nama, nama);
  31. curr->umur=umur;
  32. curr->next=curr->prev=NULL;
  33.  
  34. if(head==NULL)
  35. head=tail=curr;
  36.  
  37. else{
  38. curr->prev=tail;
  39. tail->next=curr;
  40. tail=curr;
  41. }
  42. }
  43.  
  44. void AddMid(char nama[], int umur){ //Ascending
  45. data *temp;
  46. curr=(struct data*)malloc(sizeof(struct data));
  47. strcpy(curr->nama, nama);
  48. curr->umur=umur;
  49. curr->next=curr->prev=NULL;
  50.  
  51. if(head==NULL)
  52. head=tail=curr;
  53. else if(head->umur > curr->umur)
  54. AddFront(nama, umur);
  55. else if(tail->umur < curr->umur)
  56. AddBack(nama, umur);
  57. else{
  58. temp=head;
  59. while(temp->next->umur<curr->umur){
  60. temp=temp->next;
  61. }
  62.  
  63. if(temp->next->umur>curr->umur){
  64. curr->next=temp->next;
  65. temp->next=curr;
  66. }
  67. }
  68. }
  69.  
  70. void DelFront(){
  71. if(head!=NULL){
  72. if(head==tail){
  73. free(head);
  74. head=tail=NULL;
  75. }
  76. else{
  77. head=head->next;
  78. free(head->prev);
  79. head->prev=NULL;
  80. }
  81. }
  82. }
  83.  
  84. void DelAll(){
  85. while(head!=NULL){
  86. DelFront();
  87. }
  88. }
  89.  
  90. void DelBack(){
  91. if(head!=NULL){
  92. if(head==tail){
  93. free(head);
  94. head=tail=NULL;
  95. }
  96. else{
  97. tail=tail->prev;
  98. free(tail->next);
  99. tail->next=NULL;
  100. }
  101. }
  102. }
  103.  
  104. void ShowData(){
  105. int i=1;
  106. curr=head;
  107. while(curr!=NULL){
  108. printf("%d %s %d\n", i, curr->nama, curr->umur);
  109. i++;
  110. curr=curr->next;
  111. }
  112. }
  113.  
  114. void Search(){
  115. char key[50];
  116. gets(key);
  117. curr=head;
  118. while(curr!=NULL){
  119. if(strcmp(key, curr->nama)==0){
  120. printf("%d", curr->umur);
  121. curr=curr->next;
  122. }
  123. }
  124. }
  125.  
  126. int main(){
  127. char nama[50];
  128. int umur;
  129.  
  130. flush;
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment