STACKS 3.1 INTRODUCTION 3.2 DEFINITION

Similar documents
Application of Stack (Backtracking)

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Prepared By:- Dinesh Sharma Asstt. Professor, CSE & IT Deptt. ITM Gurgaon

Linear Data Structure

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Types of Data Structures


DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Lecture No.04. Data Structures

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

UNIT VI. STACKS AND QUEUES

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Formal Languages and Automata Theory, SS Project (due Week 14)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 16 EXAMINATION Model Answer Subject Code:

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

C: How to Program. Week /Mar/05

Infix to Postfix Conversion

CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar


Stack and Its Implementation

Lecture Data Structure Stack

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

DEEPIKA KAMBOJ UNIT 2. What is Stack?

NCS 301 DATA STRUCTURE USING C

UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING B.E SECOND SEMESTER CS 6202 PROGRAMMING AND DATA STRUCTURES I TWO MARKS UNIT I- 2 MARKS

Operators in C. Staff Incharge: S.Sasirekha

Guide for The C Programming Language Chapter 5

The time and space are the two measure for efficiency of an algorithm.

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Computers Programming Course 5. Iulian Năstac

Dynamic Memory Allocation

Basics of Programming

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Top of the Stack. Stack ADT

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

MODULE V: POINTERS & PREPROCESSORS

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Unit 3. Operators. School of Science and Technology INTRODUCTION

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #13

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Data Structures Week #3. Stacks

Content: Learning Objectives

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

Chapter 2 - Introduction to C Programming

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

CSE 230 Intermediate Programming in C and C++

STACKS AND QUEUES. Problem Solving with Computers-II

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Module III. Linear Data Structures and their Linked Storage Representation

Chapter 1 & 2 Introduction to C Language

DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

CS 211 Programming Practicum Spring 2018

Top of the Stack. Stack ADT

Q 1. Attempt any TEN of the following:

Subject: Fundamental of Computer Programming 2068

UNIT 3 OPERATORS. [Marks- 12]

Data Structure - Stack and Queue-

UNIT IV INTRODUCTION TO C

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Stack Abstract Data Type

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

12 Abstract Data Types

Programming and Data Structures

Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

Cpt S 122 Data Structures. Data Structures

Programming and Data Structure Solved. MCQs- Part 2

Linked List. April 2, 2007 Programming and Data Structure 1

Solution for Data Structure

M1-R4: Programing and Problem Solving using C (JULY 2018)

Review of the C Programming Language for Principles of Operating Systems

a) State the need of data structure. Write the operations performed using data structures.

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Procedural programming with C

Problem with Scanning an Infix Expression

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

POLYNOMIAL ADDITION. AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition.

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

UNIT-I Fundamental Notations

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Transcription:

STACKS 3 3.1 INTRODUCTION A stack is a linear data structure. It is very useful in many applications of computer science. It is a list in which all insertions and deletions are made at one end, called the top of the stack. Some of the analogies used to visualize this data structure are a) Stack of Trays (or) Plates Placed on a Table Here, one plate is placed on top of another, thus creating a stack of plates. Suppose, a person takes a plate off the top of the stack of plates. The plate most recently placed on the stack is the first one to be taken off. The bottom plate is the first one placed on the stack and the last one to be removed. Fig. 3.1 Stack of plates b) Another Familiar Example of a Stack is a Railway Station for Shunting Cars In this example, the last railway car placed on the stack is the first to leave. c) Shipment in a Cargo For the shipment of goods, they have to be loaded into a cargo. During unloading, they are unloaded exactly in the opposite order as they are loaded, that is, the Cargo goods which are loaded last should be unloaded first. 3.2 DEFINITION A stack is an ordered collection of homogeneous Stack data elements where the insertion and deletion operations occur at one end only, called the top of the stack. Fig. 3.3 shows the schematic diagram of a stack. Fig. 3.2 A railway shunting The alternative representation of a stack is system represenation of a stack given in Figure. 3.4. Other names for a stack are pushdown list, and LIFO (or) Last In First Out list. The stack allows access to only one item, i.e., the last item inserted. In the schematic diagram of a stack, after inserting Item 4 into the stack, it acts as the topmost element. It will be removed first. The very first item that was inserted into the stack is Item 1. It will be removed as the last item.

Stacks 69 Push Pop Item 4 Top Item 3 Item 2 Bottom Item 1 Bottom Top Fig. 3.3 Schematic diagram of a stack Fig. 3.4 Alternative representation of a stack 3.3 PRIMITIVE OPERATIONS The primitive operations that can be performed on a stack are given below: 1. Inserting an element into the stack (PUSH operation) 2. Removing an element from the stack (POP operation) 3. Determining the top item of a stack without removing it from the stack (PEEP operation) The syntax used for the PUSH operation is PUSH (stack, item) where stack is the name of the stack into which the item specified as the second argument is placed at the top position of the stack. The syntax used for the POP operation is POP(stack) where stack is the name of the stack from which the item has to be removed, i.e., the element at the topmost position of the stack is removed. Consider a stack structure as given in Figure 3.5(a). After inserting (PUSH) an element (viz, 55) into this stack, the contents of the stack after the PUSH operation is given in Figure 3.5(b). Suppose if the POP operation is specified as POP(st). Then the content of the stack can be visualized as in Figure 3.5(c). Top 44 33 22 55 44 33 22 Top 44 33 22 Top Fig. 3.5(a) Fig. 3.5(b) Fig. 3.5(c) That is, the element pointed out by Top will be removed and then Top will be decremented by 1. So it points the item placed below the removed item. It is also possible to verify the item placed at the top of the stack without removing it. This operation is called PEEP. The syntax used for this operation is

