Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 03-02-09   #1 (permalink)
New to Overclock.net
 
Join Date: Mar 2009
Posts: 2

Rep: 0 cr80expert5 Unknown
Unique Rep: 0
Trader Rating: 0
Default C Programming XorList Assignment Help

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)
{

}
Also, the rndm.h is a file provided by my professor that includes the init_seed, set_range, and rndm() functions that are typed in my main. Here's the complete code that I have so far:
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;
}
and then here are my errors:
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
Like I said, I'm very new to C and don't recognize these errors in my code. However, I did research what they mean but cannot translate the meanings into a solution. Please help someone.

Last edited by cr80expert5 : 03-02-09 at 11:33 PM
cr80expert5 is offline   Reply With Quote
Old 03-02-09   #2 (permalink)
WaterCooler
 
wimcle's Avatar
 
intel nvidia

Join Date: Jan 2009
Location: Shawnee,KS
Posts: 304

Rep: 38 wimcle is acknowledged by some
Unique Rep: 35
Trader Rating: 1
Default

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() );
__________________
System: water damage
CPU
920 4.1 21x195
Motherboard
Gigabyte ex58-ds4
Memory
6+3G OCZ gold ddr3 1600 c7
Graphics Card
BFG gtx280
Hard Drive
wd6401aals x6
Power Supply
Corsair tx650w
Case
CM 590
CPU cooling
Dangerden MC-TDX
GPU cooling
XSPC razor
OS
XP
Monitor
Samsung 2343bwx
wimcle is offline   Reply With Quote
Old 03-02-09   #3 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

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
Overclock.net Mod of the Month
Spotswood is offline   Reply With Quote
Old 03-03-09   #4 (permalink)
New to Overclock.net
 
Join Date: Mar 2009
Posts: 2

Rep: 0 cr80expert5 Unknown
Unique Rep: 0
Trader Rating: 0
Default

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;
}
Here's the following error when i gcc my program:
Code:
[reynoldc@athena:145]> a.out
Please enter the seed:123456
Please enter the range:1 9999
Segmentation fault
cr80expert5 is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 03:21 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.11701 seconds with 8 queries