The stack is one of the most important data structures in computer science. A stack is a last-in first-out data structure (LIFO).
===================================================================
Push and Pop operation of stack.
=================================================================
Push and Pop operation of stack using linked list.
The fundamental operation of stack is push and pop. The term push is use to place some data element into the stack and pop is use to remove some data element from the stack.
Stack Overview
Stack follows the rule of last in first out rule. Mainly two action are performed by stack one is push and other is pop. The last thing which we placed or push on stack is the first thing we can get when we pop. A stack is a list of element with insertion and deletion at one end only.
Below depict the operational behaviors of stack. You can see one thing that push and pop operation is done at one end and whatever element we keep on stack is shown at the top of stack.
Push and Pop operation of stack.
This tutorial demonstrate the push and pop operation of stack using array.
Description:
In this tutorial of data-structure you will see push and pop operation of stack. In the previous tutorial is clearly explained the push pop operation. The drawback of implementing stack is that the size of stack is fixed it cannot grow or shrink.
Code:
#include <stdio.h> #include <stdlib.h> void push(int stack[], int *top, int value) { if(*top < 4 ) { *top = *top + 1; stack[*top] = value; printf("\nStack element should be less than four\n"); } else { printf("\nThe stack is full can not push a value\n"); exit(0); } } void pop(int stack[], int *top, int * value) { if(*top >= 0 ) { *value = stack[*top]; *top = *top - 1; } else { printf("\nThe stack is empty can not pop a value\n"); exit(0); } } void main() { int stack[4]; int top = 0; int n,value; do { do { printf("\nEnter the element to be pushed\n"); scanf("%d",&value); push(stack,&top,value); printf("\nEnter 1 to continue and other key to pop\n"); scanf("%d",&n); printf(""); } while(n == 1); printf("\nEnter 1 to pop an element\n"); scanf("%d",&n); while( n == 1) { pop(stack,&top,&value); printf("\nThe value poped is %d",value); printf("\nEnter 1 to pop an element\n"); scanf("%d",&n); } printf("\nEnter 1 to continue and other key to push\n"); scanf("%d",&n); } while(n == 1); }
Output:
=================================================================
Push and Pop operation of stack using linked list.
The fundamental operation of stack is push and pop. The term push is use to place some data element into the stack and pop is use to remove some data element from the stack.
Description:
In this tutorial of data-structure you will see push and pop operation of stack using linked list. In the previous tutorial the stack operation in done using array.
Code:
# include <stdio.h> # include <stdlib.h> struct node { int data; struct node *link; }; struct node *push(struct node *p , int value) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); if(temp==NULL) { printf("No Memory available\n"); exit(0); } temp->data = value; temp->link = p; p = temp; return(p); } struct node *pop(struct node *p , int *value) { struct node *temp; if(p==NULL) { printf(" The stack is empty and cannot pop element\n"); exit(0); } *value = p->data; temp = p; p = p->link; free(temp); return(p); } void main() { struct node *top = NULL; int n,value; do { do { printf("Enter the element to be pushed in stack\n"); scanf("%d",&value); top = push(top,value); printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); printf("Enter 1 to pop an element from stack\n"); scanf("%d",&n); while( n == 1) { top = pop(top,&value); printf("The value poped is %d\n",value); printf("Enter 1 to pop an element and other to push\n"); scanf("%d",&n); } printf("Enter 1 to continue\n"); scanf("%d",&n); } while(n == 1); }
No comments:
Post a Comment