Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // NewList: -> LIST
  5. // Cons: ELEM x LIST -> LIST
  6. // (Append: LIST x LIST -> LIST)
  7. // IsNull: LIST -> BOOL
  8. // Length: LIST -> NAT
  9. // Head: LIST -> ELEM
  10. // Tail: LIST -> LIST
  11.  
  12. struct ElemStruct {
  13. char content;
  14. struct ElemStruct *next;
  15. };
  16. typedef struct ElemStruct *Elem;
  17.  
  18. Elem NewElem(char c) {
  19. Elem e = malloc(sizeof(*Elem));
  20. e->content = c;
  21. e->next = NULL;
  22. return e;
  23. }
  24.  
  25. typedef struct {
  26. Elem first;
  27. } *List;
  28.  
  29. List NewList(void) {
  30. List l = malloc(sizeof(*List));
  31. l->first = NULL;
  32. return l;
  33. }
  34.  
  35. List Cons(char content, List l) {
  36. Elem newElem = NewElem(content);
  37.  
  38. if (l->first == NULL) {
  39. l->first = newElem;
  40. } else {
  41. Elem tmp = l->first;
  42. while (tmp->next != NULL) {
  43. tmp = tmp->next;
  44. }
  45. tmp->next = newElem;
  46. }
  47.  
  48. return l;
  49. }
  50.  
  51. void Print(List list) {
  52. printf("|list|->");
  53. Elem elem = list->first;
  54. while (elem != NULL) {
  55. printf("|%c|->", elem->content);
  56. elem = elem->next;
  57. }
  58. printf("NULL\n");
  59. }
  60.  
  61. // Anzahl der Elemente in `l`
  62. int Length(List l) {
  63.  
  64. }
  65.  
  66. // Gib Inhalt (elem->content) des ersten
  67. // Elements zurück. Gib
  68. // einen Fehler mit printf aus, wenn die
  69. // Liste leer ist.
  70. char Head(List l) {
  71.  
  72. }
  73.  
  74. // Lösch das erste Element der List. Gib
  75. // einen Fehler aus (printf), wenn die
  76. // Liste leer ist.
  77. List Tail(List l) {
  78.  
  79. }
  80.  
  81. // Gib 1 zurück, wenn die Liste leer ist
  82. // und 0, wenn die Liste nicht leer ist.
  83. int IsNull(List l) {
  84.  
  85. }
  86.  
  87. int main(void){
  88. List l = NewList();
  89. Print(l);
  90. Cons('a', l);
  91. Print(l);
  92. Cons('b', l);
  93. Print(l);
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement