Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct element
  5. {int DATA;
  6. struct element *nastepny;
  7. struct element *poprzedni;
  8. };
  9. typedef struct element Node;
  10.  
  11. void usun(Node** st,Node**kon,int n)
  12. {
  13.  
  14. if(*st==NULL||n<0)
  15. {
  16. printf("Pusta lista");
  17. return;
  18. }
  19. if(*st==*kon)
  20. {
  21. if(n==0)
  22. {
  23. *st=NULL;
  24. free(*kon);
  25. *kon=NULL;
  26. }
  27. return;
  28. }
  29. int i;
  30. Node* tmp= *st;
  31. for(i=0;i < n &&tmp!=NULL;i++)
  32. {
  33. tmp=tmp->nastepny;
  34. }
  35. if(tmp==NULL)
  36. return;
  37.  
  38. if(tmp==*st)
  39. {
  40. //Node *tmp=*st;
  41. *st=(*st)->nastepny;
  42. (*st)->poprzedni=NULL;
  43. //free(tmp);
  44. }
  45. else if(tmp==*kon)
  46. {
  47. //Node *tmp=*kon;
  48. *kon=(*kon)->poprzedni;
  49. (*kon)->nastepny=NULL;
  50. //free(tmp);
  51. }
  52. else
  53. {
  54. tmp->poprzedni->nastepny=tmp->nastepny;
  55. tmp->nastepny->poprzedni=tmp->poprzedni;
  56. //free(tmp);
  57. }
  58. free(tmp);
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. }
  66.  
  67. void dodaj(Node** head,Node **tail,int data)
  68. {
  69. Node* nowy= (Node*)malloc(sizeof(Node));
  70. nowy->nastepny=NULL;
  71. nowy->poprzedni=NULL;
  72. nowy->DATA=data;
  73.  
  74. if(*head==NULL)
  75. {
  76. *head=nowy;
  77. *tail=nowy;
  78. return;
  79.  
  80. }
  81.  
  82. nowy->nastepny=*head;
  83. (*head)->poprzedni=nowy;
  84. *head=(*head)->poprzedni;
  85.  
  86. }
  87.  
  88. void wypisz(Node *tmp)
  89.  
  90. {
  91. while(tmp!=NULL)
  92. {
  93. printf("%d " ,tmp->DATA);
  94. tmp=tmp->nastepny;
  95. }
  96. putchar('\n');
  97.  
  98.  
  99. }
  100. void kasuj(Node** head)
  101. {
  102. Node* tmp=*head;
  103. while(*head!=NULL)
  104. {
  105. tmp=*head;
  106. *head=(*head)->nastepny;
  107. free(tmp);
  108. }
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. int main()
  117. {
  118. Node *head=NULL;
  119. Node *tail=NULL;
  120. int i;
  121. for(i=0;i<10;i++)
  122. {
  123. dodaj(&head,&tail,2*i);
  124. }
  125.  
  126. wypisz(head);
  127. usun(&head,&tail,5);
  128. wypisz(head);
  129.  
  130.  
  131. kasuj(&head);
  132.  
  133.  
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement