Double Linked List Code

L

Little

Could someone help me figure out how to put my project together. I
can't get my mind wrapped around the creation of the 4 double Linked
Lists. Thank your for your insight.

1. Create 4 double linked lists as follows:


(a) A double linked list called NAMES which will contain all C like
identifiers of less than
256 characters long identified in the input file F. Each identifier
will be represented by
a triple (I, length, string) where I is used to identify the type of
the object as being an
identifier, length is an integer representing the length of the
identifier in characters, and
string is a string of characters representing the identifier itself.


(b) A double linked list called NUMBERS which will contain all C like
numbers of less than
256 characters long identified in the input file F. Each number will be

represented by a
triple (N, length, string) where N is used to identify the type of the
objects as being a
number, length is an integer representing the length of the number in
characters, and
string is the string of characters representing the number itself.


(c) A double linked list called SEPARATORS which will contain all C
language separators
and operators identified in the input file F. Each separator will be
represented by a triple
(X, length, string) where X is S for separators, O for operators, R for

symbols denoting
relations, L for new line and E for end of file. The length is an
integer representing the
length of the symbol and string is the string of characters
representing the symbol itself.


(d) A double linked list called UNKNOWN which will contain all symbols
you discovered in
the file F which could not be classi ed in the lists NAMES, NUMBERS,
SEPARATORS.
Each object in this list will be represented by a triple (Y, length,
string) where Y is used
to signify that an unknown symbol was discovered, length is an integer
representing the
length in characters of the unknown symbol, and string is the string of

characters that
make up the unknown symbol itself.


The input file F will be an arbitrary text file in your directory. The
actions of list creation and list printing will be programmed as
functions which will be called from a main program. Use in your program

the functions list(H, T), to create an empty list L whose header is H
and tail is T, and append(L, OBJ), to append the object OBJ to the list
L.

Here are the three files that i have created:
main.c

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

#define null 0
#define true 1
#define false 0

struct NAMES{
char Name[256];
struct NAMES *Next;
struct NAMES *Prev;
};

typedef struct NAMES NAMES;
typedef struct NAMES header;
typdef header *headpt;

header *list(h,t)
header *h, *t;
{
h->Prev=null;
h->Next=t;
h->Name=null;
t->Prev=h;
t->Next=null;
t->Name=null;
return(h);
}

append (l,obj)
header *l;
NAME *obj;
{
NAME *q=l->Next;
while(q->Next != null)
q=q->Next;
obj->Prev=q->Prev;
obj->Next=q;
q->Prev=obj;
q=obj->Prev;
q->Next=obj;
}

printlist(pt)
header *pt;
{
object *q;
int i;
printf("\n->")
q=pt->Next;
while(q->Next != null)
{
i=q->Prev;
printf("%d->",i);
q=q->Next;
}
printf("\n");
}

