Multiple Double Linked Lists

L

Little

Could someone tell me what I am doing wrong here about declaring
mutiple double linked lists. This is what the information is for the
project and the code wil be below that. Thank your soo much for your
assitance in helping me solve this problem.

Information:

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.

The start of the code:

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

struct Names
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;
};

typdef struct Names DLList;
typdef struct Names header;

struct Numbers
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;
};

typdef struct Numbers DLList;
typdef struct Numbers header;

struct Separators
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;
};

typdef struct Separators DLList;
typdef struct Separators header;

struct Uknown
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;
};

typdef struct Uknown DLList;
typdef struct Uknown header;

typdef struct header *headerpt;

header *list(header *h,header *t)
{
h->prev=null;
h->next=t;
h->DLList=null;
t->prev=h;
t->next=null;
t->DLList=null;
return(h);
}

void append(header *l, DLList *obj)
{
DLList *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;
}

int main (int argc, char *argv[])
{
extern TKN get_token(FILE *);
TKN Token;
FILE *Input;
int Done = 0, k;
Input = fopen(argv[1], "r");
while (!Done)
{
Token = get_token( Input );
switch (Token.Code)
{
case 'I':
{
/* process identifier */
break;
}
case 'N':
{
/* process integer number */
break;
}
case 'F':
{
/* process real number */
break;
}
case 'W':
{
break;
}
case 'L':
{
break;
}
case 'U':
{
if (Token.String[0] == 'Z')
Done = 1;
break;
}
case 'O':
{
break;
}
case 'E':
{
break;
}
}
} /* end while */
return 0;
}
Again thank you.
 
V

Vladimir S. Oka

Little said:
Could someone tell me what I am doing wrong here about declaring
mutiple double linked lists. This is what the information is for the
project and the code wil be below that. Thank your soo much for your
assitance in helping me solve this problem.

I have a sneaking suspicion that I've seen this assignment before.
Still, bear with me...
Information:

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.

The start of the code:

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

What's in scan.h?
struct Names
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;

Using TABs instead of spaces for indentation is usually discouraged. At
least my newsreader (KNode) doesn't seem to like it, but there are eve
n better reasons...
};

typdef struct Names DLList;
typdef struct Names header;

Syntax error here: it's not `typdef` but `typedef`.
Could this be your problem?
struct Numbers
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;
};

typdef struct Numbers DLList;
typdef struct Numbers header;

struct Separators
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;
};

typdef struct Separators DLList;
typdef struct Separators header;

struct Uknown
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;
};

typdef struct Uknown DLList;
typdef struct Uknown header;

typdef struct header *headerpt;

header *list(header *h,header *t)
{
h->prev=null;
h->next=t;
h->DLList=null;
t->prev=h;
t->next=null;
t->DLList=null;
return(h);
}

void append(header *l, DLList *obj)
{
DLList *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;
}

int main (int argc, char *argv[])
{
extern TKN get_token(FILE *);
TKN Token;

What is TKN and get_token()? I presume they are defined in scan.h but
since you don't list it we can't know.
FILE *Input;
int Done = 0, k;

I'd prefer if these were on separate lines...
Input = fopen(argv[1], "r");

You should test to see if fopen() succeeded.
while (!Done)
{
Token = get_token( Input );
switch (Token.Code)
{
case 'I':
{
/* process identifier */
break;
}
case 'N':
{
/* process integer number */
break;
}
case 'F':
{
/* process real number */
break;
}
case 'W':
{
break;
}
case 'L':
{
break;
}
case 'U':
{
if (Token.String[0] == 'Z')
Done = 1;
break;
}
case 'O':
{
break;
}
case 'E':
{
break;
}

Personally, I'd include a default: case, even if it can "never happen".
E.g.:

default:
fprintf(stderr,"This can NEVER EVER happen!\n");
exit(SOME_ERROR_CODE);
break;
}
} /* end while */
return 0;
}

Also, your code doesn't seem to be doing much at all, and it's hardly
relevant to the question about /declaring/ the lists.
Again thank you.

You're welcome. Others will surely correct any mistakes I made.

Cheers

Vladimir
 
K

Keith Thompson

Little said:
Could someone tell me what I am doing wrong here about declaring
mutiple double linked lists. This is what the information is for the
project and the code wil be below that. Thank your soo much for your
assitance in helping me solve this problem.

What problem? You show us a problem description and a bunch of code.
What exactly are you asking?
Information:
[problem description snipped]

Let me guess, this is homework, right? (Asking for help on homework
is ok if you've made some effort yourself, but it's polite to mention
it.)
The start of the code:

This is very similar to something you posted two days ago. I posted a
rather lengthy followup; did you read it?
#include <stdio.h>
#include <stdlib.h>
#include "scan.h"

Last time, you posted the contents of "scan.h". This time, you
didn't. (I think there was a "scan.c" file as well.) Without knowing
what's in that header file, we can't guess what your problem might be.
struct Names
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;
};

typdef struct Names DLList;
typdef struct Names header;

You have two different typedefs for the same struct type; this is
bound to cause confusion. As I mentioned previously you don't need
any typedefs at all. Just refer to "struct Names" directly. The
typedefs are legal, and some people do like them. It's mostly a style
issue. But you shouldn't have multiple names for the same thing
unless you have a specific reason to do so.
struct Numbers
{
char* TYPE;
int length;
char data[256];
struct Names* next;
struct Names* prev;
};

typdef struct Numbers DLList;
typdef struct Numbers header;

Again, you have two typedefs for the same struct type -- *and* one
of them has the same name as the previous one, which is illegal.

Furthermore, you've misspelled "typedef", which is a syntax error.

You obviously haven't compiled this code. If you had, you would
already have corrected some of the more obvious errors. If you can't
be bothered to take the time to do this before dumping your incomplete
code here, there's not much we can do to help you. Conceivably we
could *guess* what you're trying to do, but that would be a waste of
our time. Post some real code that we can try compiling ourselves
*and* tell us what problems you're having, and maybe we can help. If
you want to show us compiler messages or program output,
copy-and-paste that as well; don't try to summarize.

(Usually when we see code with typos, it's because the poster tried to
summarize or re-type the code rather than copy-and-paste it. This is
long enough that I doubt that's what happened.)

[big snip]
 
J

James Dow Allen

Little said:
Could someone tell me [about] double linked lists.

This has been discussed here before. Assuming the elements are
acessed only by the list, the trick is to make a reversible function
of forward and back pointers (or indices). Traversal is then
accomplished with code like:
t = Elem[this].leftright ^ prev, prev = this, this = t;
(Obpuzzle: how do you do this, compliantly, without the temporary
variable?)
[Storage efficiency doesn't matter.]

Oh.... I must have misunderstood the question.

James
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top