-
Linked List Concept-ish
I am having trouble adding a second node to a doubly linked list. I define my list as follows:
Code:
typedef struct Node {
char command[256];
int length;
struct Node *prev;
struct Node *next;
} userInput;
typedef struct q {
userInput *head;
userInput *tail;
} inputQ;
inputQ input = {NULL, NULL};
userInput *newNode;
For the 1st node, I do this:
Code:
input.head = newNode;
input.tail = newNode;
newNode->prev = NULL;
newNode->next = NULL;
And, for the 2nd node, I do this:
Code:
newNode->prev = input.tail;
input.tail->next = newNode;
input.tail = newNode;
Now, when I iterate back through my list (I want to start at the tail and work my way towards the head), I can only ever get the 1st node to print, then the 2nd node is garbage, which means, to me, that I've linked something wrong. Can anyone please tell me where?
Thank you all so much!
-
I see no obvious problem here, maybe your iteration is done incorrectly?
-
I think he/she don't allocate memory every time for a new node and therefore the only valid node will allways overwriten.But to check this, we should see the complete source code.
-
I don't want to share all the source code, because this is for a project in my Operating Systems class.
The up-history I'm trying to implement is extra credit, but she has a clear policy of no source code online.
So, the logic is fine then?
I use a sys_alloc_mem function that our professor gave to us when giving us the kernel and other necessary components to boot our tiny OS which I call every time like this:
Code:
if((newNode = (userInput *)sys_alloc_mem(sizeof(userInput))) == NULL)
{
return NULL; //no memory
}
When I hit the up arrow key after typing in one command, I get that command back just fine. But, if I enter a new command, then try to hit the up arrow twice, it will print the first thing. Then, it will print a capital S and a white diamond with a black ? inside it which makes me think it is null.
Is the above not right for allocating memory?
Edit: It returns NULL, because the function it's in returns a userInput ptr.