70 Data Structures Using C PEEP(st) This operation is the combination of POP and PUSH. It is equivalent to the following statements: i = POP(st) PUSH(st,i) POP removes the item from the position pointed out by Top. It is in variable i. Then using the PUSH instruction we are inserting the same item i into the stack. Now the variable i has the value in the topmost position of the stack. 3.3.1 Other Operations The other operations that can be performed on the stack are the following: 1) Checking the Emptiness of the Stack It is not possible to pop elements from the empty stack. So at the time of issuing the POP operation, the stack should be nonempty. The syntax used to check the empty condition of the stack is Empty(stack) It returns TRUE if the stack is empty and FALSE otherwise. 2) Checking the Full Condition of the Stack If the stack is full, it is not possible to push elements into it. So before issuing the PUSH operation, make sure that the stack is not full. The syntax used to check the full condition of the stack is Full (stack) It returns TRUE if the stack is Full and FALSE otherwise. 3) Before Using the Stack, the Size of the Stack and the Variable which Points the Top Element of the Stack have to be Initial ized 3.4 AN ABSTRACT DATA TYPE (ADT) An Abstract Data Type (ADT) is a keyword used in a programming language to specify the amount of memory needed to store data and the type of data that will be stored in that memory location. However, an ADT does not specify how many bytes to reserve for the data, because the number of bytes reserved for an ADT varies from one programming language to another. ADT for Stack This is a Last-In First-Out structure. Items can only be inserted and removed from the top of the stack. The ADT for stack are the following: 1) Push() (or) Put (or) insert new item on top of stack 2) Pop() remove and return top item of stack 3) Top() Verifies the top item of stack 3.5 IMPLEMENTATION A stack can be implemented in any one of the following two ways : 1. Using an array 2. Using a linked list

Stacks 71 3.5.1 Array Implementation of Stacks In the array implementation of a stack, a stack can grow either towards the highindexed end of the array (or) towards the low-indexed end of the array. The array implementation of the stack which grows towards the low indexed end of the array is shown in Fig. 3.6. Top Maximum length Last element First element The array implementation requires the following. 1) An array Fig. 3.6 An array implementation of a stack 2) A variable top to hold the topmost element of the stack Here, the stack is of fixed size. This means that some maximum limit is specified for storing the elements into the stack. Once the maximum limit is reached, it is not possible to store the elements into it. In Figure 3.6, the low-indexed end of the array holds the last element that can be retrieved from the array, i.e., the very first element that was pushed into the stack occupies the last index position of the array. The last element that is pushed into the stack is pointed out by the variable top. It is the first to be removed from the stack. The array implementation has the following disadvantages. 1. Memory space is wasted (because of storing less number of items in the array than the maximum size of the array). 2. There is a limitation in storing the items into the stack. 3. Insertion and deletion operations in a stack using an array involves more data movements. The advantage of this method is that the implementation of the stack using an array is easy. A sample program that illustrates the array implementation of a stack is given below: Program /* Array implementation of a stack */ #include<stdio.h> #include<conio.h> int s[20],item,i,n=10; extern top=-1; main() while(1) clrscr(); printf("array implementation of a stack\n "); printf("1. Insertion \n"); printf("2. Deletion \n"); printf(" \n"); printf(" \n"); printf("enter your choice (1..4) "); scanf("%d",&i); switch(i)

72 Data Structures Using C case 1: printf("insertion \n"); printf("enter Item "); scanf("%d",&item); insert(item); case 2: printf("deletion\n"); dele(); case 3: display(); case 4: exit(1); insert(item) if(top>=n) printf("stack Full"); top=top+1; s[top]=item; return; dele() if(top<0) printf("stack empty"); return; item=s[top]; printf("deleted item is %d",item); getch(); top=top-1; return; /*Function used to display the content of the stack in the LIFO manner*/ display() int i=0; printf("content of the stack is :"); while(i <= top) printf("%d ",s[i]); i++; getch(); return;

Stacks 73 Sample Input and Output Array implementation of a stack 1. Insertion 2. Deletion Enter your choice (1..4)1 Insertion Enter Item 11 Array implementation of a stack 1. Insertion 2. Deletion Enter your choice (1..4)1 Insertion Enter Item 12 Array implementation of a stack 1. Insertion 2. Deletion Enter your choice (1..4)3 Content of the stack is :11 12 Array implementation of a stack 1. Insertion 2. Deletion Enter your choice (1..4) 4 3.5.2 Linked List Implementation of a Stack In the case of linked-list implementation of a stack, the first element inserted into the stack is pointed out by the second element, the second element by the third, and so on. In general the (n-1) th element is pointed out by the n th element. The pointer address corresponding to the n th element has to be maintained. The linked-list implementation of a stack is as follows. The linked-list implementation of a stack requires the following: 1. Structure declaration containing some n fields. 2. Pointer to the first node (or) the last node. Data Pointer Data Pointer Data Pointer First element pushed Last element pushed Fig. 3.7 Linked-list implementation of a stack

