Normal Stack Program in C
Normal Stack Program in C
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}//END OF MAIN FUNCTION
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
3|Page
#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node* next;
};
typedef struct Node Node;
Node *top,*newptr,*save_st,*save_end,*ptr;
struct Node* Create_New_Node(int);
void push(Node*);
void pop(void);
void Display(Node*);
int main()
{
int val,choice;
char ch='y';
top=NULL;
newptr=NULL;
clrscr();
do
{
printf("\n1. push");
printf("\n2. pop");
printf("\n3. Display");
printf("\nEnter Your Choice");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n Enter a value");
scanf("%d",&val);
newptr=Create_New_Node(val);
if(newptr==NULL)
{
printf("\n Stack Is FULL");
exit(0);
}
else
5|Page
{
printf("\n Node Create Successfully\n");
push(newptr);
}
break;
case 2:pop();
break;
case 3: printf("\nElements in the stack are as follows..");
Display(top);
break;
default:printf("\n Wrong Choice");
}//end of switch statement
printf("\n Do you want to continue(y/Y)?");
fflush(stdin);
scanf("%c",&ch);
return 0;
}// end of main
save_st=top;
top=np;
np->next=save_st;
}
printf("\n Your node insert successfully in the stack");
}
/*********************************************************/