0% found this document useful (0 votes)
81 views6 pages

Normal Stack Program in C

1. The document describes a C program that implements a stack using an array and its push, pop, and display functions. It also describes implementing a stack using pointers and linked nodes with the same functions. 2. The array implementation defines the stack array, top pointer, and choice-driven menu to call the push, pop and display functions on the array. 3. The linked node implementation creates nodes with data and next pointer, defines a top node pointer, and implements push to add to the top, pop to remove from the top, and display to traverse the nodes.

Uploaded by

Sourav Gayen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views6 pages

Normal Stack Program in C

1. The document describes a C program that implements a stack using an array and its push, pop, and display functions. It also describes implementing a stack using pointers and linked nodes with the same functions. 2. The array implementation defines the stack array, top pointer, and choice-driven menu to call the push, pop and display functions on the array. 3. The linked node implementation creates nodes with data and next pointer, defines a top node pointer, and implements push to add to the top, pop to remove from the top, and display to traverse the nodes.

Uploaded by

Sourav Gayen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1|Page

// NORMAL STACK PROGRAM IN C


===================================================
#include<stdio.h> 
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
    //clrscr();
    top=-1;
    printf("\n Enter the size of STACK[MAX=100]:");
    scanf("%d",&n);
    printf("\n\t STACK OPERATIONS USING ARRAY");
    printf("\n\t--------------------------------");
    printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
    do
    {
        printf("\n Enter the Choice:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            {
                push();
                break;
            }
            case 2:
            {
                pop();
                break;
            }
            case 3:
            {
                display();
                break;
            }
            case 4:
            {
                printf("\n\t EXIT POINT ");
                break;
            }
2|Page

            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

        for(i=top; i>=0; i--)


            printf("\n%d",stack[i]);
        printf("\n Press Next Choice");
    }
    else
    {
        printf("\n The STACK is empty");
    }
    
}
4|Page

// STACK PROGRAM USING POINTER AND STRUCTURE


=================================================

#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);

}while(ch=='y'|| ch=='Y');//end of while

return 0;
}// end of main

/********CODE FOR CREATING A NODE***********/

struct Node * Create_New_Node(int n)


{
ptr=(Node *)malloc(sizeof(Node));
ptr->data=n;
ptr->next=NULL;
return ptr;
}
/*************************************************/

/*********CODE FOR PUSH FUNCTION IN THE STACK*****/


void push(Node * np)
{
if(top==NULL)
{
top=np;
}
else
{
6|Page

save_st=top;
top=np;
np->next=save_st;
}
printf("\n Your node insert successfully in the stack");

}
/*********************************************************/

/************POP FUNCTION FOR STACK*****************/


void pop()
{
struct Node * np;
int val;
np=top;
val=np->data;
top=top->next;
free(np);
printf("\n Than You...Your First element %d is deleted",val);
}
/*********************************************************/

/***********DISPLAY THE ELEMENTS IN STACK************/


void Display(Node *np)
{
while(np!=NULL)
{
printf("%d->",np->data);
np=np->next;
}
}
/***********************************************************/

You might also like