954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Stack implementation using function pointers in C

Hi,
I did the stack implementation using arrays and i would like to do it with function pointers but i have no idea. If anyone could help by illustration that would be great as function pointers is pretty difficult for me!

Thanks in advance.
coolguy

coolguy_003
Newbie Poster
2 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

Here is a link that gives you links for function pointer tutorials.
http://www.google.com/search?q=function+pointer+tutorial
Given you've already implemented stack, you already know that part. If you are looking for something more specific, you'll have to make the question specific. :)

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

I am very confused. How in the world are you going to implement a stack using function pointers?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Man you surely need prayers not just answers, I wish I could help, My thoughts with you!

bobs360
Newbie Poster
8 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
 

> I am very confused. How in the world are you going to implement a stack using function pointers?
I guess he means by using dynamic memory allocation, using a node structure which holds the data as well as the pointer to the next node.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

That doesn't involve function pointers though :-/

Mentioning function pointers must be a sign of being completely lost, since there's only a finite constant set of values a function pointer can attain in any given executable; you might as well implement a stack using a bounded set of integers, or something crazy like that.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Hi thekashyap,
Your links are a saver. I tried previously for links but could get only two.
I was just trying to do basic stack operations of push and pop.
Thanks again for the links and also for the quick reply.
:)
coolguy

coolguy_003
Newbie Poster
2 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

/*stacks using pointers*/
#include
#include
#include
#include
struct node
{
int info;
struct node *next;
}*list=NULL;
void insert();
void del();
void display();
void main()
{
int c;
clrscr();
printf("MENU\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
do
{
printf("\nEnter the index number of the categories mentioned above\n");
scanf("%d",&c);
switch(c)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}while(c<=3);
getch();
}
void insert()
{
int item;
struct node *ptr,*q;
ptr=((struct node*)malloc(sizeof(struct node)));
printf("\nEnter the element to be inserted:\n");
scanf("%d",&item);
printf("\nThe Given element has been inserted\n");
ptr->info=item;
ptr->next=NULL;
if(list==NULL)
list=ptr;
else
{
q=list;
while(q->next!=NULL)
q=q->next;
q->next=ptr;
}
}
void del()
{
struct node *p,*q;
if(list==NULL)
printf("\nDeletion is not possible\nThe stack is empty\n");
else
{
p=list;
if(p->next!=NULL)
{
q=p->next;
while(q->next!=NULL)
{
p=q;
q=q->next;
}
if(q->next==NULL)
{
p->next=NULL;
printf("\nThe Deleted Elemet is %d\n",q->info);
free(q);
}
}
else
{
printf("\nThe Deleted Element is %d\n",p->info);
list=NULL;
free(p);
}
}
}
void display()
{
struct node *ptr,*q;
if(list==NULL)
printf("\nThe Stack is Empty\n");
else
{
ptr=list;
q=ptr->next;
printf("\nThe Elements of the stack are:\n");
do
{
printf("%d\t",ptr->info);
ptr=q;
q=q->next;
}while(q->next!=NULL);
if(q->next==NULL)
printf("%d\t%d",ptr->info,q->info);
}
}

nikzkool
Newbie Poster
2 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

though the reply is late... it might be of use to someone else
the program above is the executed program for stacks using pointers

nikzkool
Newbie Poster
2 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

Hello Friend!

That is very nice way to implement stack using function pointers in C.

Hope you are feeling well! If you are not there are many options for you! God Bless!! :) :) :)

anonymous alias
Newbie Poster
20 posts since Dec 2009
Reputation Points: 6
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You