Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT-I Topics to be covere d 1.Introduction to data structures.

Similar presentations


Presentation on theme: "UNIT-I Topics to be covere d 1.Introduction to data structures."— Presentation transcript:

1 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

2 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

3 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.

4 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.

5 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 Non Linear ds Linear ds Graphs Trees Arrays Linked list stack queues

6 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.

7 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.

8 3. Stacks and queues Stores a set of elements in a particular order
What is a stack? 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 Which is the first element to pick up?

9 Stack maintains a pointer called top, which keeps track of the top most element in the stack.
Any insertions or deletions should be based upon the value of top. It works on the basis of LIFO (Last in First out). According to the definition, new elements are inserted from top and the elements are deleted from same end i.e again top. This suggests that the element inserted most recently can only be deleted. 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 inserted is to be deleted first. So it is called last in first out.

10 Ex: Elements are inserted in the order as A,B,C,D.
It represents the stack of 4 elements. The top most element in the stack is E. If we want to delete element only D has to be deleted first. If deletion operation is performed on stack continuously then the order in which the elements are deleted is D,C,B,A.

11 Last In First Out D top C top C C top top B top B B B B A A A top A A

12 Basic operations: The basic operations are insertion, deletion and display. In stacks, special terms are given for insert and delete. i.e push for insert and pop is for delete. Push: inserting or adding element into the stack is called push. Pop: deleting or removing element from the stack is called pop.

13 3 3 3 2 2 2 1 1 TOP-> 1 20 TOP-> 10 10 TOP=-1 (Empty stack) Push 10 Push 20 3 TOP-> 3 40 TOP-> 3 40 TOP-> 2 30 2 30 2 30 20 20 20 1 1 1 10 10 10 Push 30 Push 40 Push 50 (Overflow)

14 3 3 TOP-> 2 30 2 1 20 TOP-> 1 20 10 10 pop pop 3 3 2 2 1 1 TOP-> 10 pop (TOP=-1) underflow pop

15 Implementing stack using arrays
Algorithm for inserting element into the stack: Algorithm push() 1. if top=(size-1) then write (‘stack overflow’) Return 2. read item 3. top←top+1 4. stack[top]← item 5. stop

16 Explanation: 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 stack. The first step of this algorithm checks for an overflow condition. Overflow means inserting element into a stack which is full. If the top value reaches to maximum size of the stack then elements cannot be inserted into the stack i.e. stack is full. Otherwise top is incremented by one and element is inserted into the stack.

17 Algorithm to delete element from the stack:
Algorithm pop() 1. if top=-1 then write (‘stack underflow’) 2. item ← stack[top] 3. top ← top-1

18 Explanation: This procedure deletes an element from the stack. The first step of this algorithm checks for underflow condition. If the top value is 0 then stack is empty. Deleting element from the stack is known as underflow. Takeout the element from the location where, the top is pointing and store it in the variable then decrement top by one.

19 Display of stack: Printing the contents of stack after push and pop operations. Algorithm print() 1. if top=-1 then write (‘stack empty’) 2. Repeat for i ← top to 0 print(stack[i]) 3. stop

20 Queue A queue is like a line of people waiting for a bank teller. The queue has a front and a rear. $ $ When you think of a computer science queue, you can imagine a line of people waiting for a teller in a bank. The line has a front (the next person to be served) and a rear (the last person to arrive. Front Rear

21 The Queue Operations New people must enter the queue at the rear.
$ $ Don’t ask me why the C++ STL used the name push. It only confuses matters with a stack. In any case, when a new item enters a queue, it does so at the rear. Front Rear

22 The Queue Operations Front Rear
When an item is taken from the queue, it always comes from the front. $ $ When an item is removed from a queue, the removal occurs at the front. Front Rear

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

24 This type of data structure is used in time sharing systems where many user jobs will be waiting in the system queue for processing. These jobs may request the services of CPU, main memory or external devices such as printer. A queue can be represented using sequential allocation (using arrays or using Linked List).

25 First In First Out rear front D C B A B A C B A D C B A rear front

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

27 Basic operations on queue:
The operations that can be performed on queue are Insertion Deletion Display Queue maintains two pointers rear and front. From rear end elements are inserted and from front elements are deleted.

28 Working of a linear queue:-
i) Initially front=rear=-1. It indicates queue is empty. 1 2 3 4 front=rear=-1 ii) Add 10 iii) Add 20 1 2 3 4 1 2 3 4 10 20 10 front rear front rear iv) Add 30 v) Add 40 1 2 3 4 1 2 3 4 10 20 30 10 20 30 40 front rear front rear

