john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

linkedlist lowmemmap

//John Pfeiffer
//Processor work versus Ram work
//if I calculate a lot, that's cpu time
//if I have large structures (arrays!) then I use alot of RAM

//I was imagining a list of nodes north with each being the head
//of a linked list east (like the binding of a book?)

//I believe that when you have a room in the upper right corner and
//are trying to "move" down the calculation of counting (traversing)
//up from root and then over all the way is more expensive than having in
//the struct the 4 way links to all your neighbors (N,E,S,W)

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>      //clrscr()!!!!
#define EMAX 0
#define NMAX 1              //NMAX must be at least 1 b/c it is the root

typedef struct east_node enode;
typedef struct head_node hnode;

typedef struct east_node* eptr;
typedef struct head_node* hptr;

//the simpler nodes that run out only east
struct east_node
{  eptr east;   };

//the heads of the linked lists that go east & north
struct head_node
{
    eptr east;
   hptr north;
};

//allocation functions
eptr ealloc(void)
{   return( (eptr)malloc( sizeof(struct east_node) )  );
}

hptr halloc(void)
{   return( (hptr)malloc( sizeof(struct head_node) )  );
}

//function that draws the outline of the "web"
//WOW who knew that for loops test at the END (post test!)
void make_map(hptr root)
{   hptr h=root;
   eptr e;
   int i,k;

    h = halloc();                   //start at 1 because root is always there!

   i=1;
    do{
        k=0;
        if( k >= EMAX)
      { h->east = NULL; }
      else{     h->east = ealloc();     }

      while( k < EMAX)
      {
            e = h->east;
         e->east = ealloc();
         k++;
      }//end of while(k < EMAX)

    if(i<NMAX)                  //testing if there will be more room
      { h->north = halloc();    }
      else      //meaning i is greater than or equal to NMAX, no more room
      { h->north = NULL;     }
      i++;
    }while(i <= NMAX);


}//end of make_map function

void display_map(hptr root)
{   hptr h;
   eptr e;
    int i=1;
    clrscr();
    printf("This is the map\n\n");
    do{
        printf("Head %d: ",i);
      if(h->east != NULL)
      {
        for( e=h->east; e!= NULL; e= e->east, i++)
          {
                printf(" %d ",i);
          }
      }
    printf("\n");
       i++;
   }while(h->north != NULL);
}//end of display_map function

//close the program and clean up the map
void free_map(hptr root)
{   hptr h, htemp;
    eptr e, etemp;
    for( h=root; h!= NULL; h= htemp)
   {
    for( e=h->east; e!= NULL; e= etemp) //skip to the east only nodes
    {  etemp = e->east;     //on the last node etemp will be set to NULL
            free(e);             //which is OK!
      }
      htemp = h->north;         //now the north link is saved OR set to NULL
        free(h);                        //and now the head is removed
   }
}//end of free_map function


//////////////////MAIN////////////////////////////
void main()
{
    hptr root;
    make_map(root);
    display_map(root);



//   free_map(root);
}//end of main

  • « linkedlist v1
  • stop kill processes by name ps pstree netstat process system usage top lsof background pause job »

Published

Dec 6, 2008

Category

c

~404 words

Tags

  • c 95
  • linkedlist 2
  • lowmemmap 1