i have a delema with my xor linked list program....now i keep getting the bottom error i will also get segmentation errors throughout the insert function!!!
i have the list commented out at all times....the only thing it will do right now is input numbers!!
and will output the number being passed in...and then what the head is afterwards...
this is the source code revised again:
Code:
#include <stdio.h>
#include <stdlib.h>
#include "rndm.h"
#define MAX_NUMBERS 100
typedef struct _node{
int data;
unsigned long link;
} node;//end node
node *tmp, *head, *tail, *next, *prv, *cur;
void insert(insertNum){
printf("This is the random number passed in %d\n", insertNum);
node *nekw = (node *)malloc(sizeof(node));
nekw->data = insertNum;
printf("this is the number inputed %d\n", nekw->data );
cur = head;
prv = NULL;
while(cur != NULL && cur->data < insertNum){
next = (node *)(cur->link ^ (unsigned long)prv);
prv = cur;
cur = next;
}//end while loop
if(prv == NULL){
head = nekw;
}//end if statement.
else{
nekw->link ^= (unsigned long) prv ^(unsigned long) cur;
prv->link ^= (unsigned long) cur ^ (unsigned long) nekw;
cur->link ^= (unsigned long) prv ^ (unsigned long) nekw;
}//end else statement.
}//end insert.
void delete(int deleteNum){
printf("I am in the delete function!\n");
cur = head;
printf("This is in cur before i start %2.1d\n", cur->data);
prv = NULL;
while(cur != NULL && (cur->data != deleteNum)){
prv =(node *)(cur->link ^ (unsigned long)next);
prv = cur;
cur = next;
}//end while loop
if(cur != NULL && (cur->data == deleteNum)){
printf("this is what was found %2.1d\n", cur->data);
prv->link ^= (unsigned long) prv ^ (unsigned long) cur;
next->link ^= (unsigned long) cur^ (unsigned long) next;
free(cur);
}//end if statement.
}//end delete.
void list(int option){
int cnt = 1;
if(option == 1){
cur = head;
prv = NULL;
printf("This is the list in acending order: \n");
while(cnt <= MAX_NUMBERS){
printf("%3.1d", cur->data);
cnt++;
if(cnt % 10 == 0){
printf("\n");
}//end if statement
prv =(node *)(cur->link^(unsigned long)next);
prv = cur;
cur = next;
}//end while loop
}//end if
else if(option == 2){
cur = tail;
next = NULL;
printf("This is the list in decending order: \n");
if(cnt % 10 == 0){
printf("\n");
}//end if statement.
while (cnt <= MAX_NUMBERS){
printf("%3.1d", prv->data);
cnt++;
if(cnt % 10 == 0){
printf("\n");
}//end if statement.
prv = (node *)(cur->link^(unsigned long) prv);
next = cur;
cur = prv;
}//end while loop.
}//end else if
}//end list.
int main(){
head = NULL;
tail = head;
long inputZ;
int num, option,cnt, beginning, end;
int numDelete;
printf("Please enter the seed: ");
scanf("%l\n", &inputZ);
init_seed(inputZ);
printf("Please enter the range: ");
scanf("%d%d\n", &beginning, &end);
set_range(beginning, end);
for(cnt=0; cnt <= MAX_NUMBERS; cnt++){
insert(rndm());
}//end for loop.
#if 0
printf("For ascending press 1, or 2 for decending: ");
scanf("%d\n", &num);
list(num);
numDelete = 7004;
delete(numDelete);
printf("For ascending press 1, or 2 for decending: ");
scanf("%d\n", &num);
list(num);
#endif
return 0;
}//end main.
here is my output:
Code:
> gcc linkList.c rndm.o
>ls
a.out* lab1/ lab2/ linkList.c rndm.h rndm.o
> a.out
Please enter the seed: 12345
Please enter the range: 1 9999
This is the random number passed in 3995
this is the number inputed 3995
This is the random number passed in 10463
this is the number inputed 10463
Segmentation fault
>
i don't understand what is going wrong....the only time the input should really brake down is the first one inserted and if a number inputed into a list of nodes is less than the first node then i have a problem....otherwise i should just be inserting nodes that have different numbers that are less than the next one!! but it seems to break down before i even insert a number!!!