© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu
Quick Links
Easy Studies

Home Lab Exercise Data Structures Lab Exercise ProgramsImplementation of Stack Using Array Abstract Data Type▼

Implementation of Stack Using Array Abstract Data Type:

Algorithm Steps:
  1. Declare an array S of Size N.
  2. Assign TOP as a pointer to denote the top element in the stack.
  3. Get the new element Y to be added in a stack.
  4. If  TOP is greater than or equal to N then display stack overflow; otherwise
  • Set TOP = TOP + 1
  • Set S[TOP] = Y.
  • To delete top element from the stack check if TOP = 0, the display stack underflow, otherwise
  • decrement TOP by one, and display S[TOP + 1]
  • Display the stack S from 1 to TOP

C Program To Implement Stack Using Array Abstract Data Type:

#include #include int i, top, item, s[10]; int max = 10; void push ( int item, int s[ ] ) void pop (int s[ ] ) void display ( int s[ ] ) void display (int s[ ] ) void main( ) { int ch; clrscr( ); top = -1; do { printf ( “\n\n 1.PUSH \n2.POP \n 3. EXIT \n” ); printf (“ \n ENTER UR CHOICE: “); scanf ( “%d” , &ch ); switch (ch) { case 1: printf ( “\tPUSH\n”); if (top >= max – 1) { printf (“\n STACK IS FULL \n “); } else { printf (“\n ENTER AN ELEMENT :”); scanf ("%d", &item); push (item, s); } display(s); break; case 2: printf ("\t\nPOP\n"); if (top < 0 ) { printf ("\nSTACK IS EMPTY"); } else { pop(s); } display(s); break; } } while (ch!= 3) getch( ); } void push ( int item, int s[ ] ) { top = top + 1; s[top] = item; } void pop(int s[ ] ) { item = s[top]; top = top – 1; printf ("\n DELETED ELEMENT IS %d \n", item); } void display( int s[ ] ) { printf ("\n"); for ( i = top; i >= 0; i-- ) { printf ("\n %d", s[i]); } }

SAMPLE INPUT OUTPUT:

1.PUSH
2.POP
3.EXIT

ENTER YOUR CHOICE: 1
PUSH
ENTER AN ELEMENT : 10
1.PUSH
2.POP
3.EXIT
ENTER YOUR CHOICE: 1
PUSH
ENTER AN ELEMENT: 20

1.PUSH
2.POP
3.EXIT
ENTER YOUR CHOICE: 2
POP
DELETED ELEMENT IS:20

10
1.PUSH
2.POP
3.EXIT
ENTER YOUR CHOICE: 3

 
SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑
bottom