Example : A data structure stack example/program in Array form using C to create a Stack with user-defined nodes to store 10 numeric values, and then display all the stored values.
#include <stdio.h>

int main() 
{
    int stack[10]; // Stack to store 10 numeric values
    int top = -1;  // Initially, stack is empty
    int value,i;
    printf("Enter 10 numeric values to store in the stack:\n");

    for (i = 0; i < 10; i++) 
	{        
        scanf("%d", &value);
        top++;            // Move top up
        stack[top] = value; // Store value in stack
    }

    printf("\nValues stored in stack are:\n");
    for (i = top; i >= 0; i--) 
	{
        printf("%d ", stack[i]);
    }

    return 0;
}

Output:
Enter 10 numeric values to store in the stack:
10
20
30
40
50
60
70
80
90
100

Values stored in stack are:
100 90 80 70 60 50 40 30 20 10
Example : A data structure stack example/program in Linked List form using C to create a Stack with user-defined nodes to store records with at least two attributes of an employee, and then display all the stored records.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct emp
{
  int empsal;
  char empname[50];  
  struct emp *ptr;  
};
   
struct emp *top, *link, *newnode;
void main()
{
   int i, num;  
	
   printf("Enter no. of nodes you want to create in a stack\n");
   scanf("%d",&num);
	 	 
   printf("Enter %d records of Employee as Employee Salary and Employee name one by one =\n",num);
	 
   //first Node creation code
   top= (struct emp*)malloc(sizeof(struct emp));   
   scanf("%d%s",&top->empsal,top->empname);
   top->ptr=NULL; 
	      
   //remaining Node creation code
   for(i=1;i<num;i=i+1)
   {
	newnode= (struct emp*)malloc(sizeof(struct emp));		
	scanf("%d%s",&newnode->empsal,newnode->empname);
	newnode->ptr=NULL;
			
	newnode->ptr=top;
	top=newnode;			
   }
	
   //Display/Print/Traverse code
   printf("\nThe Output of Stack are = \n");
   link=top;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->empsal,link->empname);
	link=link->ptr;		
   }	
   getch();
}

Output:

Enter no. of nodes you want to create in a stack
5
Enter 5 records of Employee as Employee Salary and Employee name one by one =
50000 A
32000 B
80000 C
32500 D
65000 E

The Output of Stack are =
65000  E
32500  D
80000  C
32000  B
50000  A
Example : A data structure stack example/program in Array form using C to insert new nodes in an existing Stack [Push Operation] and then display all the stored records.
#include <stdio.h>

int main() 
{
    int stack[10]; // Stack to store maximum 10 numeric values
    int top = -1;  // Initially, stack is empty
    int value,i,n;
    printf("Enter 7 numeric values to store in the stack:\n");

    for (i = 0; i < 7; i++) 
	{        
        scanf("%d", &value);
        top++;            // Move top up
        stack[top] = value; // Store value in stack
        }

    printf("\nValues stored in stack are:\n");
    for (i = top; i >= 0; i--) 
	{
        printf("%d ", stack[i]);
        }
    
    printf("\nNow how many values do you want to push (max 10)? ");
    scanf("%d", &n);

    if (n > 10) 
	{
        printf("Cannot push more than 10 values.\n");
        return 0;
        }

    printf("Enter %d numeric values:\n", n);
    for (i = 0; i < n; i++) 
    {
        scanf("%d", &value);

        if (top == 9) 
	{  // Stack is full
            printf("Stack Overflow! Cannot push %d\n", value);
            goto final;
        } 
	else 
	{
            top++;
            stack[top] = value;
            printf("%d pushed to stack at position %d.\n", value, top);
        }
    }
    
    final:
    printf("\nThus, Final values stored in stack are:\n");
    for (i = top; i >= 0; i--) 
    {
        printf("%d ", stack[i]);
    }

    return 0;
}

Output:
Enter 7 numeric values to store in the stack:
10
20
30
40
50
60
70

Values stored in stack are:
70 60 50 40 30 20 10
Now how many values do you want to push (max 10)? 5
Enter 5 numeric values:
80
80 pushed to stack at position 7.
90
90 pushed to stack at position 8.
100
100 pushed to stack at position 9.
110
Stack Overflow! Cannot push 110

Thus, Final values stored in stack are:
100 90 80 70 60 50 40 30 20 10
Example : A data structure stack example/program in Linked List form using C to insert a new node in an existing Stack [Push Operation] and then display all the stored records.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct emp
{
  int empsal;
  char empname[50];  
  struct emp *ptr;  
};
   
