error in program with linked list

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 852 Views - Last Post: 15 February 2010 - 11:54 PM Rate Topic: -----

#1 momososo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 10

error in program with linked list

Posted 14 February 2010 - 09:12 AM

hi
i have to do a programe to read many polonomials from file
and then seperate every one to power and coffecient and store them at array of linked list
i do it but it didnt work
i dont know how to fix the error
i need it tonight
can u help me please ?

the code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node *ptr;
typedef struct node2 *ptr2;

struct node
{

char part[20];
ptr next;
};
struct node2
{
char power[20];
char num[20];
ptr2 next;
};
typedef ptr list;
typedef ptr position;
typedef ptr2 list2;
typedef ptr2 position2;
struct node cut(char l[50]);
int main()
{
FILE *in=fopen("data.txt","r");
char l[50];

char *tokptr,*tokptr2;

while( fscanf(in,"%s",l))
{
tokptr=strtok(l,"+");



list parts=(list) malloc(sizeof( struct node));
position temp =(position) malloc(sizeof(struct node));
temp=parts;

while (tokptr != NULL)
{
position name =(position) malloc(sizeof(struct node));
strcpy(name->part,tokptr);
printf(" \n %s",name->part);
temp->next=name;
temp=name;
tokptr=strtok(NULL,"+");
}

position2 temp1=(position2)malloc(sizeof(struct node2));
position temp2=(position)malloc(sizeof(struct node));
list2 p=(list2)malloc(sizeof(struct node2));
temp1=p;
temp2=parts;
while(temp2->next != NULL)
{
tokptr2=strtok(temp2->next->part,"x^");
position2 fun=(position2) malloc(sizeof(struct node2));
if(tokptr2=NULL)
strcpy( fun->num,"1");
else
strcpy( fun->num,tokptr2);
printf(" \n %s " ,fun->num );
tokptr2=strtok(NULL,"^");
if(tokptr2=NULL)
strcpy( fun->power,"1");
else
strcpy( fun->power,tokptr2);
printf(" %s" , fun->power);
temp1->next=fun;
temp1=fun;
temp2=temp2->next;

}

}
fclose(0);
return 0;
}

Is This A Good Question/Topic? 0
  • +

Replies To: error in program with linked list

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in program with linked list

Posted 14 February 2010 - 09:20 AM

Can you attach the data.txt file please.
Was This Post Helpful? 0
  • +
  • -

#3 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: error in program with linked list

Posted 14 February 2010 - 09:30 AM

And :code:
Was This Post Helpful? 0
  • +
  • -

#4 momososo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 10

Re: error in program with linked list

Posted 14 February 2010 - 09:38 AM

sorry i caoudnt attach it .... i dont know why
but we can add any polonomials we need to it
it may have any numbers of polonomials

the name of file is " data.txt"

do it with those polonomials


65x^4+x^9-43
9x^5+6x^9-4x^3-6
X^6-13x^4+8x^19+6
6x^6+x^3-43
9x^2+6x^9+4x^3-6
X^6-13x^4+8x^19+6
65x^4+x^9+43
9x^5+6x^9-4x^8-6
X^6-13x^4+8x^19+6
5x^4+3x-43
9x^5+6x^9-4x-6

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node *ptr;
typedef struct node2 *ptr2;

struct node
{

    char part[20];
    ptr next;
};
struct node2
{
    char power[20];
    char num[20];
    ptr2 next;
};
typedef ptr list;
typedef ptr position;
typedef ptr2 list2;
typedef ptr2 position2;
struct node cut(char l[50]);
int main()
{
    FILE *in=fopen("data.txt","r");
    char  l[50];

    char *tokptr,*tokptr2;

   while( fscanf(in,"%s",l))
   {
    tokptr=strtok(l,"+");



    list parts=(list) malloc(sizeof( struct node));
    position temp =(position) malloc(sizeof(struct node));
    temp=parts;

    while (tokptr != NULL)
            {
               position name =(position) malloc(sizeof(struct node));
               strcpy(name->part,tokptr);
               printf(" \n %s",name->part);
               temp->next=name;
               temp=name;
               tokptr=strtok(NULL,"+");
            }

    position2 temp1=(position2)malloc(sizeof(struct node2));
    position temp2=(position)malloc(sizeof(struct node));
    list2 p=(list2)malloc(sizeof(struct node2));
    temp1=p;
    temp2=parts;
    while(temp2->next != NULL)
    {
            tokptr2=strtok(temp2->next->part,"x^");
            position2 fun=(position2) malloc(sizeof(struct node2));
          if(tokptr2=NULL)
                    strcpy(  fun->num,"1");
else
       strcpy(  fun->num,tokptr2);
            printf(" \n %s  " ,fun->num );
            tokptr2=strtok(NULL,"^");
          if(tokptr2=NULL)
                       strcpy(  fun->power,"1");
else
            strcpy(  fun->power,tokptr2);
            printf("    %s" , fun->power);
            temp1->next=fun;
            temp1=fun;
            temp2=temp2->next;

    }

   }
    fclose(0);
return 0;
} 

Was This Post Helpful? 0
  • +
  • -

#5 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in program with linked list

Posted 14 February 2010 - 09:58 AM

As far as I can see, your pointers are all messed up. Could you explain to me exactly what you are trying to do here. For example, I presume you are trying to break 65x^4+x^9-43 into 65x^4 and x^9-43 in a chained list, with the code

    list parts=(list) malloc(sizeof( struct node));
    position temp =(position) malloc(sizeof(struct node));
    temp=parts;

    while (tokptr != NULL)
            {
               position name =(position) malloc(sizeof(struct node));
               strcpy(name->part,tokptr);
               printf(" \n %s",name->part);
               temp->next=name;
               temp=name;
               tokptr=strtok(NULL,"+");
            }




