473,378 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Double Linked List Code

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;

Jan 24 '06 #1
1 4118
"Little" <co************@yahoo.com> writes:
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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
32
by: Clunixchit | last post by:
How can i read lines of a file and place each line read in an array? for exemple; array=line1 array=line2 ...
6
by: deanfamily | last post by:
I am re-posting my second problem. I have a double-linked list. I need to know if it is possible to remove just one of an item, instead of all that match the given criteria with the remove()...
3
by: Little | last post by:
Could someone help me get started on this program or where to look to get information, I am not sure how to put things together. 1. Create 4 double linked lists as follows: (a) A double linked...
3
by: Little | last post by:
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...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
9
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.