Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #ifndef NULL
  5. #define NULL ((void*)0)
  6. #endif
  7.  
  8. typedef struct Elem_s {
  9. char Titel[256];
  10. char Interpret[256];
  11. struct Elem_s* next;
  12. } Elem_t;
  13.  
  14. void readfromKeyboard(Elem_t *item) {
  15. if(item == NULL) {
  16. perror("Item not allocated!");
  17. exit(-1);
  18. }
  19. printf("Neuer Eintrag:\nTitel\n");
  20. scanf("%s",item->Titel);
  21. printf("Interpret\n");
  22. scanf("%s",item->Interpret);
  23. item->next=NULL;
  24. }
  25.  
  26. Elem_t *allocateElement() {
  27. Elem_t* item;
  28.  
  29. item = (Elem_t*)malloc(sizeof(Elem_t));
  30. readfromKeyboard(item);
  31. return item;
  32. }
  33.  
  34. Elem_t* insertLast(Elem_t* list) {
  35. Elem_t* last;
  36. Elem_t* newitem = allocateElement();
  37. if(list == NULL) {
  38. return newitem;
  39. }
  40.  
  41. last = list;
  42.  
  43. while(last->next != NULL) {
  44. last=last->next;
  45. }
  46.  
  47. last->next = newitem;
  48. return list;
  49. }
  50.  
  51. void freeList(Elem_t** list) {
  52. Elem_t* last;
  53. Elem_t* temp;
  54.  
  55. if(list == NULL) {
  56. return;
  57. }
  58.  
  59. last= (*list);
  60. while(last != NULL) {
  61. temp= last->next;
  62. free((void*)last);
  63. last=temp;
  64. }
  65. (*list) = NULL;
  66. }
  67.  
  68. void print_singleElement(Elem_t *element) {
  69. if(element == NULL) { return; }
  70. printf("%s\t%s\n",element->Interpret,element->Titel);
  71. }
  72.  
  73. void print_entireList(Elem_t *list) {
  74. unsigned long i=0;
  75. while(list != NULL) {
  76. printf("%lu.",i++);
  77. print_singleElement(list);
  78. list=list->next;
  79. }
  80. }
  81.  
  82.  
  83. int main() {
  84. Elem_t* list = insertLast(NULL);
  85. list=insertLast(list);
  86. list=insertLast(list);
  87. print_entireList(list);
  88. freeList(&list);
  89.  
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement