|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
C Programming XorList Assignment Help
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
New to Overclock.net
|
The language C is a completely new language to me and I'm having a few difficulties with a solution to solve the errors that my program is producing. So here it goes...my professor is having my class design an xor linked list and the following are required so please do not offer to change the following code even if it's to my benefit
:Code:
struct node
{
int data;
unsigned long link;
};
void insert(struct node **head, struct node **tail, int insertNum)
{
}
void delete(struct node **head, struct node **tail, int numDelete)
{
}
void list(struct node *head, int a)
{
}
Code:
/*
Author - Chad Reynolds
Xor Linked Lists Program
*/
#include <stdio.h>
#include <stdlib.h>
#include "rndm.h"
#define MAX_NUMBERS 100
//data structure
struct node
{
int data;
unsigned long link;
};
//head and tail nodes
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,
node *cur = head,
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, o09beginning 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(0);
//delete 7004 from list
delete(7004);
//list in decending
list(1);
return 0;
}
Code:
[reynoldc@athena:92]> gcc xorlist.c rndm.o xorlist.c: In function `insert': xorlist.c:39: error: request for member `link' in something not a structure or union xorlist.c:46: warning: assignment from incompatible pointer type xorlist.c:54: warning: assignment from incompatible pointer type xorlist.c: In function `delete': xorlist.c:73: warning: initialization from incompatible pointer type xorlist.c:85: warning: assignment from incompatible pointer type xorlist.c:91: warning: assignment from incompatible pointer type xorlist.c: In function `main': xorlist.c:144: error: syntax error before ',' token xorlist.c:144: error: too few arguments to function `insert' xorlist.c:148: warning: passing arg 1 of `list' from incompatible pointer type xorlist.c:153: warning: passing arg 1 of `list' from incompatible pointer type Last edited by cr80expert5 : 03-02-09 at 11:33 PM |
|
|
|
|
|
#2 (permalink) | ||||||||||||
|
WaterCooler
![]() |
the error is a missing comma in this line:
__________________insert((struct node**)head, (struct node**)tail rndm() ); should be: insert((struct node**)head, (struct node**)tail, rndm() );
|
||||||||||||
|
|
|
|
|
#3 (permalink) |
|
Case Modder
![]() |
Its been 20 years since I used straight C, but Visual C++ complained a lot about defining variables inline (rather than at the beginning of the function). Any who, this pretty much compiled, even though I don't have one of the header files. I hope this helps.
![]() Code:
/*
Author - Chad Reynolds
Xor Linked Lists Program
*/
#include <stdio.h>
#include <stdlib.h>
//#include "rndm.h"
#define MAX_NUMBERS 100
//data structure
struct node
{
int data;
unsigned long link;
};
//head and tail nodes
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));
struct node * next;
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, o09beginning 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;
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;
struct node * next;
int count = 1;
//if value passed is 1, we want to start at the tail, otherwise, start at he head.
if(a == 1)
{
cur = tail;
}
while ( cur != NULL )
{
printf( "%4.1d ", cur->data );
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(0);
//delete 7004 from list
delete(7004);
//list in decending
list(1);
return 0;
}
__________________
Rich Custom Wooden Case Builder
|
|
|
|
|
|
#4 (permalink) |
|
New to Overclock.net
|
Thanks so much for the help so far but now I'm getting only one error, a segmentation fault after I run my program.
Now here's my current code with no errors: Code:
/*
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;
}
Code:
[reynoldc@athena:145]> a.out Please enter the seed:123456 Please enter the range:1 9999 Segmentation fault |
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|