You are on page 1of 15

Session Objectives

Define Stack & Queue Push and Pop Operation in a stack Discuss LIFO Method Define Enqueue & Dequeue method in a Queue Discuss FIFO Method

Stack : (Pushdown list) It is a linear data structure. The insertion of element on to the stack is called as PUSH operation, and deletion of element from the stack is called as POP operation.

Elements insertion or deletion can be done at one end. It follows Last-In First-Out technique or LIFO technique.

Using TOP pointer Push or Pop operation is performed

Stack Related Terms : Top : This pointer points to the top most element of the stack. The top of the stack indicates its door from where elements are entered or deleted. It is used to verify stacks current position.

Stack Underflow : When there is no element in the stack, the status of the stack is known as Stack Underflow. The TOP is present at the bottom of the stack.
Stack Overflow : When the stack contains equal number of elements as per its capacity and no more elements can be added, the status of stack is known as Stack Overflow. In such position, the TOP rests at the highest position. Storage : The capacity of the stack is known as Storage. Representation of Stack : The stack is represented in various ways. The stack can be shown as completely empty or fully filled or filled with few elements. When stack has no elements, it is called as Empty Stack.

The stack with completely filled elements is called as Filled Stack, and no further elements can be inserted.
A top pointer maintains track of the top elements of the stack. For empty stack, top value is zero.

When stack has one element the value of top is one. Whenever an element is

inserted in the stack the value of top is incremented by one (PUSH Operation)
In the same manner, the value of top is reduced by one when an element is deleted from the stack (POP operation). The push operation can be applied to any stack, but before applying pop operation on the stack. Stack Implementation : (i) Static Implementation : It can be achieved using arrays. Once a size of an array is declared, its size cannot be modified during execution. It is also inefficient for utilization of memory. It cannot be expanded, when we store more elements. The vacant space of stack is wasted, when we store less elements.

(ii)

Dynamic Implementation : Pointer can be used for implementation of

stack. The link list is an example of this implementation. Using this


implementation at runtime, there is no restriction on the number of elements. The stack may be expandable. Memory is efficiently utilized with pointers Operation on stack : The following fundamental operations on stack can be carried out through static implementation called array implementation. (i) creating a stack (ii) Checking stack either full or empty (iii) Initializing a stack (iv) Insert (PUSH) an element into the stack (v) Delete (POP) an element from the stack (vi) Display elements of stack.

Stack using Pointer : Pointer implementation of a stack can be carried out is similar to array implementation. This Dynamic Implementation, create NODE at run time, accordingly PUSH, POP operation can be done.

(i) creating a stack


(ii) Checking stack either full or empty (iii) Initializing a stack (iv) Insert (PUSH) an element into the stack (v) Delete (POP) an element from the stack (vi) Display elements of stack.

#include <stdio.h> struct stacktype { int item[100]; int top; }; struct stacktype stack; int givendata,poppeddata; int choice; isstackfull() { if (stack.top == 100) return 1; else return 0; } isstackempty() { if (stack.top == 0) return 1; else return 0; }
Continued,

void createstack() { stack.top = 0; } void push() { stack.top = stack.top +1; stack.item[stack.top] = givendata; } void pop() { poppeddata = stack.item[stack.top]; stack.top= stack.top -1; } void main() { clrscr(); createstack(stack);
Continued,

do {

printf("\n1. Push"); printf("\n2. Pop"); printf("\n3. Show Top"); printf("\n4. Quit\n"); printf("\n Enter your Choice : "); scanf("%d",&choice);
switch(choice) { case 1: printf("\nEnter data to push : "); scanf("%d",&givendata); if (isstackfull(stack)) printf("\nStack Full!!!,cannot push"); else push(); printf("Data pushed into stack\n"); break;
case 2: if (isstackempty(stack)== 1)
Continued,

printf("\nStack Empty\n"); else { pop(); printf("\nData popped is %d\n",poppeddata); } break; case 3: if (isstackempty()== 1) printf("\nStack empty\n"); else printf("\nTop most item is %d\n",stack.item[stack.top]); break; } }while(choice != 4); }

# include <stdio.h> # include <malloc.h> #define null 0 struct list { int item; struct list *next; }; typedef struct list node; node *start,*temp;

void push() { node *new,*temp; int newitem; while(1) { printf("\nEnter new integer data [0 to exit]:"); scanf("%d",&newitem);
Continued,

if (newitem ==0) break; new = (node *)malloc(sizeof(node)); new->item = newitem; new->next = start; start = new; } } void display() { node *temp; temp = start; if (start == null) printf("\nEmpty Stack"); else while(temp) { printf("%d ",temp->item); temp = temp->next; } } Continued,

void pop() { node *temp; if (start == null) printf("\n Empty Stack"); else { printf("\n Popped item is %d",start->item); temp = start->next; free(start); start = temp; }} void main() { int choice; start = null; clrscr(); do{ printf("\n 1. Push"); printf("\n 2. Pop"); printf("\n 3. Display");
Continued,

printf("\n 4. Exit"); printf("\n Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; } }while(choice != 4); }

You might also like