Skip to main content

Definition of Stack and its algorithm with working process(data structure)



                 Stack                     
  A Stack is an ordered collection of items where the addition of items and the removal of existing items always takes place at the same end. This end is commonly referred to as the “top".

* working method of stack :-

i> Initially top=-1
ii> By increasing top pointer we can insert element    in the stack.
iii> when top=size of stack-1  then stack is full.
iv> By decreasing the top pointer we can remove   the   element from stack.
v> when top=-1 then the stack is empty.



figure shows run time stack








* The fundamental operations which are possible on a stack are:-
    
   1. push operation (insertion).
   2. pop operation (deletion).
   3. peep opetation(extract information).
  4. update operation (change value at any position).

1. push operation:-
 In case of stack Insertion of any item in stack is called push. In stack any item is inserted from top of the stack, When you insert any item in stack top will be increased by 1.
push operation in stack

* Algorithm for insertion (push operation):-

 Step1: [Check for stack overflow]
             If  top>= size of stack  then
             output:- " stack is overflow "  and exit.

Step 2: [Incriment the pointer value by 1 ]
             top=top+1

Step 3: [perform the insertion operation ]
             S[top] = value.       (here S is array name)

step 4: Exit.


2. pop operation:-
  In case of stack deletion of any item from stack is called pop. In any item is delete from top of the stack, When you delete any item from stack top will be decreased by 1.

pop operation in stack


* Algorithm for deletion (pop operation ):-

Step 1: [Check for stack underflow]
            If top=0  then
            output:- "Stack is underflow "  and exit.

step 2: [Removing the top valve ]
            value = S[top].      

step 3: [decrementing the pointer value by 1 ]
           top = top-1

Step 4: [Return the formal information os the stack ]
             return ( value )


3. peep operation :-
    If we are interested about any information stored at any locatoin in a stack, finding that information is known as peep operation.
                In this operation we simply move the pointer in the desired location fetch the information associated with that location.

* Algorithm for peep operation:-

Step 1: [check for stack underflow ]
            if  top-i +1<= 0 then     (here i is the position which have to extract)
            output:- "stack is underflow " and exit. 

Step 2: [return the ith element from the top of stack]
            return (S[top-i +1])



4. update operation:-
   Update operation is required when the containt of same location in a stack is to be changed. If we want to update value at ith position in a stack S , we move top pointer to the ith loction from the top of the stack and  input new value of that location.

* Algorithm for update opertaion:-
Step 1: If  top-i +1<= 0  then
          output:- "stack is underflow "  and exit.
Step 2: [Change the element ]
          S[top-i+1] = value.

Comments

Post a Comment

Please comment.

Popular posts from this blog

Process Scheduling And Types of Process Schedular :-

        ⇰ PROCESS SCHEDULING Process Scheduling  is a task  of Operating System that schedules processes of different states like new, ready, waiting, terminated  and running.This scheduling helps in allocation of CPU time for each process, and Operating System allocates the CPU time for each procss. And the process scheduling plays important role to keep the CPU busy all the time.  ⏩   Followings are some objectives of Process Scheduling :-  i > To increase the amount of users within acceptable response times.  ii > To maintain the balance between response and utilization of system. iii > To decrease the enforce priorities and  give reference to the processes holding the key resources.      ⇰  PROCESS SCHEDULAR A scheduler carries out the pro cess scheduling work. Schedulers are often implemented so they keep all computer resources busy and  allows multiple users to share system resources  to achieve  multiprogramming .  There are  mainy three types of pro

Process & Its state And process control block :-

                ⇰  PROCESS :- A process can be thought of as a program in execution. Means when any program is executed it becomes process. A processwill need certain resources such as CPU time , memory, files and I/O devices to complete its task. These resources are allocated to the process either when it is created or at the time of execution.             A process is the unit of work in most systems. A system consistes of a collection of processes. All these processes may execute concurrently. Traditionally a process contained only a single thread. Most modern operating ststems now supports processes that have multiple threads.         The operating system is responsible for several important works of process management as - the creation and deletion of process, the schrduling of process, communication and deadlock handling of process. Process is broudly divided into two types:-  i> System  Process. ii> User Process. Early computers allowed only one program be ex

Semaphores In Process Synchronization

   ⇰  Semaphores :-   Semaphore is actually a method or tool to prevent race condition. Race condition can cause loss of data or even deadlock situation. For prevention from these conditions, the semaphore is one of the method.  Semaphore was proposed by Dijkstra in 1965. Simaphore    is a very significant technique to manage concurrent processes.  Semaphore is useful tool in the prevention of race condition. But the use of semaphore never means a guarantee that a program is free from these problems.     Semaphore is an integer variable which is used in mutual exclusive manner by various concurrent cooperative processes in order to acheive synchronization. Hence semaphore is one of the way to achieve synchronization.  Semaphore is basically  a variable which is non-negative and shared between threads. This variable is used to solve the critical section problem and to achieve process synchronization in the multiprocessing environment. Semaphore contains some operations as f