unit-i

55
UNIT-I Topics to be covere d 1.Introduction to data structures. 2. Abstract data type (ADT) 3. Stacks and queues 4. Circular queues and their implementation with arrays. 5. Stack applications: 5.1. infix to post fix conversion 5.2. postfix expression evaluation. 6. Applications of queues

Upload: naava

Post on 19-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

UNIT-I. Topics to be covere d 1.Introduction to data structures. 2. Abstract data type (ADT) 3. Stacks and queues 4. Circular queues and their implementation with arrays. 5. Stack applications: 5.1. infix to post fix conversion 5.2. postfix expression evaluation. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: UNIT-I

UNIT-I

Topics to be covere d1.Introduction to data structures.2. Abstract data type (ADT) 3. Stacks and queues4. Circular queues and their implementation with arrays.5. Stack applications:5.1. infix to post fix conversion5.2. postfix expression evaluation.6. Applications of queues

Page 2: UNIT-I

1.Introduction to data structures. Definition:- A data structure is an arrangement of data in a

computers memory or even on disk storage. It has a different way of storing and organizing data in a computer.

Concept of data structure:- manipulation of real-life data requires the following essential tasks-

1.storage representation of user data 2. Retrieval of stored data 3. transformation of user data

Page 3: UNIT-I

Mathematical definition of data structure is D=(d,f,a)

where D= data structure d= a set of variables (data objects). f= a set of functions a= set of rules to implement the functions.

Page 4: UNIT-I

2. Abstract data type (ADT)

Data structure is implemented around the concept of an abstract data type that defines the both data organization and data handling operations.

ADT refers to the basic mathematical concept that defines the data type.

ADT is also called as user defined data type. Only tells about what are the data values required

and what operations performed on those objects.

Page 5: UNIT-I

Overview of data structures

Several data structures are available in the computer science and used based on their applications.

Data structures are classified into different types.

Data structure

Linear dsNon Linear ds

Arrays

Linked list stack queues

Trees

Graphs

Page 6: UNIT-I

Linear data structure- all the elements are stored in sequential order or linear order.

Non linear data structure- no such sequence in elements, rather all the elements are distributed over a plane.

Page 7: UNIT-I

Applications of data structures Implementing data bases of different

organizations (ds used is B-Trees) Implement compilers for different languages

(ds used is hash tables). Used in every program and software system.

Page 8: UNIT-I

3. Stacks and queues

Stores a set of elements in a particular order

A stack is an ordered collection of homogeneous data elements where the insertion and deletion takes place at one end

Stack principle: LAST IN FIRST OUT = LIFO It means: the last element inserted is the first one to be

removed Example

What is a stack?

Which is the first element to pick up?

Page 9: UNIT-I

Stack maintains a pointer called top, which keeps track of the Stack maintains a pointer called top, which keeps track of the top most element in the stack.top most element in the stack.

Any insertions or deletions should be based upon the value Any insertions or deletions should be based upon the value of top.of top.

It works on the basis of LIFO (Last in First out).It works on the basis of LIFO (Last in First out). According to the definition, new elements are inserted from According to the definition, new elements are inserted from

top and the elements are deleted from same end i.e again top and the elements are deleted from same end i.e again top. top.

This suggests that the element inserted most recently can This suggests that the element inserted most recently can only be deleted.only be deleted.

In the stack, the elements are removed in the reverse order In the stack, the elements are removed in the reverse order of that in which they were added to the stack i.e last element of that in which they were added to the stack i.e last element inserted is to be deleted first.inserted is to be deleted first.

So it is called last in first out.So it is called last in first out.

Page 10: UNIT-I

Ex: Elements are inserted in the order as A,B,C,D.Ex: Elements are inserted in the order as A,B,C,D. It represents the stack of 4 elements.It represents the stack of 4 elements.

The top most element in the stack is E.The top most element in the stack is E.

If we want to delete element only D has to be deleted first.If we want to delete element only D has to be deleted first.

If deletion operation is performed on stack continuously then the If deletion operation is performed on stack continuously then the order in which the elements are deleted is D,C,B,A.order in which the elements are deleted is D,C,B,A.

Page 11: UNIT-I

Last In First Out

A top

A

B

A

top

B

C top

A

B

CD to

p

A

BC top

A

B top

Page 12: UNIT-I

Basic operations:Basic operations: The basic operations are insertion, deletion and display.The basic operations are insertion, deletion and display.

In stacks, special terms are given for insert and delete. i.e push In stacks, special terms are given for insert and delete. i.e push for insert and pop is for delete.for insert and pop is for delete.

Push: inserting or adding element into the stack is called push.Push: inserting or adding element into the stack is called push.

Pop: deleting or removing element from the stack is called pop.Pop: deleting or removing element from the stack is called pop.

Page 13: UNIT-I

TOP=-1(Empty stack)

3

2

10

Push 10

3

2

10 10TOP->

Push 20

3

2

10 10

TOP-> 20

Push 30

3

2

10

TOP->

10

20

30

Push 40

3

2

10

TOP->

10

20

30

40

Push 50(Overflow)

2

10

TOP->

10

20

30

403

Page 14: UNIT-I

pop

3

2

10 10

TOP-> 20

pop

3

10

TOP->

10

20

302

pop

3

2

10 10TOP->

pop (TOP=-1) underflow

3

2

10

Page 15: UNIT-I

Implementing stack using Implementing stack using arraysarraysAlgorithm for inserting element into the stack:Algorithm for inserting element into the stack:

Algorithm push()Algorithm push()1. if top=(size-1)1. if top=(size-1)

then write (‘stack overflow’)then write (‘stack overflow’) ReturnReturn2. read item2. read item3. top3. top←top+1←top+14. stack[top]← item4. stack[top]← item5. stop5. stop

Page 16: UNIT-I

Explanation:Explanation: The stack is of size max. This procedure inserts an element The stack is of size max. This procedure inserts an element

item on to the top of a stack which is represented by an array item on to the top of a stack which is represented by an array stack.stack.

The first step of this algorithm checks for an overflow The first step of this algorithm checks for an overflow condition.condition.

Overflow means inserting element into a stack which is full.Overflow means inserting element into a stack which is full. If the top value reaches to maximum size of the stack then If the top value reaches to maximum size of the stack then

elements cannot be inserted into the stack i.e. stack is full.elements cannot be inserted into the stack i.e. stack is full. Otherwise top is incremented by one and element is inserted Otherwise top is incremented by one and element is inserted

into the stack.into the stack.

Page 17: UNIT-I

Algorithm to delete element from the stack:Algorithm to delete element from the stack:

Algorithm pop()Algorithm pop()1. if top=-11. if top=-1

then write (‘stack underflow’)then write (‘stack underflow’) 2. item 2. item ← stack[top]← stack[top]3. top ← top-13. top ← top-1

Page 18: UNIT-I

Explanation:Explanation: This procedure deletes an element from the stack.This procedure deletes an element from the stack.

The first step of this algorithm checks for underflow condition.The first step of this algorithm checks for underflow condition.

If the top value is 0 then stack is empty.If the top value is 0 then stack is empty.

Deleting element from the stack is known as underflow.Deleting element from the stack is known as underflow.

Takeout the element from the location where, the top is Takeout the element from the location where, the top is pointing and store it in the variable then decrement top by pointing and store it in the variable then decrement top by one.one.

Page 19: UNIT-I

Display of stack:Display of stack: Printing the contents of stack after push and pop Printing the contents of stack after push and pop

operations.operations.Algorithm print()Algorithm print()1. if top=-11. if top=-1

then write (‘stack empty’)then write (‘stack empty’) 2. Repeat for i 2. Repeat for i ← top to 0← top to 0

print(stack[i])print(stack[i])3. stop3. stop

Page 20: UNIT-I

Queue

A queue is like a line of A queue is like a line of people waiting for a people waiting for a bank teller. The queue bank teller. The queue has a has a frontfront and a and a rearrear..

$ $

FrontRear

Page 21: UNIT-I

The Queue Operations

New people must enter the queue New people must enter the queue at the rear. at the rear.

$ $

FrontRear

Page 22: UNIT-I

The Queue Operations

When an item is taken from the queue, it When an item is taken from the queue, it always comes from the front. always comes from the front.

$ $

FrontRear

Page 23: UNIT-I

A queue is special type of data structure in which insertions take A queue is special type of data structure in which insertions take place from one end called rear end and deletions take place from place from one end called rear end and deletions take place from other end called front end i.e insertions and deletions take place other end called front end i.e insertions and deletions take place from different ends.from different ends.

QueuesQueues

Page 24: UNIT-I

This type of data structure is used in time sharing This type of data structure is used in time sharing systems where many user jobs will be waiting in systems where many user jobs will be waiting in the system queue for processing. These jobs may the system queue for processing. These jobs may request the services of CPU, main memory or request the services of CPU, main memory or external devices such as printer.external devices such as printer.

A queue can be represented using sequential A queue can be represented using sequential allocation (using arrays or using Linked List). allocation (using arrays or using Linked List).

Page 25: UNIT-I

ABA

CBA

DCBA

DCBrear

front

rearfront

rear

front

rear

front

rear

front

First In First Out

Page 26: UNIT-I

Classification of queue:Classification of queue: Ordinary queue or Linear queue.Ordinary queue or Linear queue. Circular queue.Circular queue. Double ended queue.Double ended queue. Priority queue.Priority queue.

Page 27: UNIT-I

Basic operations on queue:Basic operations on queue:The operations that can be performed on queue areThe operations that can be performed on queue are InsertionInsertion DeletionDeletion DisplayDisplay

Queue maintains two pointers rear and front. Queue maintains two pointers rear and front. From rear end elements are inserted and from front From rear end elements are inserted and from front

elements are deleted.elements are deleted.

Page 28: UNIT-I

Working of a linear queue:-

i) Initially front=rear=-1. It indicates queue is empty.

0 1 2 3 4

front=rear=-1

0 1 2 3 4

ii) Add 10

10

front rear

0 2 3 4

iii) Add 20

front rear

1

10 20

0 2 3 4

iv) Add 30

front rear

110 20 30

0 2 3 4

v) Add 40

front rear

110 20 30 40

Page 29: UNIT-I

0 2 3 4

vi) Add 50

front rear

1

10 20 30 40 500 2 3 4

vii) Add 60 (overflow)

front rear

1

10 20 30 40 50

0 2 3 4

viii) delete (10 is removed)

front rear

1

20 30 40 500

front rear

1

30 40 50

ix) delete (20 is removed)

2 3 4

Page 30: UNIT-I

0

front rear

1

40 50

x) delete (30 is removed)

2 3 4 0

front rear

1

50

xi) delete (40 is removed)

2 3 4

0

front=rear=-1

1

ix) delete (underflow)

2 3 40

front=rear=-1

1

xii) delete (50 is removed)

2 3 4

Page 31: UNIT-I

Implementation of queue using Implementation of queue using arrayarrayAlgorithm add()Algorithm add()1.1. If rear If rear ≥ ≥ size-1size-1

then write (‘overflow’)then write (‘overflow’)2.2. Read itemRead item

3.3. rearrear←← rear + 1 rear + 13.3. queue[rear]queue[rear]←← item item5.5. stopstop

Page 32: UNIT-I

Explanation:Explanation: This procedure adds an element item to the queue.This procedure adds an element item to the queue.

First it checks for an overflow condition.First it checks for an overflow condition.

If the rear value reaches or exceeds size of the queue then If the rear value reaches or exceeds size of the queue then elements cannot be inserted into the queue. ie. Overflow.elements cannot be inserted into the queue. ie. Overflow.

Whenever element is inserted into the queue, rear is increment Whenever element is inserted into the queue, rear is increment by one and place the element in the location where rear is by one and place the element in the location where rear is pointing.pointing.

Page 33: UNIT-I

Algorithm to delete element Algorithm to delete element from the queuefrom the queueAlgorithm delete()Algorithm delete()1.1. If front= rearIf front= rear

then write (‘queue underflow’)then write (‘queue underflow’)item item ←← queue [front] queue [front]2. front 2. front ←← front + 1 front + 1

Page 34: UNIT-I

Explanation:Explanation: This procedure deletes an element from the queue.This procedure deletes an element from the queue.

The first step of this algorithm checks for underflow condition.The first step of this algorithm checks for underflow condition.

If the front value is 0 then queue is empty.If the front value is 0 then queue is empty.

Deleting element from the empty queue is known as underflow.Deleting element from the empty queue is known as underflow.

Take out the element from the location where, the front is Take out the element from the location where, the front is pointing and store it in the variable, then increment front by one. pointing and store it in the variable, then increment front by one.

Page 35: UNIT-I

Drawback in queue

-> in a queue when the rear pointer reaches to the end of the queue, insertion would be denied even if room is available at the front one way to remove this is using the circular queue

( circular array)

Page 36: UNIT-I

# include <stdio.h># define size 4int front=-1,rear=-1,item,choice,queue[size];void main(){ clrscr(); while(1) { printf("*** MENU ***\n 1. INSERTION\n 2. DELETION\n 3.TRAVERSE\n 4. EXIT\n"); printf("enter your choice:"); scanf("%d",&choice); switch(choice) { case 1: insertion(); break; case 2:deletion(); break; case 3:display(); break; case 4:exit(); default:printf("*** wrong choice ***\n"); } }}

Page 37: UNIT-I

void insertion(){ if(rear==size-1) printf("*** queue is full ***\n"); else { printf("enter item into queue:"); scanf("%d",&item); rear++; queue[rear]=item; if(front==-1) front=0; }}

void deletion(){ if(front==-1) printf("*** queue is empty ***\n"); else { item=queue[front]; printf("the deleted item from queue is %d\n",item); if(front==rear) front=rear=-1; else front++; }}

Page 38: UNIT-I

void display(){ int i; if(front==-1) printf("*** queue is empty ***\n"); else { for(i=front;i<=rear;i++) if(i==front && i==rear) printf("%d at %d ->front=rear\n",queue[i],i); else if(i==front) printf("%d at %d ->front\n",queue[i],i); else if(i==rear) printf("%d at %d ->rear\n",queue[i],i); else printf("%d at %d\n",queue[i],i);} }

Page 39: UNIT-I

EMPTY QUEUE[2] [3] [2] [3]

[1] [4] [1] [4]

[0] [5] [0] [5]

front = -1 front = 0 rear = -1 rear = 3

J2

J1

J3

Circular queues

Can be seen as a circular queue

Page 40: UNIT-I

How to test whether circular queue is empty or full? The circular q is controlled by the MOD operation. Circular queue is empty when front =-1 rear=-1Circular queue is full front = (rear+1)% length

Page 41: UNIT-I

Implementation of circular queue using array

void insertion(){if(front==(rear+1)%size)printf("*** queue is full ***\n");else{printf("enter item into queue:");scanf("%d",&item);rear=(rear+1)%size;queue[rear]=item; if(front==-1) front=0;}}

Page 42: UNIT-I

void deletion(){if(front==-1)printf("*** queue is empty ***\n");else{ item=queue[front]; printf("the deleted item from queue is %d\n",item); if(front==rear) front=rear=-1; else front=(front+1)%size;}}

Page 43: UNIT-I

example

1.encq(A)2.encq(B)3.encq(c )4. Encq ( D )5.Decq6.encq(E)7. Decq8.encq( F)9.Decq10. Decq11.Decq12. Decq

Page 44: UNIT-I

Applications of stacksApplications of stacks

Parenthesis matching.Parenthesis matching. Evaluation of postfix expressions.Evaluation of postfix expressions. Infix to prefix conversions.Infix to prefix conversions. Infix to postfix conversions.Infix to postfix conversions. Recursion.Recursion.

Page 45: UNIT-I

Parenthesis matchingParenthesis matching

The objective of this function is to check the The objective of this function is to check the matching of parenthesis in an expression matching of parenthesis in an expression

i.ei.e In an expression the no: of left parenthesis must be In an expression the no: of left parenthesis must be

equal to no: of right parenthesis.equal to no: of right parenthesis.

Ex:Ex: ((A+B)*C)((A+B)*C) This is a valid expression because in this no: of left This is a valid expression because in this no: of left

parenthesis (2) = no: of right parenthesis (2).parenthesis (2) = no: of right parenthesis (2).

Page 46: UNIT-I

Conversion of expressionsConversion of expressions

Arithmetic expressions can be represented inArithmetic expressions can be represented in

three ways:three ways: Infix notationInfix notation Prefix notationPrefix notation Postfix notationPostfix notation

Page 47: UNIT-I

Infix notation

general arithmetic expressions are represented with infix notation.

In which operator should be placed in between the two operands.

Example- A+B C-D E*F G/H

Page 48: UNIT-I

Prefix notation (polish notation) Operator preceded by the operand is called

prefix notation Another name is polish notation since it is

invented by the polish mathematician Jan Lukasiewicz.Examples +AB -CD *EF \GH.

Page 49: UNIT-I

Postfix notation

Operator should be placed after operands. Another names for postfix notation are

reverse polish notation and suffix notation. Examples

AB+ CD- EF* GH\

Page 50: UNIT-I

Conversion of infix to postfix

Algorithm1.Push “(“ on to stack and add “)” to end of infix.2. Scan infix from left to right and repeat steps 3 to 6 for each element of

infix until the stack is empty:3. If an operand is encountered , add it to P4.If a left parenthesis is encountered , push it onto stack5. If an operator X is encountered, then 5.1 repeatedly pop from stack and add to P each operator ( on the top of

stack) which has the same precedence as or higher precedence than X.5.2 add X to stack6. If a right parenthesis is encountered. Then : 6.1 repeatedly pop from stack and add to P each operator until a left

parenthesis is encountered 6.2 remove the left parenthesis.7. stop.

Page 51: UNIT-I

Note

1. the lower precedence operator never placed on top of the higher precedence.

Page 52: UNIT-I

Infix to post fix conversionInfix:-((A+B) ^C-(D*E)/F)Initial stack output 1 ( 2 (( 3 (( A 4 ((+ A 5 ( AB+ 6 (^ AB+ 7 (^ AB+C 8 (- AB+C ^ 9 (-( AB+C ^ 10 (-( AB+C ^D11 (-(* AB+C ^D 12 (-(* AB+C ^DE 13 (- AB+C ^DE* 14 (-/ AB+C ^DE* 15 (-/ AB+C ^DE*F 16 AB+C ^DE*F/-

Page 53: UNIT-I

Convert following infix expressions to postfix expression.

1. (A-B)*(D/E)

2. (A+B^D)/(E-F)+G

3. A*(B+D)/E-F*(G+H/K)

4. (A-B)/

Page 54: UNIT-I

Postfix expression evaluation

Algorithm1.Scan expression from left to right and repeat steps 2 and 3 for each

element of expression.2. If an operand is encountered, put it on stack.3.If an operator op1 is encountered then 3.1 remove the two top elements of stack, where A is the top and B is the

next top element. 3.2 evaluate Bop1 A 3.3 place the result back to stack.4. set the top value on stack.5. stop

Page 55: UNIT-I

Evaluate the expression

5,6,2,+,*,12,4,/,- symbol scanned stack1. 5 52. 6 5,63. 2 5,6,24. + 5,85. * 406. 12 40,127. 4 40,12,48. / 40,39. - 37