D
duncanblacksmithmath
I know a lot of you have seen this before but I have worked on the
program and have gotten it to work thus far but I need help getting
these two functions to work and implementing them.
Here is what I have been trying to work out for these functions but
can't get them to fully work out for me.
information:
A function to access an element of a list by its index called access
i(LL, i). It traverses
the list LL and returns the address of the i-th element in LL, if there
is one. Otherwise it
returns null.
A function to access an element of a list by value called access v(LL,
V). This function
traverses the list LL searching for an element with the value V. The
address of the rst such
element, if found, is returned. Otherwise a null pointer is returned.
What is needed in the main:
Access the 10-th identifer and the name "XYZ" in the list NAMES. Print
the identifer hold
on the 10-th place in the list NAMES and also print the place (index
and/or address) of the
list element where your program found the name "XYZ". Provide for all
contingencies.
functions (outline):
struct Val *access_i(struct Obj *l, int i)
{
struct Obj *next=l;
int j=0;
while (j != i)
{
if (next -> n_link == NULL)
return (NULL);
else
{
next=next->n_link;
j=j+1;
}
}
if (next -> n_link == NULL)
return (NULL);
else
return (next);
}
struct Val *access_v(struct Obj *l, int v)
{
struct Obj *next=l->n_link;
int val=next->p_value;
while (val != v)
{
if (next -> n_link == NULL)
return (NULL);
else
{
next=next->n_link;
val=next->p_value;
}
}
if (next -> n_link == NULL)
return (NULL);
else
return (next);
}
errors:
program1.c: In function `access_i':
program1.c:72: warning: return from incompatible pointer type
program1.c: In function `access_v':
program1.c:78: warning: initialization makes integer from pointer
without a cast
program1.c:86: warning: assignment makes integer from pointer without a
cast
program1.c:92: warning: return from incompatible pointer type
complete code (compiles and executes correctly):
scan.h reads in a file and in the main the catch functions finds the
numbers, separators, unknowns, and names
/* Compiling command: cc scan.c lms1.c
where lms1.c is this file;
Calling sequence: "a.out Filename"
where Filename is the name of a text file */
#include <stdio.h>
#include <stdlib.h>
#include "scan.h"
struct Val
{
char type;
int length;
char value[256];
} Values[1000];
struct Obj
{
struct Obj *p_link;
struct Val *p_value;
struct Obj *n_link;
} Objects[1000], *IdList, *NrList, *SpList, *UnList ;
int i = 0, j = 0; /* 0 <= i,j <= 999, objects and vcalues indices */
struct Obj *List (struct Obj *h, struct Obj *t)
{
h->p_link = NULL;
h->n_link = t;
h->p_value = NULL;
t->p_link = h;
t->n_link = NULL;
t->p_value = NULL;
return h;
}
int Append(struct Obj *L, struct Val *item)
{
struct Obj *Temp = L->n_link;
while (Temp->n_link != NULL)
Temp = Temp->n_link;
if ((i <= 999))
{
(Temp->p_link)->n_link = &Objects;
Objects.n_link = Temp;
Objects.p_link = Temp->p_link;
Temp->p_link = &Objects;
Objects.p_value = &Values[j];
i = i+1;
return 1;
}
else return 0;
}
int PrintLists(struct Obj *list)
{
struct Obj *Temp = list->n_link;
printf("Type\tLength\tValue\n");
while (Temp->n_link != NULL)
{
printf("%c\t%d\t%s\n", Temp->p_value->type,
Temp->p_value->length, Temp->p_value->value);
Temp = Temp->n_link;
}
}
main (argc, argv)
int argc;
char *argv[];
{
extern TKN get_token(FILE *);
TKN Token;
FILE *Input;
int Done = 0;
IdList = List(&Objects[0], &Objects[1]);
NrList = List(&Objects[2], &Objects[3]);
SpList = List(&Objects[4], &Objects[5]);
UnList = List(&Objects[6], &Objects[7]);
i = 8; j = 0;
Input = fopen(argv[1], "r");
while (!Done)
{
Token = get_token( Input );
switch (Token.Code)
{
case 'I':
{
/* process identifier */
printf("Symbol: Identifier %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'I';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (IdList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'N':
{
/* process integer number */
printf("Symbol: Integer number %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'N';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (NrList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'F':
{
/* process real number */
printf("Symbol: Real number %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'F';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (NrList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'W':
{
printf("White symbol received\n");
break;
}
case 'U':
{
if (Token.String[0] == 'Z')
Done = 1;
else
printf("Unprintable character
discovered\n");
break;
}
case 'O':
{
printf("Symbol: Separator %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'S';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (SpList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'E':
{
printf("Error condition: %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'E';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (UnList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
}
} /* end while */
printf("List of NAMES\n");
PrintLists(IdList);
printf("List of NUMBERS\n");
PrintLists(NrList);
printf("List of SEPARATORS\n");
PrintLists(SpList);
printf("List of UNKNOWNS\n");
PrintLists(UnList);
}
Thanks for the help.
program and have gotten it to work thus far but I need help getting
these two functions to work and implementing them.
Here is what I have been trying to work out for these functions but
can't get them to fully work out for me.
information:
A function to access an element of a list by its index called access
i(LL, i). It traverses
the list LL and returns the address of the i-th element in LL, if there
is one. Otherwise it
returns null.
A function to access an element of a list by value called access v(LL,
V). This function
traverses the list LL searching for an element with the value V. The
address of the rst such
element, if found, is returned. Otherwise a null pointer is returned.
What is needed in the main:
Access the 10-th identifer and the name "XYZ" in the list NAMES. Print
the identifer hold
on the 10-th place in the list NAMES and also print the place (index
and/or address) of the
list element where your program found the name "XYZ". Provide for all
contingencies.
functions (outline):
struct Val *access_i(struct Obj *l, int i)
{
struct Obj *next=l;
int j=0;
while (j != i)
{
if (next -> n_link == NULL)
return (NULL);
else
{
next=next->n_link;
j=j+1;
}
}
if (next -> n_link == NULL)
return (NULL);
else
return (next);
}
struct Val *access_v(struct Obj *l, int v)
{
struct Obj *next=l->n_link;
int val=next->p_value;
while (val != v)
{
if (next -> n_link == NULL)
return (NULL);
else
{
next=next->n_link;
val=next->p_value;
}
}
if (next -> n_link == NULL)
return (NULL);
else
return (next);
}
errors:
program1.c: In function `access_i':
program1.c:72: warning: return from incompatible pointer type
program1.c: In function `access_v':
program1.c:78: warning: initialization makes integer from pointer
without a cast
program1.c:86: warning: assignment makes integer from pointer without a
cast
program1.c:92: warning: return from incompatible pointer type
complete code (compiles and executes correctly):
scan.h reads in a file and in the main the catch functions finds the
numbers, separators, unknowns, and names
/* Compiling command: cc scan.c lms1.c
where lms1.c is this file;
Calling sequence: "a.out Filename"
where Filename is the name of a text file */
#include <stdio.h>
#include <stdlib.h>
#include "scan.h"
struct Val
{
char type;
int length;
char value[256];
} Values[1000];
struct Obj
{
struct Obj *p_link;
struct Val *p_value;
struct Obj *n_link;
} Objects[1000], *IdList, *NrList, *SpList, *UnList ;
int i = 0, j = 0; /* 0 <= i,j <= 999, objects and vcalues indices */
struct Obj *List (struct Obj *h, struct Obj *t)
{
h->p_link = NULL;
h->n_link = t;
h->p_value = NULL;
t->p_link = h;
t->n_link = NULL;
t->p_value = NULL;
return h;
}
int Append(struct Obj *L, struct Val *item)
{
struct Obj *Temp = L->n_link;
while (Temp->n_link != NULL)
Temp = Temp->n_link;
if ((i <= 999))
{
(Temp->p_link)->n_link = &Objects;
Objects.n_link = Temp;
Objects.p_link = Temp->p_link;
Temp->p_link = &Objects;
Objects.p_value = &Values[j];
i = i+1;
return 1;
}
else return 0;
}
int PrintLists(struct Obj *list)
{
struct Obj *Temp = list->n_link;
printf("Type\tLength\tValue\n");
while (Temp->n_link != NULL)
{
printf("%c\t%d\t%s\n", Temp->p_value->type,
Temp->p_value->length, Temp->p_value->value);
Temp = Temp->n_link;
}
}
main (argc, argv)
int argc;
char *argv[];
{
extern TKN get_token(FILE *);
TKN Token;
FILE *Input;
int Done = 0;
IdList = List(&Objects[0], &Objects[1]);
NrList = List(&Objects[2], &Objects[3]);
SpList = List(&Objects[4], &Objects[5]);
UnList = List(&Objects[6], &Objects[7]);
i = 8; j = 0;
Input = fopen(argv[1], "r");
while (!Done)
{
Token = get_token( Input );
switch (Token.Code)
{
case 'I':
{
/* process identifier */
printf("Symbol: Identifier %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'I';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (IdList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'N':
{
/* process integer number */
printf("Symbol: Integer number %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'N';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (NrList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'F':
{
/* process real number */
printf("Symbol: Real number %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'F';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (NrList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'W':
{
printf("White symbol received\n");
break;
}
case 'U':
{
if (Token.String[0] == 'Z')
Done = 1;
else
printf("Unprintable character
discovered\n");
break;
}
case 'O':
{
printf("Symbol: Separator %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'S';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (SpList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
case 'E':
{
printf("Error condition: %s\n",
Token.String);
if (j < 999)
{
j = j+1;
Values[j].type = 'E';
Values[j].length = strlen(Token.String);
strcpy(Values[j].value, Token.String);
Append (UnList, &Values[j]);
}
else
printf("No plave available for this
value\n");
break;
}
}
} /* end while */
printf("List of NAMES\n");
PrintLists(IdList);
printf("List of NUMBERS\n");
PrintLists(NrList);
printf("List of SEPARATORS\n");
PrintLists(SpList);
printf("List of UNKNOWNS\n");
PrintLists(UnList);
}
Thanks for the help.