74 Data Structures Using C Using this, the linked list can be traversed. Here, the stack does not need to be of fixed size. There can be any number of elements or nodes in the stack. This is the notable advantage of the linked-list implementation of a stack. The second advantage of this method is that insertion and deletion operations do not involve more data movements (just the change of pointers). The third advantage of this method is that memory space is not wasted, because memory space is allocated only when the user wants to push an element into the stack. The disadvantage of this method is that more space is used for each node (because pointer fields are involved). In this arrangement, initially memory is allocated for the first node that has to be pushed into the stack. Here, each node can have the following fields. a) Data field b) Pointer field For the first node, the pointer field will have the value NULL because it cannot point to any node (that is, no previous node exists for the first node). When the user creates the next node, memory space for this node should be allocated, and the starting address of this node should be placed in the pointer field of the previously created node (that is, in the immediate predecessor node) When the user wants to remove or pop an element from the stack, the node pointed out by the pointer Top is removed and then Top should be modified such that it should have the address of the immediate predecessor node of this deleted node. The disadvantage of this method is that, in each node some space is used by the pointer field whereas in the array implementation, no pointer field is used. Comparing the advantages and disadvantages of this method and the array implementation, this linked-list method of implementation has more advantages. So this linked-list method of implementation is an efficient one than the array implementation of a stack. The following program illustrates the implementation of a stack using a linked list. Program /* Linked list implementation of a stack. */ #include <stdio.h> #include <conio.h> #include <alloc.h> /* structure containing data part and link part */ struct node int d ; struct node *link ; ; struct node *top = NULL; void push(int) ; int pop(); void display(); void main( ) int item,ch; clrscr( ); while(1)

Stacks 75 printf("linked list implementation of a stack\n"); printf("1.push...\n"); printf("2.pop...\n"); printf("3.display...\n"); printf("4.exit...\n"); printf("enter your choice(1..4) "); scanf("%d",&ch); switch(ch) case 1: printf("enter the item to be inserted into the stack : "); scanf("%d",&item); push(item); case 2: item = pop(); printf ( "\nitem popped: %d",item) ; getch(); case 3: display(); getch(); case 4: exit(1); /* Function used to add a new node to the stack */ void push (int item) struct node *temp ; temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ; if (temp == NULL) printf ( "\nstack is full" ) ; temp -> data = item ; temp -> link = top ; top = temp ; /* Function pops an element from the stack */ int pop() struct node *temp ; int item ; if (top == NULL) printf ( "\nstack is empty." ) ; return NULL ; temp = top; item =temp -> data; top = top -> link; free (temp) ; return item ;

76 Data Structures Using C /* Function used to display the content of the stack */ void display () struct node *top1; int item; top1 = top; if (top1 == NULL) return; while (top1!= NULL) item = top1 -> d; printf("%d ",item); top1 = top1 -> link ; Sample Input and Output Linked list implementation of a stack 1. Push 2. Pop Enter your choice(1..4) 1 Enter the item to be inserted into the stack : 11 Linked list implementation of a stack 1. Push 2. Pop Enter your choice(1..4) 3 11 Linked list implementation of a stack 1. Push 2. Pop Enter your choice(1..4) 4 3.6 APPLICATIONS OF STACK In this subsection, three applications of stacks are explained. They are the following: 1. Infix to postfix conversion 2. Evaluation of arithmetic expressions 3. Decimal to binary conversion 3.6.1 Infix to Postfix Conversion An arithmetic expression is the combination of variables, constants and arithmetic operators. The arithmetic operators used are

Stacks 77 + (addition) (subtraction) * (multiplication) / (division) ^ (exponentiation) % (Remainder operator) Variables and constants may be preceded by unary operators like, +. To evaluate any arithmetic expression, the order of precedence and associativity of the operators should be known. This is as follows. Operators Precedence Associativity (unary) + (unary) 4 - ^ (Exponentiation) 3 Right to left *, / 2 Left to right +, 1 Left to right The arithmetic expression A + B * C/D E ^ F * G is evaluated as fol- Example lows: A + B * C / D E ^ F * G 2 1 5 3 4 To override the rules of precedence and associativity, the arithmetic expression may be parenthesized as follows. ((A + B) * ((C/D) (E ^ (F * G)))) With these parenthesization, the innermost parenthesis part will be evaluated first, then the next innermost, and so on. ((A + B) ((C / D) - (E ^ F * G))) 6 1 2 3 4 5 6 The problem in evaluating the arithmetic expression of this form is that the expression must be scanned from left to right repeatedly. To overcome this problem, the following should be done. a) Conversion of a given expression into a special notation b) Evaluation of that new expression using a stack