DS - Unit-1 - Large Font
DS - Unit-1 - Large Font
DATA STRUCTURES
Basically data are represented by data values held temporarily within a
program’s data area or recorded permanently on a file. So, the simple data is
represented by data type. Data structure is often confused with data types. Data
type is a collection of finite set of permitted values, and a set operations on those
values, whereas, Data structure is a study about the organizing the related data to
enable us to work efficiently with data, exploring the relationships within the
data. A data type can be defined as follows.
Data type = Permitted Data Values + Operations
Often the different data values are related to each other. To enable programs to
make use of these relationships, these data values must be in an organized form.
So the organized collection of data is called a data structure.
Data Structure = Organized Data + Allowed Operations
Definition:
Data may be organized in many different ways; the logical or mathematical
model of a particular organization of data is called a data structure. The data
structure can also be defined as methods to store the information in a computer.
Study of Data Structures study covers the following
» Amount of memory require to store
» Amount of time require to process
» Representation of data in memory
» Operations performs on data
Need of Data Structures:
As applications are getting complex and data rich, common problems that
applications face now-a-days are
» Data Search: As data grows, search will become slower.
» Processor speed: Processor speed although being very high, falls limited if the
data grows to billion records.
» Multiple requests: As thousands of users can search data simultaneously on a
web server, even the fast server fails while searching the data.
Department of Computer Science, SSBN Degree College, ATP
1
Data Structures
Primitive
Non- Primitive
Data Structures
Data Structures
In second case the linear relationship b/w the elements represented by means of
pointers or links. These are linked lists.
On a stack only two operations are performed they are insertion and deletion.
These are known as push and pop respectively.
Primitive Operations:
The two changes, which can be made to a stack, are given special name.
When an item is added to a stack, it is pushed onto the stack, and when an item
is removed, it is popped from the stack.
» Push: Add an item to a stack, moving the stack pointer (top) up to
accommodate the new item.
» Pop: Removes the most recently pushed item from the stack, moving the
stack pointer (top) down.
To use a stack efficiently we need to check status of stack as well. For the same
purpose, the following functionality is added to stacks −
» peek − get the top data element of the stack, without removing
it. » isFull − check if stack is full.
» isEmpty − check if stack is empty.
Example:
The below figure shows the general model that there is some element that is
at the top of the stack, and it is the only element that is visible.
Push Pop
G
Top
F
F
F
Top
E
E
E
Top
D
D
D
C
C
C
B
B
B
A
A
A
D top
C
B
main
Example 2: (A + B) * C
Symb Postfix string OpStack
( (
A A (
+ A (+
B AB (+
) AB+
* AB+ *
C AB + C *
AB + C*
( ((
A A ((
- A (( -
( A (( - (
B AB (( - (
+ AB (( - ( +
C ABC (( - ( +
) ABC + (( -
) ABC + - (
* ABC + - (*
D ABC + - D (*
) ABC + - D *
$ ABC + - D * $
( ABC + - D * $(
E ABC + - D * E $(
+ ABC + - D * E $(+
F ABC + - D *EF $(+
) ABC + - D *EF + $
ABC + -D*EF + $
A B C
Front
Rear
A B C D E
Front Rear
C D E
Front Rear
Conditions of a queue:
1. front: The front pointer is used to delete an element from queue
2. rear: Rear pointer is used to insert an element in the queue
3. if rear= Size and front=0 (Size is capacity of queue) then queue full
Representation of Queues:
Queues may be represented inside the memory mainly in 2 ways by means
of one way is linear arrays. When a queue is maintained by a linear array Q it
needs two pointer variables f and r to store the location of front element and to
store the location of rear element respectively.
When deleting an element f is incremented by 1 and whenever an element
is inserted r is incremented by 1. If Q size is n, after n insertions the near element
of the queue will occupy Q(n). This happens even though the queue itself may
not containing many elements i.e. a shown below.
If we want to insert an element into ‘Q’ front rear the queue at this stage i.e.
when n=r it is not possible. One way to overcome this is to simply move the entire
queue to the beginning of the array and changing front and rear. But this process
is very expensive so, the method is to assume queue as circular.
Algorithm to insert an element into the Queue:
Let Q is the given queue, front and rear be the pointer variables to indicate
the front and rear of the queue respectively. Size is the max capacity and item is
an element.
insert(Q, front, rear, item)
1. If rear>= Size -1
Print “Queue overflow”
Return.
2. increment rear = rear+1
3. insert the element Q[rear]=item
4. check front if queue is empty
if front =-1 then front=0
5. Exit
Department of Computer Science, SSBN Degree College, ATP
23
que[++rear]=item;
if(front==-1)
front++;
}
Department of Computer Science, SSBN Degree College, ATP
24
Operations on Queue:
» Insert or Enqueue : adding an element at the rear end of the Circular Queue.
» Delete or Dequeue: removing an element from the fronty end of the Circular
Queue.
Exceptions:
» Queue Overflow: Occurs when an insert operation is performed on a Circular
Queue which is already full
Condition: (front == 0 && rear == MAX-1 ) || (front == rear +1 ) » Queue
Underflow: Occurs when a delete operation is performed on an empty Circular
Queue.
Condition: front == -1
Note: when front=rear then queue has only one element
Algorithms to insert & delete an element into circular queue:
Let Q be a circular queue rear and front represent the rear and front ends of
queue. MAX is the capacity and item is the element to be inserted. CQinsert(Q,
front, rear, item)
1. check for overflow
If (front=0 and rear= Size -1 ) or front=rear+1 then
Print “Queue Overflow:
Return.
2. set rear
if front =-1 then
front=rear=0
Department of Computer Science, SSBN Degree College, ATP
28
}
Department of Computer Science, SSBN Degree College, ATP
32
pq.front is the position of the smallest element, pq.rear is 1 greater than the
position of the largest. Deletion involves merely increasing pq.front (for the
ascending queue) or decreasing pq.rear (for a descending queue). However,
insertion requires locating the proper position of the new element and shifting
the preceding or succeeding elements (again, the technique of shifting
whichever group is smaller is helpful). This method moves the work of searching
and shifting from the deletion operation to the insertion