Basic linked list help

This is a discussion on Basic linked list help within the C Programming forums, part of the General Programming Boards category; Code: typedef struct node{ int data; struct node *next; }Node, *NodePtr; typedef struct{ NodePtr stack; }Stack, *StackPtr; Stack st; Is ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    16

    Basic linked list help

    Code:
    typedef struct node{
       int data;
       struct node *next;
    }Node, *NodePtr;
    
    typedef struct{
       NodePtr stack;
    }Stack, *StackPtr;
    
    Stack st;
    Is the actual stack itself "st"? How would I change what the node pointer (next?) is pointing to?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    st is an instance of the Stack struct. However, all that struct contains is a pointer to struct node. There's no actual memory allocated for that one. To use that code you'd do something like this:
    Code:
    {
      Stack st;
    
      st.stack = malloc(sizeof(Node));
      st.stack->data = 86;
      st.stack->next = NULL;
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Here's a really simple implementation:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node
    {
     int data;
     struct node *next;
    } Node, *NodePtr;
    
    typedef struct
    {
      NodePtr stack;
    } Stack, *StackPtr;
    
    void stack_init(StackPtr st)
    {
      st->stack = NULL;
    }
    
    void push(StackPtr st, int val)
    {
      NodePtr new;
    
      if(!(new = malloc(sizeof(Node))))
      {
        puts("Memory allocation error!");
        return;
      }
    
      new->data = val;
      new->next = st->stack;
      st->stack = new;
    }
    
    int pop(StackPtr st)
    {
      NodePtr node = st->stack;
      int val;
    
      if(!node)
      {
        puts("Stack underflow!");
        return 0;
      }
    
      val = node->data;
      st->stack = node->next;
      free(node);
    
      return val;
    }
    
    int main(void)
    {
      Stack st;
      int vals[8] = { 86, 10, 37, 42, 7, 231, 53, 29 };
      int i;
    
      stack_init(&st);
    
      for(i = 0;i < 8;++i)
        push(&st, vals[i]);
    
      for(i = 0;i < 8;++i)
        printf("%d\n", pop(&st));
    
      return 0;
    }
    itsme@dreams:~/C$ ./stack
    29
    53
    231
    7
    42
    37
    10
    86
    itsme@dreams:~/C$
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21