How To Create A Stack In C
The C Program is written for implementation of STACK using Array, the basic operations of stack are PUSH() and POP().
STACK uses Last in First Out approach for its operations. Push and Pop operations will be done at the same end called "top of the Stack"
PUSH function in the code is used to insert an element to the top of stack, POP function used to remove the element from the top of stack.
Finally, the display function in the code is used to print the values. All stack functions are implemented in C Code.
The same implementation of stack using c is written using pointers: Stack operations using pointers in c
C Program for STACK Using Arrays
#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; } default: { printf ("\n\t Please Enter a Valid Choice(1/2/3/4)"); } } } while(choice!=4); return 0; } 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"); for(i=top; i>=0; i--) printf("\n%d",stack[i]); printf("\n Press Next Choice"); } else { printf("\n The STACK is empty"); } }
OUTPUT:
Enter the size of STACK[MAX=100]:10 STACK OPERATIONS USING ARRAY -------------------------------- 1.PUSH 2.POP 3.DISPLAY 4.EXIT Enter the Choice:1 Enter a value to be pushed:12 Enter the Choice:1 Enter a value to be pushed:24 Enter the Choice:1 Enter a value to be pushed:98 Enter the Choice:3 The elements in STACK 98 24 12 Press Next Choice Enter the Choice:2 The popped elements is 98 Enter the Choice:3 The elements in STACK 24 12 Press Next Choice Enter the Choice:4 EXIT POINT
A new version of the above code which is shorter for real time implementation of stack data structure in C language
#include<stdio.h> #include<stdlib.h> int n, top = -1, *stack; void push(int x){ if(top==n) return; stack[++top]=x; } int pop(){ if(top==-1) return -1; return stack[top--]; } int peek(){ if(top==-1) return -1; return stack[top]; } void display(){ for(int i=top ; i>-1 ; i--) printf("%d ",stack[i]); printf("\n\n"); } int main(){ n = 10; printf("Initializing the stack with size 10\n\n"); stack = (int*)malloc(n*sizeof(int)); printf("Pushing elements into the stack\n1\n2\n3\n\n"); push(1); push(2); push(3); printf("Displaying elements of the stack -\n"); display(); printf("The top of the stack = %d\n\n",peek()); printf("Pop the top of the stack = %d\n\n",pop()); printf("Pop the top of the stack = %d\n\n",pop()); printf("Displaying elements of the stack -\n"); display(); return 0; }
OUTPUT:
Initializing the stack with size 10 Pushing elements into the stack 1 2 3 Displaying elements of the stack - 3 2 1 The top of the stack = 3 Pop the top of the stack = 3 Pop the top of the stack = 2 Displaying elements of the stack - 1
The shorter version of the code was contributed by Nizam.
Queue follows the insert and delete operations through First in First Out approach, check the below program for Queue operations.
Implementation of Queue using Array in C
How To Create A Stack In C
Source: https://www.programming9.com/programs/c-programs/302-implementation-of-stack-using-array-in-c
Posted by: watsonaccee1988.blogspot.com
0 Response to "How To Create A Stack In C"
Post a Comment