Data Structure - [STACK Operations]

♠ Posted by Unknown in , at 03:24

STACK Operations

A STACK is defined formally as a list (a linear DATA STRUCTURE) in which all insertion and deletions are made at one end called the Top Of Stack (TOS). The fundamental operations, which are possible on a stack, are:
1.         Push à Insertion
2.         Pop à Deletion
3.         Peep à Extract Information
4.         Update à Update information associated at some location in the stack.

The most recently pushed element can be checked prior to performing a pop operation. A stack is based on the Last In First Out algorithm (LIFO) in which insertion and deletion operations are performed at one end of a stack. Also, the information can only be removed in the opposite order in which it was added to the stack. The most accessible information in a stack is at the top of stack and least accessible information is at the bottom of the stack.

For example, consider a stack of plates on the counter in a cafeteria. During the dinner time customers take plates from the top of the stack and waiter puts the washed plates on the top of the stack. The plate that is put most recently on the stack is the first one to bed taken off. The plate at the bottom is the last one to be used. 

A pointer TOS keeps track of the top most information in the STACK.  Initially when the stack is empty, TOS has a value zero and when the stack contains a single information TOS has a value one and so on. Before an item is pushed onto the stack, the pointer is incremented by one. The pointer is decrements by one each time a deletion is made from the stack.

The Algorithm Of STACK operations:

1. Push Operation:

Push means to insert an item onto the stack. The push algorithm is illustrated below. The algorithm illustrated inserts an item to the top of stack, which is represented by array S and containing Size number of items with a pointer Tos denoting the position of top most item in the stack.

                                    Step 1  : [check for stack overflow]
                                                If Tos >= Size
                                                Output “Stack is Overflow” and exit
                                    Step 2  :           [Increment the Pointer value by one]
                                                Tos = Tos + 1
                                    Step 3  :           [perform insertion]
                                                S[Tos] = Value
                                    Step 4  :           Exit

2. Pop Operation:

The pop operation deletes or removes the topmost item from the stack. After removal of top most information, new value of the pointer Tos becomes the previous value of Tos that is Tos = Tos – 1 and freed position is allocated to free space. The algorithm to remove an item from the stack is illustrated below.

                                    Step 1  :[check whether the stack is empty]
                                                If Tos = 0
                                                Output “Stack underflow” and exit

                                    Step 2  :[Remove the Tos information]
                                                Value = S[Tos]
                                                Tos = Tos – 1
                                    Step 3  :[Return the former information of the stack]
                                    Return (Value)            

Example:

/* STACK IMPLEMENTATION USING POINTER AND STRUCTURE */

#include<stdio.h>
#define SIZE 5
struct library
{
 int b_no[SIZE];
}*book;

int cnt=0;
main()
{
 int ans;
 clrscr();
 do
 {
  printf("1. Enter the New Element :\n");
  printf("2. Remove The Element :\n");
  printf("3. Exit :\n");
  printf("Enter Your Choice :");
  scanf("%d",&ans);
  switch(ans)
  {
            case 1:
                        push(book);
                        break;
            case 2:
                        pop(book);
                        break;
            case 3:
                        break;
            default:
                        printf("You Have Entered Wrong Choice...\n");
  }
 }while(ans != 3);
 getch();
 return;
}

push(struct library *bk)
{
 int newitem;
 if(cnt >= SIZE)
 {
  printf("The STACK is full...\n");
  return;
 }

 else
 {
  printf("Enter the New Value :");
  scanf("%d",&newitem);
  bk->b_no[cnt++] = newitem;
 }
 getch();
 return;
}

pop(struct library *bk)
{
 int remitem;
 if(cnt <= 0)
 {
  printf("The STACK is empty...\n");
  return;
 }
 else
 {
  remitem = bk->b_no[--cnt];
  printf("The Removeble item is %d\n",remitem);
 }
 getch();
 return;
}

0 comments:

Post a Comment