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

Recommended Answers

All 9 Replies

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. :)

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

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

> 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.

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.

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

/*stacks using pointers*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<malloc.h>
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);
}
}

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

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!! :) :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.