/*
Author - Chad Reynolds
*/
#include <stdio.h>
#include <stdlib.h>
#include "rndm.h"
#define MAX_NUMBERS 100
struct node
{
int data;
unsigned long link;
};
struct node *head = NULL;
struct node *tail = NULL;
//insert function will insert numbers into node in accending order
void insert(struct node **head, struct node **tail, int insertNum)
{
struct node *prev = NULL;
struct node *cur = *head;
struct node *node = malloc(sizeof(struct node));
node->data = insertNum;
while ((cur != NULL) && (cur->data < node->data))
{
struct node *next = (struct node *) (cur->link ^ (unsigned long)prev);
prev = cur;
cur = next;
}
if ( head == NULL )
{
*head = node;
(*head)->link = 0;
}
else if ( cur == NULL )
{
prev->link = prev->link ^ (unsigned long)node;
node->link = (unsigned long)prev ^ (unsigned long)cur;
*tail = node;
}
else if ( cur->data >= node->data )
{
//if prev is null, beginning of list, new head
if ( prev == NULL )
{
*head = node;
}
//else get the previous node to the prev item
else
{
struct node *prevlink = (struct node *)(prev->link ^ (unsigned long)cur);
prev->link = (unsigned long)prevlink ^ (unsigned long)node;
}
//link the list.
node->link = (unsigned long)prev ^ (unsigned long)cur;
struct node * next = (struct node *) ((unsigned long)prev ^ cur->link);
cur->link = (unsigned long)node ^ (unsigned long)next;
}
}
//delete will delete a number from the list if it is found
void delete(struct node **head, struct node **tail, int numDelete)
{
struct node *prev = NULL;
struct node *cur = *head;
while ( cur != NULL )
{
struct node *next = (struct node *) (cur->link ^ (unsigned long)prev);
//if the number is found, then delete it
if (cur->data == numDelete)
{
if(prev)
{
prev->link ^= (unsigned long)cur ^ (unsigned long)next;
}
else
*head = next;
if(next)
{
next->link ^= (unsigned long)cur ^ (unsigned long)prev;
}
else
*tail = prev;
free(cur);
break;
}
prev = cur;
cur = next;
}
}
//list, can list from head, or tail
void list(struct node *head, int a)
{
struct node *cur = head;
struct node *prev = NULL;
//if value passed is 1, we want to start at the tail, otherwise, start at he head.
if(a == 1)
{
cur = tail;
}
int count = 1;
while ( cur != NULL )
{
printf( "%4.1d ", cur->data );
struct node * next = (struct node *) (cur->link ^(unsigned long)prev);
prev = cur;
cur = next;
//if 10 items, print new line
if ( (count % 10) == 0 ) printf( "n" );
count++;
}
printf("n");
}
int main()
{
float seeds = 0;
int i = 0;
int start = 0;
int end = 0;
printf("Please enter the seed:");
scanf("%f",&seeds);
init_seed(seeds);
printf("Please enter the range:");
scanf("%d %d",&start,&end);
set_range(start,end);
for (i = 0; i < MAX_NUMBERS; ++i )
{
insert( (struct node**)head, (struct node**) tail, rndm() );
}
//list in accending
list((struct node*)head, 0);
//delete 7004 from list
delete((struct node**)head, (struct node**)tail, 7004);
//list in decending
list((struct node*)head, 1);
return 0;
}