struct emp *top, *link, *newnode;
void main()
{
   int i, num;  
	
   printf("Enter no. of nodes you want to create in a stack\n");
   scanf("%d",&num);
	 	 
   printf("Enter %d records of Employee as Employee Salary and Employee name one by one =\n",num);
	 
   //first Node creation code
   top= (struct emp*)malloc(sizeof(struct emp));   
   scanf("%d%s",&top->empsal,top->empname);
   top->ptr=NULL; 
	      
   //remaining Node creation code
   for(i=1;i<num;i=i+1)
   {
	newnode= (struct emp*)malloc(sizeof(struct emp));		
	scanf("%d%s",&newnode->empsal,newnode->empname);
	newnode->ptr=NULL;
			
	newnode->ptr=top;
	top=newnode;			
   }
	
   //Display/Print/Traverse code
   printf("\nThe Output of Stack before insertion are = \n");
   link=top;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->empsal,link->empname);
	link=link->ptr;		
   }
   
   // creation of single node and insertion in stack
   printf("\nEnter new record of Employee as Employee Salary and Employee name =\n");
   newnode=(struct emp*)malloc(sizeof(struct emp));		
   scanf("%d%s",&newnode->empsal,newnode->empname);
   newnode->ptr=NULL;
		
   newnode->ptr=top;
   top=newnode;
	 
   printf("\nThe Output of Stack after insertion are = \n");	
   link=top;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->empsal,link->empname);
	link=link->ptr;		
   }
   getch();
}

Output :
Enter no. of nodes you want to create in a stack
3
Enter 3 records of Employee as Employee Salary and Employee name one by one =
2000 A
1000 C
5000 B

The Output of Stack before insertion are =
5000  B
1000  C
2000  A

Enter new record of Employee as Employee Salary and Employee name =
6000 M

The Output of Stack after insertion are =
6000  M
5000  B
1000  C
2000  A
Example : A data structure stack example/program in Array form using C to delete nodes from an existing Stack [Pop Operation] and then display all the stored values.
#include <stdio.h>

int main() 
{
    int stack[10]; // Stack to store maximum 10 numeric values
    int top = -1;  // Initially, stack is empty
    int value, i;

    printf("Enter 7 numeric values to store in the stack:\n");
    for (i = 0; i < 7; i++) 
    {        
        scanf("%d", &value);
        top++;            // Move top up
        stack[top] = value; // Store value in stack
    }

    printf("\nStack elements before popping (Top to Bottom):\n");
    for (i = top; i >= 0; i--) 
    {
        printf("%d ", stack[i]);
    }

    printf("\n\nPerforming Pop Operations:\n");
    while (top >= 0)   // Continue until stack is empty
    {
        printf("Popped: %d\n", stack[top]);
        top--;  // Move top down after popping
    }

    if (top == -1)
        printf("\nStack is now empty.\n");

    return 0;
}

Output:
Enter 7 numeric values to store in the stack:
10
20
30
40
50
60
70

Stack elements before popping (Top to Bottom):
70 60 50 40 30 20 10

Performing Pop Operations:
Popped: 70
Popped: 60
Popped: 50
Popped: 40
Popped: 30
Popped: 20
Popped: 10

Stack is now empty.
Example : A data structure stack example/program in Linked List form using C to delete a node from an existing Stack [Pop Operation] and then display all the stored records.
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct emp
{
  int empsal;
  char empname[50];  
  struct emp *ptr;  
};
   
struct emp *top, *link, *newnode;
void main()
{
   int i, num;  
	
   printf("Enter no. of nodes you want to create in a stack\n");
   scanf("%d",&num);
	 	 
   printf("Enter %d records of Employee as Employee Salary and Employee name one by one =\n",num);
	 
   //first Node creation code
   top= (struct emp*)malloc(sizeof(struct emp));   
   scanf("%d%s",&top->empsal,top->empname);
   top->ptr=NULL; 
	      
   //remaining Node creation code
   for(i=1;i<num;i=i+1)
   {
	newnode= (struct emp*)malloc(sizeof(struct emp));		
	scanf("%d%s",&newnode->empsal,newnode->empname);
	newnode->ptr=NULL;
			
	newnode->ptr=top;
	top=newnode;			
   }
	
   //Display/Print/Traverse code
   printf("\nThe Output of Stack records before deletion are = \n");
   link=top;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->empsal,link->empname);
	link=link->ptr;		
   }
   
   //Deletion code
	link=top;
	top=top->ptr;
	free(link);
	 
	printf("\nThe Output of Stack records after deletion are = \n");	
	link=top;
   while(link!=NULL)
   {
	printf("%d  %s\n",link->empsal,link->empname);
	link=link->ptr;		
   }
   getch();
}

Output :

Enter no. of nodes you want to create in a stack
5
Enter 5 records of Employee as Employee Salary and Employee name one by one =
2000 A
6000 G
7000 X
9000 N
4000 K

The Output of Stack records before deletion are =
4000  K
9000  N
7000  X
6000  G
2000  A

The Output of Stack records after deletion are =
9000  N
7000  X
6000  G
2000  A

Loading


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.