1

So I have a linked list stack as an opaque object in C. am passing in a head pointer pointer to the function.

this is the code for the delete head function. am calling it pop

MY_STACK pop(MY_STACK* head) {  
    Node_ptr hHead = (Node_ptr)head;
    if (*head == NULL){

        printf("badness \n");
        return FAILURE;
    }


    hHead = hHead->next;

    return SUCCESS;
}

this doesn't work. it doesn't delete the head node. I actually can. but the head node doesn't switch to next when I do that and the program crashes because no head lol. how do I switch the head to next. because its not working. this is a node pointer pointer. its a public version of the Node called MY_STACK. I don't know how familiar you are with opaque object but for this am required to do it like this. I cant just put eveythign together I know how to do head delete with that but here its not working

this is MY_STACK header file. Node_ptr is the private version which holds the same things + data and next fields. I have to cast MY_sTACK to Node_ptr to access those things.

    #ifndef MY_STACK_H
#define MY_STACK_H

#include "my_status.h"

enum boolean {FALSE, TRUE};
typedef enum boolean Bool;

struct my_stack_public;
typedef struct my_stack_public* MY_STACK;

struct my_stack_public
{
    void (*destroy)(MY_STACK* phMy_stack);
    Status (*push)(MY_STACK* hMy_stack, char item);
    Status (*pop)(MY_STACK* hMy_stack);
    char (*top)(MY_STACK hMy_stack);
    Bool (*empty)(MY_STACK hMy_stack);
};

MY_STACK my_stack_init_default(void);

#endif

I have an insert function which works. it changes the head. but for some reason the pop function doesn't do it

jobobdsfos
  • 19
  • 1
  • 4
  • am casting head to Node_ptr because stack is a public version of Node struct that doesn't have access to things like the data. – jobobdsfos Jul 04 '15 at 07:06
  • Please show how MY_STACK is defined. – Vlad from Moscow Jul 04 '15 at 07:19
  • 1
    This is a prime example of why to NOT typedef a struct. In this case, 'MY_STACK' is actually a pointer, however a reader/maintainer of the code must dig through the code to find that detail. Code should be written so it is very clear, not obscured. Also, in general, all capitals+underscores for names is normally used for #define and const names. Suggest, in future, use camel case for typedef names – user3629249 Jul 05 '15 at 13:06

2 Answers2

2

The function has return type MY_STACK. I suspect that it is a typedef for a pointer to node. So the function has to return a pointer to node.

It can look the following way

MY_STACK pop( MY_STACK *head )
{
    MY_STACK node = *head;

    if ( *head != NULL ) *head = ( *head )->next;

    return node;
}

If the function has to delete the node that is the current head and return whether success or failure of the operation then the function can look like

int pop( MY_STACK *head )
{
    if ( *head != NULL ) 
    {
        MY_STACK node = *head;
        *head = ( *head )->next;
        free( node );
        return SUCCESS;
    }
    else
    {
        return FAILURE;
    }
}

where SUCCESS and FAILURE are some integer constants.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • yeah. this crashes. head is not switching – jobobdsfos Jul 04 '15 at 16:49
  • @jobobdsfos What does crash? – Vlad from Moscow Jul 04 '15 at 16:53
  • I try to see if the list is empty by seeing if head is NULL after adding one element and deleting it. and if I use free(). it crashes. if I don't. head is the old head. it didn't change. – jobobdsfos Jul 04 '15 at 17:06
  • @jobobdsfos Do you mean the second function realization in my post? – Vlad from Moscow Jul 04 '15 at 17:10
  • @jobobdsfos The second function? – Vlad from Moscow Jul 04 '15 at 17:13
  • @jobobdsfos If you mean the second function then it follows that when you add the first element element to the stack its data member next was not set to NULL. So it seems the function that pushes elements in the stack is wrong. – Vlad from Moscow Jul 04 '15 at 17:15
  • how? the head deletion thing is not working at all to begin with. am sure my insertion function works just fine. – jobobdsfos Jul 04 '15 at 17:18
  • @jobobdsfos I am sure you are wrong. You may ask the question in English in my personal forum at http://cpp.forum24.ru in the first section - a section for beginners. – Vlad from Moscow Jul 04 '15 at 17:23
  • the insert function is as simple as make a new node. allocate space. put in the item. put next as head. and put the new node at head. how am I wrong? – jobobdsfos Jul 04 '15 at 17:30
  • @jobobdsfos Show how MY_STACK is defined – Vlad from Moscow Jul 04 '15 at 17:36
  • @jobobdsfos I do not see data member next in the structure definition. – Vlad from Moscow Jul 04 '15 at 17:43
  • well that's how am required to do it. next is hidden in the private version. you get access to it when you cast MY_STACK to Node_ptr. Node_ptr is the same structure but its private. has same function declarations + character data and Node_ptr next; – jobobdsfos Jul 04 '15 at 17:45
  • @jobobdsfos I need to see how you call the function and how all other things are defined. As I said it is better to discuss these questions in my personal form. The final result can be placed here in the answer. – Vlad from Moscow Jul 04 '15 at 17:49
  • @jobobdsfos The third line counted from the top is Log in. You need to enter some nickname and password. Maybe it will ask whether you are male or famale. If I remember all questions can be ignored. – Vlad from Moscow Jul 04 '15 at 18:16
-1

Suggest use pointer MY_STACK as return type, try this:

MY_STACK pop(MY_STACK* head) {
    MY_STACK tmpHead = *head;
    if ((*head) == NULL) {
        printf("badness\n");
        return NULL;
    } else {
        *head = (*head)->next;
        tmpHead -> next = NULL;
        return tmpHead;
    }
}
coderz
  • 4,847
  • 11
  • 47
  • 70
  • 'head' is defined as pointer to pointer, Which is good for the ability to change what the variablie 'head' points to. However, head is NOT a pointer to an instance of the MY_STACK struct. so this line: 'head=head->next' will not work as expected. – user3629249 Jul 05 '15 at 13:12
  • @user3629249 I've made a mistake that thought `MY_STACK` is not a pointer. Answer updated. – coderz Jul 05 '15 at 13:45