29 vi) Add 50 vii) Add 60 (overflow) 1 2 3 4 1 2 3 4 10 20 30 40 50 10 20 30 40 50 front rear front rear viii) delete (10 is removed) ix) delete (20 is removed) 1 2 3 4 1 2 3 4 20 30 40 50 30 40 50 front rear front rear

30 x) delete (30 is removed) xi) delete (40 is removed) 1 2 3 4 1 2 3 4 40 50 50 front rear front rear xii) delete (50 is removed) ix) delete (underflow) 1 2 3 4 1 2 3 4 front=rear=-1 front=rear=-1

31 Implementation of queue using array
Algorithm add() 1. If rear ≥ size-1 then write (‘overflow’) Read item 3. rear← rear + 1 3. queue[rear]← item 5. stop

32 Explanation: This procedure adds an element item to the queue. First it checks for an overflow condition. If the rear value reaches or exceeds size of the queue then elements cannot be inserted into the queue. ie. Overflow. Whenever element is inserted into the queue, rear is increment by one and place the element in the location where rear is pointing.

33 Algorithm to delete element from the queue
Algorithm delete() If front= rear then write (‘queue underflow’) item ← queue [front] 2. front ← front + 1

34 Explanation: This procedure deletes an element from the queue. The first step of this algorithm checks for underflow condition. If the front value is 0 then queue is empty. Deleting element from the empty queue is known as underflow. Take out the element from the location where, the front is pointing and store it in the variable, then increment front by one.

35 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)

36 # include <stdio.h>
# define size 4 int 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"); }

37 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; front++; }

38 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); if(i==front) printf("%d at %d ->front\n",queue[i],i); if(i==rear) printf("%d at %d ->rear\n",queue[i],i); printf("%d at %d\n",queue[i],i); }

39 Circular queues EMPTY QUEUE Can be seen as a circular queue [2]
[3] [2] [3] J2 J3 [1] [4] [1] [4] J1 [0] [5] [0] [5] front = front = 0 rear = rear = 3 Can be seen as a circular queue

40 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=-1 Circular queue is full front = (rear+1)% length

41 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; }

42 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; front=(front+1)%size; }

43 example 1.encq(A) 2.encq(B) 3.encq(c ) 4. Encq ( D ) 5.Decq 6.encq(E)
8.encq( F) 9.Decq 10. Decq 11.Decq 12. Decq

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

45 Parenthesis matching The objective of this function is to check the matching of parenthesis in an expression i.e In an expression the no: of left parenthesis must be equal to no: of right parenthesis. Ex: ((A+B)*C) This is a valid expression because in this no: of left parenthesis (2) = no: of right parenthesis (2).

46 Conversion of expressions
Arithmetic expressions can be represented in three ways: Infix notation Prefix notation Postfix notation

47 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

48 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.

49 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\

50 Conversion of infix to postfix
Algorithm 1.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 P 4.If a left parenthesis is encountered , push it onto stack 5. 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 stack 6. 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.

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

52 Infix to post fix conversion
Infix:-((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 ^D 11 (-(* AB+C ^D 12 (-(* AB+C ^DE 13 (- AB+C ^DE* 14 (-/ AB+C ^DE* 15 (-/ AB+C ^DE*F AB+C ^DE*F/-

53 Convert following infix expressions to postfix expression.
(A-B)*(D/E) (A+B^D)/(E-F)+G A*(B+D)/E-F*(G+H/K) (A-B)/

54 Postfix expression evaluation
Algorithm 1.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

55 Evaluate the expression
5,6,2,+,*,12,4,/,- symbol scanned stack ,6 ,6,2 ,8 * ,12 ,12,4 / ,3


Download ppt "UNIT-I Topics to be covere d 1.Introduction to data structures."

Similar presentations


Ads by Google