Stack on Pointer (Dynamic Memory Allocation) using DevC++
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
node *high = NULL;
void pushdynamic ()
{
node*temp = (node*) malloc (sizeof(node));
printf ("Input values = ");
scanf ("%d",&temp->data);
temp->link = high;
high = temp;
}
void popdynamic ()
{
if (high==NULL)
{
printf ("Stack is Empty");
}
else
{
node *temp;
temp=high;
printf ("Values to be deleted = %d",temp->data);
high=high->link;
free (temp);
}
}
void displaydynamic ()
{
if (high==NULL)
{
printf ("Stack is Empty\nCan't display the elemen");
}
else
{
node *temp;
temp=high;
while(temp!=NULL)
{
printf ("The Elemen of Stack\n");
printf ("%d\n",temp->data);
temp=temp->link;
}
}
}
void selection ()
{
int pilih;
do
{
printf ("\n\nStack Using Dynamic Memory Allocation");
printf ("\n1. Push ");
printf ("\n2. Pop");
printf ("\n3. Display");
printf ("\ninsert your selection = ");
scanf ("%d",&pilih);
switch (pilih)
{
case 1:
pushdynamic ();
break;
case 2:
popdynamic ();
break;
case 3:
displaydynamic ();
break;
}
}while (pilih!=0);
}
int main ()
{
selection ();
printf ("\n");
getche ();
return 0;
}