2
37 In most of the high level languages, a stack can be easily implemented either through an array or a linked list. What identifies the data structure as a stack in either case is not the implementation but the interface: the user is only allowed to pop or push items onto the array or linked list, with few other helper operations. Creation of stack means creating array structure without putting value into it. The following will demonstrate both implementations, using C. The array implementation aims to create an array where the first element (usually at the zero-offset) is the bottom. That is, array[0] is placed at the first element pushed onto the stack and the last element popped off. The program must keep track of the size, or the length of the stack. The stack itself can therefore be effectively implemented as a two-element structure in C: typedef struct { int size; int items[STACKSIZE]; } STACK; The push() operation is used both to initialize the stack, and to store values to it. If we use a dynamic array, then we can implement a stack that can grow or shrink as much as needed. The size of the stack is simply the size of the dynamic array. A dynamic array is a very efficient implementation of a stack. The linked-list implementation is equally simple and straightforward. In fact, a simple singly linked list is sufficient to implement a stack-it only requires that the head node or element can be removed, or popped, and a node can only be inserted by becoming the new head node. Unlike the array implementation, our structure typedef corresponds not to the entire stack structure, but to a single node: typedef struct stack { int data; struct stack *next; } STACK; Such a node is identical to a typical singly linked list node, at least to those that are implemented in C. The push() operation both initializes an empty stack, and adds a new node to a non-empty one. It works by receiving a data value to push onto the stack, along with a target stack, creating a new node by allocating memory for it, and then inserting it into a linked list as the new head. Before going into detail we should be familiar with some operations related to stack. These are given below :- Creation of Stack Array Linked list Stack Operations

Operation on Stack

Embed Size (px)

DESCRIPTION

Operation on the Stack

Citation preview

  • 37

    In most of the high level languages, a stack can be easily implemented either through an array or alinked list. What identifies the data structure as a stack in either case is not the implementation but theinterface: the user is only allowed to pop or push items onto the array or linked list, with few otherhelper operations. Creation of stack means creating array structure without putting value into it. Thefollowing will demonstrate both implementations, using C.

    The array implementation aims to create an array where the first element (usually at the zero-offset)is the bottom. That is, array[0] is placed at the first element pushed onto the stack and the last elementpopped off. The program must keep track of the size, or the length of the stack. The stack itself cantherefore be effectively implemented as a two-element structure in C:

    typedef struct {

    int size;

    int items[STACKSIZE];

    } STACK;

    The push() operation is used both to initialize the stack, and to store values to it.

    If we use a dynamic array, then we can implement a stack that can grow or shrink as much as needed.The size of the stack is simply the size of the dynamic array. A dynamic array is a very efficientimplementation of a stack.

    The linked-list implementation is equally simple and straightforward. In fact, a simple singly linkedlist is sufficient to implement a stack-it only requires that the head node or element can be removed,or popped, and a node can only be inserted by becoming the new head node.

    Unlike the array implementation, our structure typedef corresponds not to the entire stack structure,but to a single node:

    typedef struct stack {

    int data;

    struct stack *next;

    } STACK;

    Such a node is identical to a typical singly linked list node, at least to those that are implemented in C.

    The push() operation both initializes an empty stack, and adds a new node to a non-empty one. Itworks by receiving a data value to push onto the stack, along with a target stack, creating a new nodeby allocating memory for it, and then inserting it into a linked list as the new head.

    Before going into detail we should be familiar with some operations related to stack. These are givenbelow :-

    Creation of Stack

    Array

    Linked list

    Stack Operations

  • Push: Push means insert any item in stack. The process of adding a new element to the topof stack is called PUSH operation. Pushing an element in the stack invoke adding of element, as thenew element will be inserted at the top after every push operation the top is incremented by one. Incase the array is full and no new element can be accommodated, it is called STACK-FULL condition.This condition is called stack overflow.

    Pop : Pop means delete an element from stack. The process of deleting an element from thetop of stack is called POP operation. After every pop operation top is decremented by one. If there is no element on the stack and the pop is performed then this will result into stack underflow condition.

    Top : Top tells about the location from where elements can be removed or where elementscan be added. It gets the next element to be removed.

    IsEmpty : This is a condition which checks whether the stack is empty or not, beforeapplying pop operation IsEmpty condition has to be checked to ensure the presence of an element ina stack.

    IsFull : This is a condition which checks whether the stack is full or not, before applying pushoperation IsFull condition has to be checked to find out whether there is any space or not to add anelement in a stack.

    Display : This operation is used to display all the elements present in a stack linearly.