Is that correct. If so, what you have will not work.

This post has been edited by Martyn.Rae: 14 February 2010 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

#6 momososo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 10

Re: error in program with linked list

Posted 14 February 2010 - 10:06 AM

View PostMartyn.Rae, on 14 February 2010 - 08:58 AM, said:

As far as I can see, your pointers are all messed up. Could you explain to me exactly what you are trying to do here. For example, I presume you are trying to break 65x^4+x^9-43 into 65x^4 and x^9-43 in a chained list, with the code

    list parts=(list) malloc(sizeof( struct node));
    position temp =(position) malloc(sizeof(struct node));
    temp=parts;

    while (tokptr != NULL)
            {
               position name =(position) malloc(sizeof(struct node));
               strcpy(name->part,tokptr);
               printf(" \n %s",name->part);
               temp->next=name;
               temp=name;
               tokptr=strtok(NULL,"+");
            }




Is that correct. If so, what you have will not work.


with this code i must break 65x^4+x^9-43 into 65x^4 and x^9 and -43 in a chained list but i couldnt do it will cause i dont know how to break using multiple delimiters ( - or + )

after that i must break every part into power and coffecient
ex :
65x^4 i must break it to 65 and 4
and store them at new linked list every node in it have 2 parts called power and cofficient ( i called it nym in my program)
but this didnt work will , i dont know how to fix it
Was This Post Helpful? 0
  • +
  • -

#7 momososo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 10

Re: error in program with linked list

Posted 14 February 2010 - 12:06 PM

please help me to fix it
Was This Post Helpful? 0
  • +
  • -

#8 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in program with linked list

Posted 14 February 2010 - 12:08 PM

Do you have to use strtok, or can you use another method?
Was This Post Helpful? 0
  • +
  • -

#9 momososo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 10

Re: error in program with linked list

Posted 14 February 2010 - 12:27 PM

we can use any method we need
Was This Post Helpful? 0
  • +
  • -

#10 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in program with linked list

Posted 14 February 2010 - 12:33 PM

ok - I have been studying your code a stage further, and to use strtok, was not a wise choice. Lets go back to basics. Lets take an example, 9x^5+6x^9-4x^3-6. We are looking for <integer> <x> <^> <integer> <+/-> etc etc. I presume you could have
9x^5+6x^9-4x^3-6-9x^5+6x^9-4x^3-6+9x^5+6x^9-4x^3-6 etc. Are you interested in the + or minus signs?
Was This Post Helpful? 0
  • +
  • -

#11 momososo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 10

Re: error in program with linked list

Posted 14 February 2010 - 12:51 PM

i didnt understand what u mean
Was This Post Helpful? 0
  • +
  • -

#12 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in program with linked list

Posted 14 February 2010 - 12:54 PM

ok

If we take 9x^2 - 16

9x^2 is taken as a coefficient of 9 and a power is 2.
-16 is taken as a coefficient of -16 and a power of 1.
x^6 is taken as a coefficient of 1 and a power of 6

is that what you are looking for?
Was This Post Helpful? 0
  • +
  • -

#13 momososo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 10

Re: error in program with linked list

Posted 14 February 2010 - 01:07 PM

exactly

i want to apply this for all polonomial in the file and store this in array of linked list
Was This Post Helpful? 0
  • +
  • -

#14 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: error in program with linked list

Posted 14 February 2010 - 01:22 PM

ok,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node *ptr;

struct node
{
	ptr next;
	char coefficient[20];
	char power[20];
};


int main()
{
	FILE *in=fopen("data.txt","r");
	char l[50];

	
	ptr head = NULL;
	ptr tail = NULL;
	
	while( 1 )
	{
		char *input_string = l;
		fscanf(in,"%s",l);
		if ( feof(in) ) break;
		while ( *input_string != 0 )
		{
			char power[50];
			char coefficient[50];
			char * pointer = coefficient;

			power[0] = 0;;
			coefficient[0] = 0;;
			if ( *input_string == '+' || *input_string == '-' )
				*pointer++ = *input_string++;
			else
				*pointer++ = '+';
        	while ( *input_string >= '0' &&  *input_string <= '9' ) *pointer++ = *input_string++;
			if ( pointer == coefficient+1 ) *pointer++ = '1';
			*pointer = 0;
			if ( *input_string == 'x' || *input_string == 'X' )
			{
				input_string++;
				if ( *input_string == '^' ) input_string++;
				pointer = power;
				if ( *input_string == '+' || *input_string == '-' )
					*pointer++ = *input_string++;
				else
					*pointer++ = '+';
	    	    while ( *input_string >= '0' &&  *input_string <= '9' ) *pointer++ = *input_string++;
				if ( pointer == power+1 ) *pointer++ = '1';
				*pointer = 0;
			}
			else
			{
				power[0] = '+';
				power[1] = '1';
				power[2] = 0;
			}

			ptr node = (ptr) malloc(sizeof(struct node));
			node->next = NULL;
			strcpy(node->coefficient, coefficient);
			strcpy(node->power, power);
			if ( head == NULL )
				head = node;
			else
				tail->next = node;
			tail = node;
		}
	}
	fclose(in);
	return 0;
} 



That produces a list containing all coefficients and power pairs in the file.
The head of the list is head.

EDIT: Ahhh .. just spotted you want this in an array of linked lists!
I have just provided a linked list.

This post has been edited by Martyn.Rae: 14 February 2010 - 01:24 PM

Was This Post Helpful? 0
  • +
  • -

#15 momososo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-February 10

Re: error in program with linked list

Posted 14 February 2010 - 01:55 PM

this code save all in the same linked list ..... right ?
i try to make an array of linked list but i coudnt !!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2