Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

Stack Push

/* Function to push an item to stack*/
void push(struct sNode** top_ref, int new_data)
{
    /* allocate node */
    struct sNode* new_node = (struct sNode*)malloc(sizeof(struct sNode));
    if (new_node == NULL) {
        printf("Stack overflow 
");
        getchar();
        exit(0);
    }

    /* put in the data */
    new_node->data = new_data;

    /* link the old list off the new node */
    new_node->next = (*top_ref);

    /* move the head to point to the new node */
    (*top_ref) = new_node;
}


Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #Stack #Push
ADD COMMENT
Topic
Name
3+9 =