Member Functions in C - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Member Functions in C

Chainsaw666Chainsaw666 Posts: 198Member
Ok, I'm working with using member functions in structures in C, which I've never done before, and I'm stuck. Here's the code:
[code]
typedef struct Node
{
char data;
struct Node* next;
}node,*node_ptr;
typedef struct stack
{
node* head;
int length;
void (*push)(char,node_ptr);
char (*pop)(node_ptr);
}stack,*stack_ptr;
void push(char info,node_ptr head)
{
node_ptr current;
current=(node *)malloc(1);
current->data=info;
current->next=head;
head=current;
}
char pop(node_ptr head)
{
node_ptr current;
char input;
input=head->data;
head=head->next;
free(current);
return(input);
}
[/code]
Now my problem is that I don't want to have to reference a node as a variable to push and pop, I want to use the head of the stack as the only node they will modify. How would I go about doing this?

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2005-8-8 8:13:52[/red][/b][hr]
    C does not really have c++ style member functions -- they are just global functions that can be called at any time anywhere in the program.

    Here is one way to solve your proglem, which uses pointer-to-pointer. Also note the change to malloc() in push(), your program was not allocating enough memory for the node structure.

    [code]
    typedef struct Node
    {
    char data;
    struct Node* next;
    }node,*node_ptr;

    typedef struct stack
    {
    node* head;
    int length;
    void (*push)(char,node_ptr);
    char (*pop)(node_ptr);
    }stack,*stack_ptr;

    void push(char info,node_ptr* head)
    {
    node_ptr current;
    current=(node *)malloc(sizeof(struct Node));
    current->data=info;
    current->next = 0;
    if(*head == 0)
    {
    *head = current;
    }
    else
    {
    // find the tail
    node_ptr n = *head;
    while(n->next != 0)
    n = n->next;
    // add node to the end of the list
    n->next = current;
    }
    }

    char pop(node_ptr* head)
    // removes the head node of the linked list
    // in FIFO (first in, first out)
    {
    node_ptr current;
    char input;
    if(*head == 0) return 0;
    current = *head;
    *head = current->next;
    input = current->data;
    free(current);
    return(input);
    }

    int foo()
    {
    struct stack stk;
    stk.head = 0;
    stk.length = 0;

    push('A',&stk.head);
    pop(&stk.head);
    return 0;
    }

    [/code]







  • Chainsaw666Chainsaw666 Posts: 198Member
    Thanks for the help, you killed two birds with one stone.


Sign In or Register to comment.