One Directional Bi Linked List
Tests
home top contents previous up next

//====================================================================================
//
//  Test cases for procedure do_copy
//   Copyright (C) 2009 Konstantin Kirillov.
//
//====================================================================================


#include <stdio.h>
#include <stdlib.h>
#include "nbranch.h"


void print_nbranch(NBList* entry);
void print_rbranch(NBList* entry);


int main(int argc, char* argv[])
{
	char* test[]={"Good","Program", "desperately", "needs a", "good", "test.", ""};
    NBList* copy;
	NBList* entry;

	printf("\n\nSource list:\n");

    entry=create(test);
    print_nbranch(entry);
	print_rbranch(entry);

    printf("\n\nCopy of the list:\n");
	copy=do_copy(entry);
    print_nbranch(copy);
	print_rbranch(copy);

	getchar();
	return 0;
}




//=================================================================================
//
//  Procedure to print nbranch for testing purposes.
//
//---------------------------------------------------------------------------------
void print_nbranch(NBList* entry)
{
  NBList* current=entry; 
  printf("\nnbranch:   ");
  while( current!=NULL )
  {
     printf("%s ",(*current).data);
	 current=(*current).next;
  }
}
//=================================================================================



//=================================================================================
//
//  Procedure to print rbranch for testing purposes.
//
//---------------------------------------------------------------------------------
void print_rbranch(NBList* entry)
{
  NBList* current=entry; 
  printf("\nrbranch:   ");
  while( current!=NULL )
  {
	  NBList* random=(*current).random;
	  if(random!=NULL)printf("%s ",(*random).data);
	  current=(*current).next;
  }
}
//=================================================================================



//=================================================================================
//
//  Procedure "create" for testing purposes.
//  Create NBList with "random" links to each even node.
//
//---------------------------------------------------------------------------------
//Returns:      entry node
//Input:        array of strings. Empty string terminates the job.
//----------------------------------------------------------------------------------

NBList* create(char** data)
{
  //Empty has NULL entry:
  NBList* entry=NULL;
  NBList* current; 
  char** current_data=data;

  //-----------------------------------
  //Spawn nbranch and data.
  //- - - - - - - - - - - - - - - - - - 
  while( (*current_data)[0]>0 )
  {

     //Reserve memory for next node:
	 NBList* target=(NBList*)malloc(sizeof(NBList));
	 if(entry==NULL)
	 {
		 entry=target;
	 }else
	 {
         (*current).next=target;
	 }
	 current=target;

     (*current).data=*current_data;

	 //We don't know about continuation:
     (*current).next=NULL;   
     (*current).random=NULL;

	 current_data++;
  }
  //- - - - - - - - - - - - - - - - - - 
  //Spawn nbranch and data.
  //-----------------------------------


  //-----------------------------------
  //Spawn rbranches
  //- - - - - - - - - - - - - - - - - - 
  current=entry;
  current_data=data;

  { int count=0;
    while( (*current_data)[0]>0 )
	{
		NBList* previous;
		(*current).random=NULL;
		if( (count++)%2 == 1)(*previous).random=current;
		previous=current;
		current=(*current).next;
		current_data++;
	}
  } 
  //- - - - - - - - - - - - - - - - - - 
  //Spawn rbranches
  //-----------------------------------

  return entry;
}
//=================================================================================
      


Copyright (C) 2009 Konstantin Kirillov