Thread: Stacks

  1. #1
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198

    Stacks

    Hello, I was having some problems with structs, stacks and memory management, and I was hoping someone could help me out.

    i am having problems with my make node

    my struct is as follows
    Code:
    typedef struct node{
    Stack entry;
    struct node *next;
    } Node;
    
    for the Make node
    Node *MakeNode(StackEntry item) 
    {
      Node *nptr;
      entry = nptr->item;
    }
    Thanks

  2. #2
    Microsoft. Who? MethodMan's Avatar
    Join Date
    Mar 2002
    Posts
    1,198
    Also i would like return null if it is out of memory, if not a ptr to enough memory to hold a node

    Thanks

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Node *nptr;
    >entry = nptr->item;
    You forgot to allocate memory for the node and you got the assignment backwards:
    Code:
    typedef struct node{
      Stack entry;
      struct node *next;
    } Node;
    
    Node *MakeNode(StackEntry *item) 
    {
      Node *nptr;
      if ( ( nptr = malloc ( sizeof *nptr ) ) != NULL ) {
        nptr = item;
        return nptr;
      }
      else
        return NULL;
    }
    [edit]
    I may have mistaken your intention, here is another option:
    Code:
    Node *MakeNode(StackEntry *item) 
    {
      if ( ( item = malloc ( sizeof *item ) ) != NULL )
        return item;
      else
        return NULL;
    }
    [/edit]
    -Prelude
    Last edited by Prelude; 03-19-2002 at 09:28 PM.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 07:22 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  3. Avioding Stacks
    By LoafOfBread34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 06:20 AM
  4. Dumping singly linked list into 2 stacks.
    By strotee76 in forum C++ Programming
    Replies: 5
    Last Post: 05-16-2004, 05:48 PM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 02:01 AM