Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // NewList: -> LIST
- // Cons: ELEM x LIST -> LIST
- // (Append: LIST x LIST -> LIST)
- // IsNull: LIST -> BOOL
- // Length: LIST -> NAT
- // Head: LIST -> ELEM
- // Tail: LIST -> LIST
- struct ElemStruct {
- char content;
- struct ElemStruct *next;
- };
- typedef struct ElemStruct *Elem;
- Elem NewElem(char c) {
- Elem e = malloc(sizeof(*Elem));
- e->content = c;
- e->next = NULL;
- return e;
- }
- typedef struct {
- Elem first;
- } *List;
- List NewList(void) {
- List l = malloc(sizeof(*List));
- l->first = NULL;
- return l;
- }
- List Cons(char content, List l) {
- Elem newElem = NewElem(content);
- if (l->first == NULL) {
- l->first = newElem;
- } else {
- Elem tmp = l->first;
- while (tmp->next != NULL) {
- tmp = tmp->next;
- }
- tmp->next = newElem;
- }
- return l;
- }
- void Print(List list) {
- printf("|list|->");
- Elem elem = list->first;
- while (elem != NULL) {
- printf("|%c|->", elem->content);
- elem = elem->next;
- }
- printf("NULL\n");
- }
- // Anzahl der Elemente in `l`
- int Length(List l) {
- }
- // Gib Inhalt (elem->content) des ersten
- // Elements zurück. Gib
- // einen Fehler mit printf aus, wenn die
- // Liste leer ist.
- char Head(List l) {
- }
- // Lösch das erste Element der List. Gib
- // einen Fehler aus (printf), wenn die
- // Liste leer ist.
- List Tail(List l) {
- }
- // Gib 1 zurück, wenn die Liste leer ist
- // und 0, wenn die Liste nicht leer ist.
- int IsNull(List l) {
- }
- int main(void){
- List l = NewList();
- Print(l);
- Cons('a', l);
- Print(l);
- Cons('b', l);
- Print(l);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement