Linear Data Structure

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

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


An Introduction to Trees

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

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

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

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

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

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Stacks and their Applications

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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?

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

Stack and Its Implementation

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

Infix to Postfix Conversion

Problem with Scanning an Infix Expression

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

Solution for Data Structure

Lecture 4 Stack and Queue

Lecture Data Structure Stack

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

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

Lecture No.04. Data Structures

March 13/2003 Jayakanth Srinivasan,

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

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

UNIT VI. STACKS AND QUEUES

Stacks Fall 2018 Margaret Reid-Miller

STACKS 3.1 INTRODUCTION 3.2 DEFINITION

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Stacks. Ordered list with property: Insertions and deletions always occur at the same end. INSERT DELETE A3 A3 TOP TOP TOP

1. Stack Implementation Using 1D Array

Stacks, Queues (cont d)

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

Algorithms and Data Structures

STACKS AND QUEUES. Problem Solving with Computers-II

Top of the Stack. Stack ADT

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

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

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

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

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

S.E. Sem. III [CMPN] Data Structures. Primitive Linear Non Linear

The Stack and Queue Types

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

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

CS W3134: Data Structures in Java

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Top of the Stack. Stack ADT

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

Postfix (and prefix) notation

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

CS 206 Introduction to Computer Science II

// The next 4 functions return true on success, false on failure

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear

Stacks. Revised based on textbook author s notes.

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

CS 211 Programming Practicum Spring 2018

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS THE STACK

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

BBM 201 DATA STRUCTURES

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

CSE 214 Computer Science II Stack

BBM 201 DATA STRUCTURES

CS 211 Programming Practicum Spring 2017

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

UNIT 3

What can we do with Coin dispenser?


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

Data Structure & Algorithms Laboratory Manual (CS 392)

[CS302-Data Structures] Homework 2: Stacks

Code No: R Set No. 1

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Stacks, Queues and Hierarchical Collections

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Tribhuvan University Institute of Science and Technology Computer Science and Information Technology (CSC. 154) Section A Attempt any Two questions:

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

CS 211. Project 5 Infix Expression Evaluation

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Problem with Scanning an Infix Expression

Stack and Queue. Stack:

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

Lecture 12 ADTs and Stacks

Types of Data Structures

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Computers Programming Course 6. Iulian Năstac

Recursive Data Structures and Grammars

12 Abstract Data Types

Stack Abstract Data Type

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Transcription:

Linear Data Structure Definition A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal : Visit every part of the data structure Search : Traversal through the data structure for a given element Insertion : Adding new elements to the data structure Deletion: Removing an element from the data structure. Sorting : Rearranging the elements in some type of order(e.g. Increasing or Decreasing) Merging : Combining two similar data structures into one STACK a pile of elements Introduction: 1. Stack is basically a data object 2. The operational semantic (meaning) of stack is LIFO i.e. last in first out Example: Definition: It is an ordered list of elements n, such that n>0 in which all insertions and deletions are made at one end called the top. Primary operations defined on a stack: 1. PUSH : add an element at the top of the list. 2. POP : remove at the top of the list. 3. Also "IsEmpty()" and IsFull()" function, which tests whether a stack is empty or full respectively. 1. Practical daily life : a pile of heavy books kept in a vertical box, dishes kept one on top of another 1

2. In computer world: In processing of subroutine calls and returns ; there is an explicit use of stack of return addresses. Also in evaluation of arithmetic expressions, stack is used. PROBLEM 1 Large number of stacks can be expressed using a single one dimensional stack only. Such an array is called a multiple stack array. Tower of Hanoi Tower of Hanoi is a historical problem, which can be easily expressed using recursion. There are N disks of decreasing size stacked on one needle, and two other empty needles. It is required to stack all the disks onto a second needle in the decreasing order of size. The third needle can be used as a temporary storage. The movement of the disks must confirm to the following rules - 1. Only one disk may be moved at a time 2. A disk can be moved from any needle to any other. 3. The larger disk should not rest upon a smaller one. Question: write a c program to implement tower of Hanoi using stack? Solution: /* Program of towers of Hanoi. */ #include <stdio.h> #include <conio.h> void move ( int, char, char, char ) ; void main( ) int n = 3 ; clrscr( ) ; move ( n, 'A', 'B', 'C' ) ; getch( ) ; void move ( int n, char sp, char ap, char ep ) if ( n == 1 ) printf ("\nmove from %c to %c ", sp, ep ) ; else move ( n - 1, sp, ep, ap ) ; 2

