Unit-I DS
Unit-I DS
Topics to be covered 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
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 tasks1.storage representation of user data 2. Retrieval of stored data 3. transformation of user data
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.
Data structure is implemented around the concept of an abstract data type that defines the both data organization and data hiding 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.
Data structure
Linear ds Arrays Linked list stack queues Non Linear ds Trees Graphs
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.
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.
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?
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.
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.
C B A top B A
top
D C B A
top
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.
3
2 1 TOP-> 0 10 Push 10
3 2 TOP-> 1 0 20 10 Push 20
TOP-> 3 2 1 0
40 30 20 10 Push 40
TOP-> 3 2 1 0
40 30 20 10 Push 50 (Overflow)
3
TOP-> 2 1 0 30 20 10
3 2 TOP-> 1 0 20 10 pop 3 2
pop
3 2 1 TOP-> 0 10 pop
1 0
pop (TOP=-1) underflow
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.
Algorithm to delete element from the stack: Algorithm pop() 1. if top=0 then write (stack underflow) 2. item stack[top] 3. top top-1
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
Queue
A queue is like a line of people waiting for a bank teller. The queue has a front and a rear.
$ $
Front Rear
Front
Rear
When an item is taken from the queue, it always comes from the front.
$ $
Front
Rear
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. 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.
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).
rear front
B A
D C front B
rear front
Classification of queue: Ordinary queue or Linear queue. Circular queue. Double ended queue. Priority queue.
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.
front
rear
v) Add 40
0 10
1 2 3 20 30
0 10
2 3 20 30 40
front
rear
front
rear
vi) Add 50 0 1 10 20 2 3 30 40 4 50
front
rear
front
rear
front
rear
front
rear
front rear
front rear
front=rear=-1
front=rear=-1
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.
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)
Circular queues
EMPTY QUEUE
[2] [1] [3] [4] [1] J1 [2] J2 J3 [3] [4]
[0]
[5]
[0]
[5]
front = 0 rear = 0
front = 0 rear = 3
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 =0 rear=0 Circular queue is full front = (rear+1)% length
Algorithm for deletion 1.if front=0 print queue is empty 2. else 2.1 item=cq [front] 2.2 if front=rear front=0,rear=0; 3. Else 3.1 front=(front+1)length 4. stop