Thread: Stack Linked List query

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    Stack Linked List query

    Would you say that my notes on the code are correct and if not will you change them so that they are, thank you.

    Code:
    void Push(int x)
    {
    	// Create a space in the heap to store data.
    	struct Node* temp = (struct Node*)malloc(sizeof(struct Node*)); 
    
    
    	// here we are saying that temp.data = x
    	temp->data = x;
    
    
    	// Here we are saying that the temp.link is pointing to top.link
    	temp->link = top;
    
    
    	// Here we are saying that top is pointing to temp.
    	top = temp;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
    Except you're allocating the wrong amount of space.

    Try writing malloc calls like this (without the cast).
    struct Node* temp = malloc(sizeof(*temp));

    Tell us what warnings you get from malloc, and we'll tell you the correct way to fix it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack using linked list problem
    By effa in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2009, 12:10 PM
  2. STACK/FIFO as a linked list
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-24-2009, 02:04 PM
  3. Stack in linked list
    By mag_chan in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 05:31 AM
  4. Linked list Stack question
    By lyrick in forum C++ Programming
    Replies: 4
    Last Post: 09-23-2005, 06:23 AM
  5. linked list stack question
    By Drew in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2003, 05:05 AM