linked list problems, heres what i have 
Author Message
 linked list problems, heres what i have

Hi,
    I wrote this in effort to grasp linked lists, below is what i have. I
thought everything was doing ok but i added a function to output all the
nodes and i keep getting a memory read error, i do not understand why, any
heklp is greatly appreciated.

isaac

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

struct Node
{
 Node *PREV;
 Node *NEXT;
 int stuff;

Quote:
};

Node *IsaacList = (Node*) malloc(sizeof(Node));

void AddNode(int stuff)
{
 file://allocate the node
 Node *NewNode = (Node *) malloc(sizeof(Node));

 file://assign values MAKE THE {*filter*}ES NULL!!!!!
 NewNode->stuff = stuff;
 NewNode->NEXT  = NULL;
 NewNode->PREV  = NULL;

 file://create tempNode to cycle though list for adding NewNode to list
 Node *TempNodeAdd = IsaacList;
 printf("TempNodeAdd->NEXT = %d\n",TempNodeAdd->NEXT);
 /*add node to end of list, if TempList->NEXT doesnt equal NULL (nothing is
there) then assign
 the Node that TempList points to, to what TempList holds as a value.
 */
 while(TempNodeAdd->NEXT != NULL)

  file://if TempList->NEXT is qual to NULL assign TempList's NEXT value to
the newly created Node (NewNode)
  if(TempNodeAdd->NEXT == NULL)
  {
   TempNodeAdd->NEXT = NewNode;
   break;
  }

  TempNodeAdd = TempNodeAdd->NEXT;
 }
 free(TempNodeAdd);

Quote:
}

void DeleteNode(Node* DNode)
{
 file://assign Node before DNode to Node AFTER(Next to) DNode
 DNode->PREV->NEXT = DNode->NEXT;
 file://assign Node after DNode to the Node BEFORE DNode
 DNode->NEXT->PREV = DNode->PREV;

 free(DNode);

Quote:
}

void ListNodes(void)
{
 Node *TempNodeList = IsaacList;

 while(TempNodeList->NEXT != NULL)
 {
  printf("stuff = %d\n", TempNodeList->stuff);
  TempNodeList = TempNodeList->NEXT;
 }

Quote:
}

void main(void)
{
 file://assign main pointers values to null
 IsaacList->NEXT = NULL;
 IsaacList->PREV = NULL;

 int NodeMake = 0;
 printf("How Many Nodes do you wish to create?\n");
 scanf("%d", &NodeMake);

 while(NodeMake != 0)
 {
  AddNode(NodeMake);
  printf("Node %d Created\
n",NodeMake);
  --NodeMake;
 }

 ListNodes();

Quote:
}

--



Sun, 20 Apr 2003 08:23:47 GMT  
 linked list problems, heres what i have

Quote:
>Hi,
>    I wrote this in effort to grasp linked lists, below is what i have. I
>thought everything was doing ok but i added a function to output all the
>nodes and i keep getting a memory read error, i do not understand why, any
>heklp is greatly appreciated.

You had answers on alt.comp.lang.learn.c-c++ a couple of days ago and
not implemented any of the recommended changes.  If you are not going
to take the advice, why ask for it?

<<Remove the del for email>>
--



Tue, 22 Apr 2003 04:03:54 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. really having trouble with linked lists

2. link list of a linked list (chained links?)

3. Incompatible NULL Assignments || Linked List inside Linked List

4. Clearing Memory || Linked List INSIDE of a Linked List

5. Freeing a Linked List inside of a Linked List

6. Linked List of Linked Lists

7. Define a linked list of a linked list

8. Link List to Link List, HELP friends

9. Problems with linked list macros

10. problem with implementing a linked list

11. Link list problem!!

12. problem with link list

 

 
Powered by phpBB® Forum Software