Facebook
From Funky Pig, 9 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 602
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4.  
  5. struct tl{
  6. int key;
  7. tl *prev,*next;
  8. } *head=NULL,*x=NULL;    
  9.  
  10. struct tl *push(struct tl *head,struct tl*x){
  11. x->prev=NULL;
  12. x->next=head;
  13. if(head) head->prev=x;  
  14. return x;        
  15. }
  16.  
  17. // zwykle wypisanie listy
  18. void list(tl *head){
  19.      while(head){
  20. printf("%d ",head->key);
  21. head=head->next;
  22. }
  23. }
  24.  
  25. // rekurencyjne wypisanie listy
  26. void listr(tl *head){
  27. if(head) {
  28. printf("%d ",head->key);
  29. listr(head->next);
  30. }
  31. }
  32.  
  33. // wypisanie dlugosci listy
  34. int len(tl *head){
  35. int l=0;
  36. while(head){
  37. l++;
  38. head=head->next;
  39. }
  40. return l;
  41. }    
  42.  
  43. // rekurencyjne wypisanie dlugosci listy
  44. int lenr(tl *head){
  45. if(head==NULL) return 0;
  46. else return lenr(head->next)+1;
  47. }
  48.  
  49. // przeszukiwanie listy
  50. tl *search(tl *head,int k){
  51. while((head!=NULL)&&(head->key!=k))
  52. head=head->next;
  53. return head;
  54. }
  55.  
  56. // usuniecie elementu listy
  57. tl *eject(tl *head, tl *x){
  58. if(x->next)
  59. if(x->prev){
  60. x->prev->next=x->next;
  61. x->next->prev=x->prev;
  62. }
  63. else{
  64. x->next->prev=NULL;
  65. head=x->next;
  66. }
  67. else
  68. if(x->prev) x->prev->next=NULL;
  69. else head=NULL;
  70. free(x);
  71. return head;
  72. }
  73.  
  74. // wskazanie na ogon listy
  75. tl *tail(tl *head){
  76. while(head->next)
  77. head=head->next;
  78. return head;
  79. }
  80.  
  81. // usuniecie ogonu listy
  82. tl *erasetail(tl *head){
  83. tl *x=tail(head);
  84. if(x->prev==NULL) head=NULL;
  85. else x->prev->next=NULL;
  86. free(x);
  87. return head;
  88. }
  89.  
  90. int m=head->key;
  91. head=head->next;
  92. while(head){
  93. if(head->key->m)m=head->key
  94. head=head->next;
  95. }
  96. return m;
  97.  
  98.  
  99. // usuniecie listy
  100. void eraselist(tl *head){
  101. while(head){
  102. tl *x=head;
  103. head=head->next;
  104. free(x);
  105. }
  106. }
  107.  
  108. int main(void) {
  109.     int i=0,n=0;
  110.     printf("---LISTY---\n\nliczba elementow listy=");
  111.     scanf("%d",&n);
  112.     for(i=1;i<=n;i++){
  113.     x=(tl*)malloc(sizeof(tl));
  114.     x->prev=x->next=NULL;
  115.     x->key=i;
  116.     head=push(head,x);
  117. }
  118.  
  119. printf("\n---ZWYKLE WYPISANIE LISTY---\n");
  120. list(head);
  121.  
  122. printf("\n---REKURENCYJNE WYPISANIE LISTY---\n");
  123. listr(head);
  124.  
  125. printf("\n---PRZESZUKIWANIE LISTY---\nelement do wyszukania=");
  126. scanf("%d",&i);
  127. x=search(head,i);
  128. if (x)printf("jest=%d",x->key);
  129. else printf("nie ma");
  130.  
  131. printf("\n---WYPISANIE DLUGOSCI LISTY---\n");
  132. printf("length=%d",len(head));
  133.  
  134. printf("\n---REKURENCYJNE WYPISANIE DLUGOSCI LISTY---\n");
  135. printf("length=%d",lenr(head));
  136.  
  137. printf("\n---USUNIECIE ELEMENTU LISTY---\nelement do usuniecia=");
  138. scanf("%d",&i);
  139. x=search(head,i);
  140. if(x) head=eject(head,x);
  141. list(head);
  142.  
  143. printf("\n---WSKAZANIE NA OGON LISTY---\n");
  144. x=tail(head);
  145. printf("tail=%d",x->key);
  146.  
  147. printf("\n---USUNIECIE OGONU LISTY---\n");
  148. erasetail(head);
  149. list(head);
  150.  
  151. printf("\n---WYPISANIE WARTOSCI MAKSYMALNEJ LISTY---\n");
  152. max(head);
  153.  
  154. printf("\n---USUNIECIE LISTY--- uszkodzone\n");
  155. eraselist(head);
  156. list(head);
  157.  
  158. getche();
  159. return 0;
  160. }
  161.