472,789 Members | 1,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 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 4074
"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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.