Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct osoba
  5. {
  6. int wiek;
  7. struct osoba *next;
  8. };
  9.  
  10. struct osoba *head;
  11. struct osoba *last;
  12. struct osoba *pom;
  13.  
  14. void create();
  15. void insertFirst(x);
  16. void insertLast(x);
  17. void insertAfter(key,x);
  18. void display();
  19.  
  20. int main()
  21. {
  22. int choice,key,x;
  23.  
  24. while(choice!=0)
  25. {
  26.  
  27. printf("\n------------------------------------------\n");
  28. printf("1.Tworzenie listy:\n");
  29. printf("2.Dodawanie elementu na poczatek listy:\n");
  30. printf("3.Dodawanie elementu na koniec listy: \n");
  31. printf("4.Dodawanie elementu po zadanym listy:\n");
  32. printf("5.Wyswietlanie elementow listy:\n");
  33. printf("6.Usuwanie elementu wybranego listy:\n");
  34. printf("7.Wyjscie z programu: 0\n");
  35. printf("------------------------------------------\n");
  36. printf("Dokonaj wyboru: ");
  37. scanf("%d",&choice);
  38.  
  39. if(choice==0)
  40. {
  41. break;
  42.  
  43. }
  44. else if(choice==1)
  45. {
  46. create();
  47. system("cls");
  48. }
  49. else if(choice==2)
  50. {
  51. system("cls");
  52. printf("dodajesz na poczatek listy\n");
  53. printf("\n---------------------------\n");
  54. printf("Podaj wiek: \n");
  55. scanf(" %d",&x);
  56.  
  57. insertFirst(x);
  58. }
  59. else if(choice==3)
  60. {
  61. system("cls");
  62. printf("Dodajesz na koniec listy\n");
  63. printf("---------------------------\n");
  64. printf("Podaj wiek: \n");
  65. scanf(" %d",&x);
  66.  
  67. insertLast(x);
  68. }
  69. else if(choice==4)
  70. {
  71. system("cls");
  72. printf("Dodajesz element po zadanym\n");
  73. printf("---------------------------\n");
  74. printf("\nPo ktorym chcesz dodac:\n");
  75. scanf("%d",&key);
  76. printf("Podaj wiek: \n");
  77. scanf("%d",&x);
  78.  
  79. insertFirst(key,x);
  80. }
  81. else if(choice==5)
  82. {
  83. system("cls");
  84. printf("Wyswietlasz elementy listy\n");
  85. printf("---------------------------\n");
  86.  
  87. display();
  88. }
  89.  
  90. }
  91.  
  92. return 0;
  93. }
  94. void display()
  95. {
  96. int nr_elementu=0;
  97. struct osoba* pom2;
  98. pom=head;
  99. while(pom2->next!=NULL)
  100. {
  101. printf("el.listy.nr:%d posiada wartosc wieku: %d\n",++nr_elementu,pom->wiek);
  102. pom2=pom->next;
  103. pom=pom2;
  104. }
  105. printf("el.listy.nr:%d posiada wartosc wieku: %d\n",++nr_elementu,pom->wiek);
  106. }
  107.  
  108. void create()
  109. {
  110. head=NULL;
  111. last=NULL;
  112. }
  113.  
  114. void insertFirst(int x)
  115. {
  116. pom=(struct osoba*)malloc(sizeof(struct osoba));
  117. pom->wiek=x;
  118.  
  119.  
  120. if(head==NULL)
  121. {
  122. head=pom;
  123. last=pom;
  124. head->next=NULL;
  125. last->next=NULL;
  126. }
  127. else
  128. {
  129. pom->next=head;
  130. head=pom;
  131. }
  132. }
  133.  
  134. void insertLast(int x)
  135. {
  136. pom=(struct osoba*)malloc(sizeof(struct osoba));
  137. if(head==NULL)
  138. {
  139. pom->next=NULL;
  140. last=pom;
  141. head=pom;
  142. head->wiek=x;
  143. }
  144. else
  145. {
  146. last->next=pom;
  147. pom->next=NULL;
  148. last=pom;
  149. last->wiek=x;
  150. }
  151. }
  152.  
  153. void insertAfter(int key, int x)
  154. {
  155. struct osoba *wstaw = (struct osoba*)malloc(sizeof(struct osoba));
  156. struct osoba *pom2;
  157. pom=head;
  158. pom2=head;
  159.  
  160. if(head==NULL)
  161. {printf("Nie mozna wykonac operacji");
  162. exit(1);}
  163.  
  164. for(int i=0; i<key; i++)
  165. {
  166. pom2=pom->next;
  167. pom=pom2;
  168. }
  169. pom->next=wstaw;
  170. wstaw->next=pom;
  171. pom=wstaw;
  172. pom->wiek=x;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement