Cons Cell Data Structure in C

I am new to C, in the early stages of creating a small Schema interpreter. For this part of the project, I am trying to create a simple cons cell data structure. He should take a list, for example

(a b c)

and present it internally as follows:

[ ][ ] -> [ ][ ] -> [ ][/]
 |         |         |
 A         B         C 

To verify that it is working correctly, I have a print function for echo output. Here is the code that does not work:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lexer.h"
#include "parse.h"

char token[20]; 


struct conscell {
    char *data;
    struct conscell *first, *rest;
};

void S_Expression ()
{   
    /* function from lexer to receive input a split into tokens no greater than 20 */
    startTokens(20);

    /* gets the next token */
    strcpy(token, getToken());

    /* List is a typedef for the struct conscell */
    List tree = createList ();
    tree = nextNode (tree);
    printList(tree);

}


List createList ()
{
    List node = malloc(sizeof (List));

    if (node == NULL) {
        printf("Out of memory!\n");
        exit(1);
    }
    node->data = NULL;
    node->first = NULL;
    node->rest = NULL;

    return node;
}

/* Recursive function to build cons cell structure */
List nextNode (List node)
{
    node = createList ();

    if (token[0] == '(')
    {         
       strcpy(token, getToken());
       node->first = nextNode(node->first);
       node->rest = nextNode(node->rest);         
     }

    else
    {
       if (token[0] == ')')
       {
          node = NULL;
       }

       else
       {
           List temp = createList();
           temp->data = token;
           temp->first = NULL;
           temp->rest = NULL;

           node->first = temp;

           strcpy(token, getToken());
           node->rest = nextNode(node->rest);            
       }
   }
   return node;
}

/* Prints output. So far, just trying to print symbols */
void printList(List node)
{
    if (node != NULL)
    {
      if (node->data != NULL)
      {        
        printf("%s", node->data);

      }
    }
}

Nothing has been printed yet. I am pretty sure this is a pointer problem. If someone could point me (not a pun) in the right direction, that would be very appreciated.

thank

+3
source share
2 answers

-, , List typedef struct conscell*. , , .

cons , . :

typedef conscell 
{
    unsigned char *data;   //<== use unsigned char for a memory buffer
    struct conscell* next; //<== only a "next" pointer needed
} conscell;

, , char, unsigned char , , lambdas .., unsigned char* void* , .

, , , cons- cons-, , ,

if (token[0] == '(')
{         
   strcpy(token, getToken());
   node->first = nextNode(node->first);
   node->rest = nextNode(node->rest);         
 }

cons- "" ""... , . node "" ( cons-cell, , ), node node .

createList(), , (.. node = NULL, , ' , node). free() node, NULL.

, printList() , , ... node . , . :

void printList(List node)
{
    List current = node;

    while (current != NULL)  //<== guard for the end-of-list
    {
      if (node->data != NULL)
      {        
        printf("%s", node->data);
      }

      current = current->next; //cycle to the next node in the linked list
    }
}

, , 1) cons , , , node. cons'ed , node. 2) , Scheme cons, , " ", , (.. ), . , - (cons 'd (cons 'c (cons 'b (cons 'a '())))), (d c b a). , , , ( , RPN).

+2

\n printf, , stdout:

printf("%s\n", node->data);
0

All Articles