main (argc, argv)
int argc;
char *argv[];
{
extern TKN get_token(FILE *);
TKN Token;
FILE *Input;
int TokenNr = 1, LineNr = 1, Done = 0, k;
Input = fopen(argv[1], "r");
while (!Done)
{
Token = get_token( Input );
switch (Token.Code)
{
case 'I':
{
/* process identifier */
printf("(%d,%d) Symbol: Identifier %s\n",TokenNr,
LineNr,Token.String);
TokenNr = TokenNr+1;
break;
}
case 'N':
{
/* process integer number */
printf("(%d,%d) Symbol: Integer number
%s\n",TokenNr,LineNr,Token.String);
TokenNr = TokenNr+1;
break;
}
case 'F':
{
/* process real number */
printf("(%d,%d) Symbol: Real number %s\n",TokenNr,LineNr,
Token.String);
TokenNr = TokenNr+1;
break;
}
case 'W':
{
printf("White symbol received\n");
break;
}
case 'L':
{
printf("New line symbol received\n");
LineNr = LineNr+1;
TokenNr = 1;
}
case 'U':
{
if (Token.String[0] == 'Z')
Done = 1;
else
printf("Unprintable character
discovered\n");
break;
}
case 'O':
{
printf("(%d,%d) Symbol: Separator
%s\n",TokenNr,LineNr,Token.String);
TokenNr = TokenNr+1;
break;
}
case 'E':
{
printf("Error condition: %s\n",
Token.String);
break;
}
}
} /* end while */
}

scan.c

/* This scanner recognizes identifiers returning the token I */
/* integers returing the token N, reals returning the token F, */
/* white spaces (blancs and tabs mapped into just one white */
/* space) returning the token W, unprintable characters returning */
/* the token U, and other characters, returning the token O. */
/* In all casses the recognized lexeme is stored in the string */
/* variable Toke.String. */

#include "scan.h"

TKN get_token (FILE *Input )
{
typedef struct pt_entry
{
int Action,
Data;
} pt_node;

static pt_node PT[ 11 ][ 8 ] = {
{{ 'S', 9 }, { 'S', 8 }, { 'S', 2 }, { 'S', 1 },
{ 'S', 1 }, { 'S', 8 }, { 'S', 8 }, { 'S', 10 }},

{{ 'A', 'I' }, { 'A', 'I' }, { 'S', 1 }, { 'S', 1 },
{ 'S', 1 }, { 'A', 'I' }, { 'A', 'I' }, { 'A', 'I' }},

{{ 'A', 'N' }, { 'A', 'N' }, { 'S', 2 }, { 'S', 5 },
{ 'A', 'N' }, { 'S', 3 }, { 'A', 'N' }, { 'A', 'N' }},

{{ 'A', 'E' }, { 'A', 'E' }, { 'S', 4 }, { 'A', 'E' },
{ 'A', 'E' }, { 'A', 'E' }, { 'A', 'E' }, { 'A', 'E' }},

{{ 'A', 'F' }, { 'A', 'F' }, { 'S', 4 }, { 'S', 5 },
{ 'A', 'F' }, { 'A', 'F' }, { 'A', 'F' }, { 'A', 'F' }},

{{ 'A', 'E' }, { 'A', 'E' }, { 'S', 7 }, { 'A', 'E' },
{ 'A', 'E' }, { 'A', 'E' }, { 'S', 6 }, { 'A', 'E' }},

{{ 'A', 'E' }, { 'A', 'E' }, { 'S', 7 }, { 'A', 'E' },
{ 'A', 'E' }, { 'A', 'E' }, { 'A', 'E' }, { 'A', 'E' }},

{{ 'A', 'F' }, { 'A', 'F' }, { 'S', 7 }, { 'A', 'F' },
{ 'A', 'F' }, { 'A', 'F' }, { 'A', 'F' }, { 'A', 'F' }},

{{ 'A', 'O' }, { 'A', 'O' }, { 'A', 'O' }, { 'A', 'O' },
{ 'A', 'O' }, { 'A', 'O' }, { 'A', 'O' }, { 'A', 'O' }},

{{ 'A', 'U' }, { 'A', 'U' }, { 'A', 'U' }, { 'A', 'U' },
{ 'A', 'U' }, { 'A', 'U' }, { 'A', 'U' }, { 'A', 'U' }},

{{ 'A', 'W' }, { 'A', 'W' }, { 'A', 'W' }, { 'A', 'W' },
{ 'A', 'W' }, { 'A', 'W' }, { 'A', 'W' }, { 'S', 10 }} };

static int TOKENS[ ] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 6, 5, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1,
1, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1,
1, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 0 };

int C, R = 0, S = 0, ch;
TKN Tkn;
ch = getc(Input);
if (( ch != EOF ) && (ch != '\n'))
C = TOKENS[ ch ];
else if (ch == EOF)
{
Tkn.Code = 'U';
Tkn.String[ 0 ] = 'Z';
Tkn.String[ 1 ] = '\0';
return ( Tkn );
}
else
{
Tkn.Code = 'L';
Tkn.String[0] = '\n';
Tkn.String[1] = '\0';
return (Tkn);
}
while (1)
if ( PT[ R ][ C ].Action == 'S' )
{
Tkn.String[ S++ ] = ch;
ch = getc(Input);
if (ch == '\n')
{
Tkn.String[ S ] = '\0';
R = PT[ R ][ C ].Data;
C = TOKENS[ ch ];
Tkn.Code = PT[ R ][ C ].Data;
ungetc(ch, Input);
return (Tkn);
}
else
{
R = PT[ R ][ C ].Data;
C = TOKENS[ ch ];
}
}
else
{
Tkn.String[ S ] = '\0';
Tkn.Code = PT[ R ][ C ].Data;
ungetc(ch, Input);
return (Tkn);
}
}

scan.h

#include <stdio.h>
#define MAX_STRING 32
typedef struct t_node
{
char Code;
char String[ MAX_STRING ];
} TKN, *TKN_PTR;
 
K

Keith Thompson

Little said:
Could someone help me figure out how to put my project together. I
can't get my mind wrapped around the creation of the 4 double Linked
Lists. Thank your for your insight.

I'm going to comment on some specific errors in your program, not on
how to solve your problem. Others might jump in and help with the
problem itself, or I might do so later.

[...]
Here are the three files that i have created:
main.c

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

This is not a legal form for an include directive. You want
#include "scan.h"
Did your compiler accept this?
#define null 0

Don't do this; just use the predefined macro NULL.
#define true 1
#define false 0

You never even use these.
struct NAMES{
char Name[256];
struct NAMES *Next;
struct NAMES *Prev;
};

typedef struct NAMES NAMES;
typedef struct NAMES header;
typdef header *headpt;

This is a typo; it's spelled "typdef". I'm not just being picky.
This is obviously not the code you actually tried to compile; if it
were, you would have corrected this error.

If you're going to post C code, post the *exact* code that you
compiled. Don't try to recompile it; copy-and-paste, insert the file,
or whatever. We're not going to waste our time guessing which errors
are in the original source files, and which ones you introduced by
re-typing it.

The typedefs are unnecessary; you can just use "struct NAMES"
directly. Having two names for the same structure is confusing.
Having another name for a pointer to that structure is equally
confusing. By using typedefs, you're hiding the fact that your types
are structures or pointers, making your code more difficult to follow.
header *list(h,t)
header *h, *t;
{

This is an old-style function declaration. You're unlikely to run
into a compiler that doesn't support modern prototypes. With a
correct prototype, this would be:

header *list(header *h, header *t) { ... }
h->Prev=null;
h->Next=t;
h->Name=null;
t->Prev=h;
t->Next=null;
t->Name=null;
return(h);
}

append (l,obj)
header *l;
NAME *obj;
{

Another old-style declaration, and implicit int to boot. Since
append() doesn't return a value, it should be declared to return void:

void append(header *l, NAME *obj) { ... }

[snip]
main (argc, argv)
int argc;
char *argv[];
{

Another old-style declaration. Note that main() returns int, so this
should be:

int main(int argc, char *argv[])
{
...
return 0;
}

(Falling off the end of main() without returning a value is allowed in
C99, but it's still a bad idea.)

[snip]
scan.h

#include <stdio.h>
#define MAX_STRING 32
typedef struct t_node
{
char Code;
char String[ MAX_STRING ];
} TKN, *TKN_PTR;

You don't use anything from stdio.h in scan.h; why the include
directive?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top