PROBLEM 2 move ( 1, sp, ' ', ep ) ; move ( n - 1, ap, sp, ep ) ; Function Calls and Stack A stack is used by programming languages for implementing function calls. Write a program to check how function calls are made using stack. Solution: /* To show the use of stack in function calls */ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <dos.h> unsigned int far *ptr ; void ( *p )( void ) ; void f1( ) ; void f2( ) ; void main( ) f1( ) ; f2( ) ; printf ( "\nback to main..." ) ; exit ( 1 ) ; void f1( ) ptr = ( unsigned int far * ) MK_FP ( _SS, _SP + 2 ) ; printf ( "\n%d", *ptr ) ; p = ( void ( * )( ) ) MK_FP ( _CS, *ptr ) ; ( *p )( ) ; printf ( "\ni am f1( ) function " ) ; void f2( ) 3

printf ( "\ni am f2( ) function" ) ; PUSH & POP Algorithms Push (item,array, n, top) If ( n> = top) Then print "Stack is full" ; Else top = top + 1; array[top] = item ; Pop (item,array,top) if ( top<= 0) Then print " stack is empty". Else item = array[top]; top = top - 1; 4

PUSH OPERATION Step - 1 Step 2 5

Step 3 Step 4 6

Step 5 Step 6 7

Step 7 Step 8 8

Step 9 POP OPERATION Step 10 9

Step 11 Step 12 10

Arithmetic Expressions Arithmetic expressions are expressed as combinations of: 1. Operands 2. Operators (arithmetic, Boolean, relational operators) Various rules have been formulated to specify the order of evaluation of combination of operators in any expression. The arithmetic expressions are expressed in 3 different notations: 1. Infix: In this if the operator is binary; the operator is between the 2 operands. And if the operator is unary, it precedes the operand. 2. Prefix: In this notation for the case of binary operators, the operator precedes both the operands. Simple algorithm using stack can be used to evaluate the final answer. 3. Postfix: In this notation for the case of binary operators, the operator is after both the corresponding operands. Simple algorithm using stack can be used to evaluate the final answer. Always remember that the order of appearance of operands does not change in any Notation. What changes is the position of operators working on those operands. RULES FOR EVALUATION OF ANY EXPRESSION: An expression can be interpreted in many different ways if parentheses are not mentioned in the expression. For example the below given expression can be interpreted in many different ways: Hence we specify some basic rules for evaluation of any expression : A priority table is specified for the various type of operators being used: 11

PRIORITY LEVEL OPERATORS 6 ** ; unary - ; unary + 5 * ; / 4 + ; - 3 < ; > ; <= ; >= ;!> ;!< ;!= 2 Logical and operation 1 Logical or operation Algorithm for evaluation of an expression E which is in prefix notation: We assume that the given prefix notation starts with IsEmpty(). If number of symbols = n in any infix expression then number of operations performed = some constant times n. Here next token function gives us the next occurring element in the expression in a left to right scan. The PUSH function adds element x to stack Q which is of maximum length n Evaluate (E) Else Top = 0; While (1) If (x = = operand) PUSH (Q, top, n, x); x= next token (E) If (x = = operator) If (x = = infinity) Pop correct number Print value of stack [top] of operands according to the as the output of the expression the operator (unary/binary) and then perform the opera- tion and store result onto the stack Algorithm for evaluation of an expression E, which is in postfix notation: We assume that the postfix notation specifies end of expression by appending NULL at the end of expression. here next token function gives us the next occurring element in the expression in a left to right scan. 12

The PUSH function adds element x to stack Q which is of maximum length n Evaluate (E) Else Top = 0; While (1) If (x = = operand) PUSH (Q, top, n, x); x= next token (E) If (x = = operator) If (x = = infinity) Pop correct number of Return (stack [top]); operands according to the ope- rands according to the operator (unary/binary) and then perform the operation and store result onto the stack 13