Reviewing my implementation of a stack using linked list I have been studying data structures, and I'm new to this.  Here is my code for the push and pop operations.


I considered using a node=top, so that I can avoid O(n) operations for the push (i.e. finding the last node every time).
I can't seem to find a better implementation for the pop operation. 


Let me know if my code is good or bad.  You can be harsh.  I'll take it positively and learn from it.

typedef struct stack_ {
    int data;
    struct stack_ *next;
}stack;

stack *push(stack *head, stack **top, int val){
    //if stack is empty
    if(head == NULL){
        head = (stack *)malloc(sizeof(stack *));
        head -&gt; data = val;
        head -&gt; next = NULL;
        *top = head; //make current node the top of stack
        return head;
    }

    stack *newnode;
    newnode = (stack *)malloc(sizeof(stack *));
    (*top)-&gt;next = newnode; //append new node to what top points to
    newnode -&gt; data = val;
    newnode -&gt; next = NULL;
    *top = newnode; //make current node the top of stack
    return head;
}

stack *pop(stack *head, stack **top){
    if(head == NULL){
        printf("Stack is empty\n");
        return NULL;
    }

    stack *cur, *prev;
    cur = head;
    while(cur -&gt; next){ //Traverse to the end
        prev = cur;
        cur = cur -&gt; next;
    }

    prev -&gt; next = NULL;
    *top = prev; //make this node the top of stack
    free(cur);
    return head;
}


Here's the main function:

int main ()
{
    stack *head, *top;
    head = top = NULL;

    while(1){
        int choice;
        printf("1.Push\n2.Pop\n3.Exit\n");
        scanf("%d", &amp;choice);

        if(choice == 3)
            break;
        else if(choice == 2){
            head = pop(head, &amp;top); 
            showstack(head); 

        }
        else if(choice == 1){
            int val;
            printf("Enter the value to be pushed\n");
            scanf("%d", &amp;val);
            head = push(head, &amp;top, val);
            showstack(head);
        }
        else
            printf("Enter the correct choice\n");

    }
    if(head)
        free(head);
    if(top)
        free(top);
    return 0;
}
Some minor comments to add to what others have said.


Adding an underscore to the end of struct stack is just noise. It achieves
nothing, so leave it out.
Omit the spaces around -&gt; and add them after keywords (if, while etc)
you are allocating only enough memory to hold a pointer to a stack node
(stack*). Instead you should be allocating a node:

head = malloc(sizeof(stack));


or:

head = malloc(sizeof *head);


Note also that there is no cast (we are writing C, not C++)




The version suggested by @ChrisWue is elegant, but it does need
two structures.  You could achieve the same thing but with only a
single struct (your existing stack) like this:

stack* push(stack *head, int data, int *result)
{
    stack *s = malloc(sizeof *s);
    if (s) {
        s-&gt;data = data;
        s-&gt;next = head;
        *result = 1;
    }
    else *result = 0;
    return s;
}

stack* pop(stack *head, int *data, int *result)
{
    if (!head) {
        *result = 0;
        return head;
    }
    *result = 1;
    stack *next = head-&gt;next;
    *data = head-&gt;data;
    free(head);
    return next;
}


But note that you must then be careful always to assign the return value to
head in the caller:

    int data;
    int ok;
    stack *head = NULL;
    ...
    head = push(head, data, &amp;ok);
    if (ok) {
        ...
    }
    head = pop(head, &amp;data, &amp;ok);
    if (ok) {
        ...
    }