C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Member Functions in C Posted by Chainsaw666 on 7 Aug 2005 at 11:00 PM
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:
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);
}

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?
Report
Re: Member Functions in C Posted by stober on 8 Aug 2005 at 3:01 AM
This message was edited by stober at 2005-8-8 8:13:52

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.

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;
}









Report
Re: Member Functions in C Posted by Chainsaw666 on 8 Aug 2005 at 10:45 AM
Thanks for the help, you killed two birds with one stone.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.