The document describes array implementation of a stack data structure in C and Java. Key operations on a stack like push, pop, and peek are implemented using arrays. Push adds an element to the top of the stack by incrementing the top pointer and inserting at that index. Pop removes the top element by decrementing top and returning the element at the new top index. Peek returns the top element without removing it. Underflow occurs on pop/peek of an empty stack, and overflow on push to a full stack. Sample programs demonstrate these stack operations using menus to select push, pop, peek or exit.
The document describes array implementation of a stack data structure in C and Java. Key operations on a stack like push, pop, and peek are implemented using arrays. Push adds an element to the top of the stack by incrementing the top pointer and inserting at that index. Pop removes the top element by decrementing top and returning the element at the new top index. Peek returns the top element without removing it. Underflow occurs on pop/peek of an empty stack, and overflow on push to a full stack. Sample programs demonstrate these stack operations using menus to select push, pop, peek or exit.
Array implementation of Stack: Deletion of an element from a stack
In array implementation, the stack is (Pop operation):
formed by using the array. All the Deletion of an element from the top of the operations regarding the stack are stack is called pop operation. The value of performed using arrays. Lets see how each the variable top will be incremented by 1 operation can be implemented on the stack whenever an item is deleted from the using array data structure. stack. The top most element of the stack is stored in an another variable and then the Adding an element onto the stack top is decremented by 1. the operation (push operation): returns the deleted value that was stored in Adding an element into the top of the another variable as the result. stack is referred to as push operation. Push The underflow condition occurs when we operation involves following two steps. try to delete an element from an already empty stack. 1.Increment the variable Top so that it can now refere to the next memory location. Algorithm : 2.Add element at the position of begin incremented top. This is referred to as if top = 0 then stack empty; adding new element at the top of the stack. item := stack(top); **Stack is overflown when we try to insert top = top - 1; an element into a completely filled stack end; therefore, our main function must always Time Complexity : o(1) avoid stack overflow condition. Implementation of POP algorithm Algorithm: using C language begin int pop () if top = n then stack full { top = top + 1 if(top == -1) stack (top) : = item; { end printf("Underflow"); Time Complexity : o(1) return 0; } Implementation of push algorithm in C else language: { void push (int val,int n) //n is size of the st return stack[top - - ]; ack } { } if (top == n ) printf("\n Overflow"); Visiting each element of the stack else (Peek operation) { Peek operation involves returning the top = top +1; element which is present at the top of the stack[top] = val; stack without deleting it. Underflow } condition can occur if we try to return the top element in an already empty stack. } Algorithm : printf("\n1.Push\n2.Pop\n3.Show\n4. PEEK (STACK, TOP) Exit"); Begin printf("\n Enter your choice \n"); if top = -1 then stack empty scanf("%d",&choice); item = stack[top] switch(choice) return item { End case 1: Time complexity: o(n) { Implementation of Peek algorithm in C push(); language: break; int peek() } { case 2: if (top == -1) { { pop(); printf("Underflow"); break; return 0; } } case 3: else { { show(); return stack [top]; break; } } } case 4: { C program: printf("Exiting...."); #include <stdio.h> break; int stack[100],i,j,choice=0,n,top=-1; } void push(); default: void pop(); { void show(); printf("Please Enter valid choice "); void main () } { }; printf("Enter the number of elements in } the stack "); } scanf("%d",&n); void push () printf("*********Stack operations usin { g array*********"); int val; if (top == n ) printf("\n---------------------------------------- printf("\n Overflow"); ------\n"); else while(choice != 4) { { printf("Enter the value?"); printf("Chose one from the below opt scanf("%d",&val); ions...\n"); top = top +1; stack[top] = val; } return false; } } void pop () else { { if(top == -1) System.out.println("Enter Value"); printf("Underflow"); int val = sc.nextInt(); else top++; top = top -1; arr[top]=val; } System.out.println("Item pushed"); void show() { return true; for (i=top;i>=0;i--) } { } printf("%d\n",stack[i]); boolean pop () } { if(top == -1) if (top == -1) { { printf("Stack is empty"); System.out.println("Underflow !!") } ; } return false; } Java Program: else import java.util.Scanner; { class Stack top --; { System.out.println("Item popped"); int top; int maxsize = 10; return true; int[] arr = new int[maxsize]; } } void display () boolean isEmpty() { { System.out.println("Printing stack ele return (top < 0); ments ....."); } for(int i = top; i>=0;i--) Stack() { { System.out.println(arr[i]); top = -1; } } } boolean push (Scanner sc) } { public class Stack_Operations { if(top == maxsize-1) public static void main(String[] args) { { int choice=0; System.out.println("Overflow !!"); Scanner sc = new Scanner(System.in); Stack s = new Stack(); System.out.println("*********Stack op } erations using array*********\n"); } System.out.println("\n---------------------- --------------------------\n"); while(choice != 4) { System.out.println("\nChose one fro m the below options...\n"); System.out.println("\n1.Push\n2.Pop\ n3.Show\n4.Exit"); System.out.println("\n Enter your cho ice \n"); choice = sc.nextInt(); switch(choice) { case 1: { s.push(sc); break; } case 2: { s.pop(); break; } case 3: { s.display(); break; } case 4: { System.out.println("Exiting....");