Im seriously not even sure what this proj is suppposed to do, but we're supposed to create an ordered double linked list data structure. Its gonna have insert, traverse, search and delete. The program is gonna use static file names. I have code for a single linked list, but I dont know how to do double.
Heres most of the code so far I believe, but I have to do it with files in the insert function
This is already a double linked list (your record struct has both a prev and a next pointer). You need a data member in the record.
You should make another struct that holds the head and tail pointers, not make them stack variables of main. Like this:
Code:
struct LList {
record *head;
record *tail;
};
Is the insert function supposed to read in the file or should it only add a node to the list?
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Originally posted by CornedBee This is already a double linked list (your record struct has both a prev and a next pointer). You need a data member in the record.
You should make another struct that holds the head and tail pointers, not make them stack variables of main. Like this:
Code:
struct LList {
record *head;
record *tail;
};
Is the insert function supposed to read in the file or should it only add a node to the list?
Yah its supposed to read it in I beelive and write it out to another file. Its basically like the last project, I posted about, ( structures ) look below on main page
I guess this linked list is just supposed to take in that info from one file again and write it out in order. Guess linked list is just another way of ordereing the info ?! Not sure.
How is the code I have so far a double linked list? All those functions so far are for only a single list.
A linked list is a group of memory block that have cross references.
In a single linked list each element only has a reference to the next element. This means you can only traverse the list in one direction.
In a double linked list each element contains references to both the next and the previous element. You can traverse this kind of list in both directions.
What you've written is a double linked list. You nodes have links to both the previous and the next elements. But your nodes contain no data...
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
but isnt my code supposed to also have the prev pointer in it? It only has next throughout it and it doesnt have the tail part of it anywhere. The only data code I need is to read the info from a file from my structures project, and write it out to another file. Which I beleive then would only go in the insert part.
LOL, Im gonna have to skip class today and work on it more thats for sure. Gonna have to turn it in late.
Last edited by chugger93; Oct 15th, 2002 at 08:00 AM.
Look, here are two double linked lists for you. One is a C++ class, the other is real plain C.
Here's the C one. You need to insert it directly into your code.
Code:
// remove the __fastcall and register keywords if you use this, they are optimizing features
typedef struct _llistNode
{
struct _llistNode *prev;
struct yourStruct data;
struct _llistNode *next;
} llist_node;
typedef struct _llistMain
{
llist_node *head;
llist_node *tail;
unsigned int size;
} llist;
// warning: this function is likely to create memory leaks if any of the content
// has dynamically allocated data
void __fastcall ClearLList(llist *ll)
{
register llist_node *q, *p = ll->head;
while(p != NULL)
{
q = p->next;
free(p);
p = q;
}
InitLList(ll);
}
void __fastcall InitLList(llist *ll)
{
ll->size = 0;
ll->head = ll->tail = NULL;
}
llist_node * InsertNode(llist *ll, llist_node *at, const struct yourStruct *pData)
{
llist_node *newnode = malloc(sizeof(llist_node));
if(newnode == NULL)
return NULL;
++(ll->size);
llist_node *after = at->next;
at->next = after->prev = newnode;
newnode->prev = at;
newnode->next = after;
newnode->data = *pData;
return newnode;
}
void DeleteNode(llist *ll, llist_node *at)
{
--(ll->size);
at->prev->next = at->next;
at->next->prev = at->prev;
free(at);
}
The C++ one is in the attached header file. It's not as simple, the teacher won't believe you did it, but you get the idea. It's a templated version supporting iterators.
All you need to do is reading the file (you already know how, look at the previous exercise) and search the list for the right inserting position.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Cornedbee, why cant we just take my code way up above and modify it? Instead of giving me all this other stuff which is kinda throwing me off. In my code above I must be on the right track.
and where in the insert funciton do i put the code to read and write out the info? does it matter? Should I put in the beginning of the insert funciton or end. Also aint I gonna need this again?
struct record
{
char name[50];
char ssn[12]
};
Last edited by chugger93; Oct 15th, 2002 at 10:58 AM.
Yes you need this struct again, as the type of the data member of the llist_node struct (instead of 'struct yourStruct').
The main problem of your code above is the lack of seperation and the resulting bad readability. You should seperate the logic of the linked list from the logic of the real problem. The InsertNode function is only there to insert a node at a given position. You need a ReadFile function that reads the file record by record, searches the linked list for the correct place to insert and inserts the record there using InsertNode. Then it reads the next record.
The universal way to traverse a list is this for-loop:
llist_node *p;
for(p=ll->head;p!=NULL;p=p->next) {
// do things here, p points to the current node
}
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Remove the __fastcall, it was something for me to try out.
You need to place InitLList before ClearLList.
And where has the type of the pointer come to?
You can use the read_in function with a little modification. It now needs to read into the linked list, not into an array. And it needs to take care of sorting, remove the sort function.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
What I mean is that read_in doesn't just stupidly inserts one entry after the other into the linked list but rather searches for the right place every time.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
main
{
llist data;
InitLList(&data);
// just take it from the last project
read filename1;
read filename2;
read_in(parameters);
// the list is sorted, read_in must guarantee that
write_out(parameters);
}
00 open file filename
10 if end of file then end function
20 read input from file
30 search for the correct insert position in list
40 insert the data into the list
Here's an adjusted version of InsertNode. The current version has a problem that is now solved.
Originally posted by CornedBee No, they rule and are very easy to use.
Thats because u've been programming for god knows how many years, me on the other hand, have been donig it for almost 2 months. Really harder on someone like me. Thankgod I only have one more project to do after this one. Im not even gonna totally complete this one, problay just turn in what I have done