0% found this document useful (0 votes)
14 views20 pages

DS UNIT 2 Part 2

Data structures are methods for organizing large amounts of data efficiently, which can be categorized into linear and non-linear types. Operations such as traversal, search, insertion, deletion, and sorting can be performed on data structures, with static and dynamic types differing in size flexibility. Stacks and queues are specific linear data structures with distinct operations and applications, including expression evaluation and resource management.

Uploaded by

zeeshan0000010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views20 pages

DS UNIT 2 Part 2

Data structures are methods for organizing large amounts of data efficiently, which can be categorized into linear and non-linear types. Operations such as traversal, search, insertion, deletion, and sorting can be performed on data structures, with static and dynamic types differing in size flexibility. Stacks and queues are specific linear data structures with distinct operations and applications, including expression evaluation and resource management.

Uploaded by

zeeshan0000010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIT 2 SAMCEP

KHADARSIR 9966648277

DATA STRUCTURE
Data structure is a method of organizing large amount of data more efficiently so

that any operation on that data becomes easy.


Whenever we want to work with large amount of data, then organizing that data

is very important. If that data is not organized effectively, it is very difficult ro


perform any task on that data. If it is organized effectively then any operation
can be performed easily on that data.
TYPES OF DATA STRUCTURE
Based on the organizing method of a data structure, data structures are divided

into two types.


i. Linear Data Structures
i. Non-Linear Data Structures
Linear Data structure:
If a data structure is organizing the data in sequential order, then that data
structure is called as Linear Data Structure.
Examples:
i. Linked List
ii. Stack
ii. Queue
Non-Linear Data Structures
If a data structure is organizing the data not in sequential order, then that data
structure is called as Non-Linear Data Structure.
Examples:
i. Tree
i. Graph
ii. Dictionaries
iv. Heaps
DATA STRUCTURE OPERATIONs
We can perform the following operations on data structure
i. Traversal : Visit every part of the data structure
ii. Search : Traversal through the data structure for a given element
i. Insertion: Adding new elements to the data structure
iv. Deletion: Removing an element from the data structure.
V. Sorting: Rearranging the elements in some type of order(e.g Increasing or
Decreasing)
STATIC DATA STRUCTURE AND DYNAMIC DATA STRUCTURE: In Static
data structure the size of the structure is fixed. This is convenient when we

1
KHADAR SIR699666
6
stored in advance. in
in static d
of elements that need to be
know the number
elements.
structure we use array to store
structure the size of
the strucfure is not fixed, d. Th
This is
In Dynamic data
need to be storod
know the number of elements that in
convenient when we not
we use linked list
to store elements
advance. in Dynamic data structure
Linked list representation
Array representation vs
to store data of similar types, but thev
Both Arrays and Linked List can be used
over each other.
both have some advantages and disadvantages
Advantages of linked Ilist compare to array:
size is not fixed, we can create
The size of the arrays is fixed. But in linked list
the memory dynamically.
has to because
array of elements is expensive,
in room
Insertinga new element an
be created for the new elements and to create room existin9 elements have to
shifted.
Deleting aelement from an array is expensive, because everything after the
deleting element has to be moved.
So Linked list provides following two advantages over arrays:
1) Dynamic size
2) Ease of insertion/deletion
Disadvantages Linked lists:
1) Random access is not allowed. We have to elements sequentially starting
access
from the first node. So we cannot do
binary search with linked lists.
2) Extra memory space for a pointer is required with each element of the list.
3 )Reverse Traversing is not possible in single linked list.

STACK or STACK ADT


A stack isa linear data structure
in which insertion and deletion take place from
the same end. This end is called as
"top" of the stack. The insertion operation is
called as "push" and the deletion
operation is called as "pop". Stack is also called
LIFO data structure because only we can delete the
element which is inserted
last.

push
Pop

top

Stack with some values

2
KHADAR SIR 9966648277

plications of Stack:
Stacks can be used for
expression evaluation.
. Stacks can be used to check
ii. Stacks can be used for parenthesis matching in an expression
Conversion from one form of
iv. Stacks can be used for Towers of Hanoi expression to another.
v.Stack data structures are used in problem.
backtracking problems
vi. Stack data structures are used in Tree
traversal problems

Examples of stacks in "daily life"


The stack of trays in a cafeteria;
A stack of plates in a cupboard;

StacADT operations: the following operations(Functions) and data(Variables)


are used in stack.
data members:
top' variable used to stores top element index.
s[] this array used for stores element of stack:
operations or functions:
empty() -

return true if stack is empty: false otherwise


sizel): of elements in the stack
return the number

top(): return the top element of the stack


-

pop(:- remove the topelement from the stack.


push(x): inserts 'x at the top of the stack
-

Stack implementation:
Stack can implement using an Array or a Linked List.
Stack implementation using Array:
A stack data structure can be implemented using one dimensional array. But stack
values. This
implemented using array, can store only fixed number of data size
implementation is very simple, just define a one dimensional array specific
of
or delete the values into that array by using
LIFO principle with the
and insert
help of a variable 'top'.
Memory representation of stack using array:

25 32 15
top 3
Algorithms for push variable STOPg

locations. "top" be
value to
PUSH: with "size" x"is the
"s" is an array -1 and
Description:
Here
initial value
of top is
most
element,
index of top
inserted.

PUSH(X)

if (top = size -1)

Print "Overflow":

Return
fop top+1:

s[top]=x

POP: variable it
locations. "top"is a integer
Here 's" is an array with "size"
Description: element.
recent element means top
stores the index of

POPO

if (top -1)

Print "Underflow"
return 0:

x= s[top]
top-top-1
returnx

Stack Implementation using linked list:


elements and
Linked stack representation involves node's, which consists of data
a pointer to the next node. The new
nodes are added at the beginning off the
stack and the nodes are deleted from the beginning of the stack. in this
implementation one "structure" is required for Node:
4
KHADAR SIR 9966648277
sentation:

ructure definition for node:


truct node

int data
struct node *next:

Memory representation of node is:


Node
30 2000
Data next
field stores the data and 'next field
In the above node representation 'data'
stores the address of the next node. That
That
variable, for example "stacktop", is used.
Another external pointer the last
first node in the stack. The link field of
contains the address of the of a
indicate the end of the stack. The example
node containsNULL value to
linked stack is given as
of linked stack:
Memory representation

stacktop
100
NULL
25 200 60 30030 300| 40040 400
100 200
Vahues
A linked stack with some

QUEUE or QUEUE ADT in which the element is inserted from


one end
data structure,
Queue is a linear from the
called tail), and the deletion of element takes place
called REAR(also data
called head). This makes queue as FIFO
other end called as FRONT(also
removed first.
which means that element inserted first will also be
structure, the process of
to add an element into queue is called Enqueue and
The process
removal of an element from queue is called Dequeue.
Applications of Queue:
In real life the queue is used in
the following
shared resource, like a printer, CPU task
1. Serving requests on a single
scheduling etc.
to hold people calling them in an
2. Call Center phone systems will use Queues(
order, until a service representative is free.)
3. In daily life, We wait in queues to buy pizza, to enter movie theaters. 5
KHADAR SIR 996

Operations: the following operations(Functions) and data(Va iabla


QueueADT
are
used in queue.
data members:
index.
front' variable used to stores front element
element index.
rear variable used to stores rear
element of queue:
9ll this array used for stores
operations or functions:
return true if queue empty; is false otherwise
empty(): -

elements in the queue


size(): return the number of
-

from the queue.


dequeue(): remove the front element
-

the queue
enqueue(x): -inserts'x' at the rear of
display(): display the queue elements
-

Queue implementation:
Linked List.
Queue also can implement using an Array or a
queue implementation using array:this
implementation stores the queue
is shown in
elements in an array. Memory representation of queue using array
below figure

40 50 10 25

front rear
a queue with 4 elements

Algorithms for queue operations:


Algorithm for insert:
locations. "front" and "rear" are two
Description: Here "q" is an array with "size" index
index of first element and rear stores
integer variables "front" stores the
of rear element.
ENQUEUE(x)

if(rear size-1)

Display "Queue is full"


return
6
KHADAR SIR 9966648277

if(rear-1)
front=rear-0:
else
rear+

g[rear x;

Algorithm for delete:


DEQUEUE0
{
if(front -1)
Display "empty
return:

x=q[front
print x
if front== rear then
front =rear -1;
else
front++

}
involves
Queue Implementation using linked list: Linked queue representation
consists of data elements and a pointer to the next node. The new
node's, which
deleted from the
nodes are added at the end of the queue and the nodes are
is required for
beginning of the queue. in this implementation one "structure"
Node:
structure definition for node:
struct node

int element:
KHADAR SIR 996

struct node *next:

) of node is:
representation
Memory
Node

30 2000
element next data and 'next
field stores the
representation
'element
In the above node
address of the next node. are used. That
field stores the "front" and "rear",
for example the queue. The
fwo pointer variables,
external
nodes respectively in
last
of the first and end of the
contains the address
NULL value to indicate the
node contains
link field of the last as:
The example of a linked stack is given
queue.
of linked stack:
Memory representation

Rear
Front

then
the last position of the queue occupied
is
Limitations of Array Queue: If
even though some
more elements in the queue
it is not possible to enqueue any
front position of the queue for example-
positions are vacant towards the
40 50 10 25

front rear

are still blank in the


according to the rear position queue is full but 5 positions
queue.
CIRCULAR QUEUE
in which if the rear end reaches the
A circular queue is a special form of queue
inserted at the front side if it is free.
maximum limit then the new elements are
Circular queues can be represented by arrays or linked representation.
insertion
representation of circular queue: In array representation, the
Array that if the rear end
and deletion operations are same as ordinary queue, except
end is moved to front end if it is free.
reaches the maximum limit, then the rear
inserted are limited, that is the size of the
The number of elements that can be
circular queue is fixed. But the difference is
that the queue is not said to be full

8
KHADAR SIR 9966648277
I the positions of the queue are filled. The memory representation of a
ar queue isas in the fig below:
ogically memory representation of circular queue is shown below:
n front

45 21

ear

Algorithm for circular queue operations:


Algorithm for insert:
Enqueue(x)
if (rear+1 % size =: front)

Display Queue is full"


return
if(rear -1)
front=rear:0
else
rear (rear+1) % size;

glrear-x

Algorithm for delete:


Dequeue)
if front = -1

Display "empty"
return;

if front =rear then


front =rear =-1
else
front (front+1)%size

linked representation of
of circular queue The a
Linked representation of ordinary queue, except
as the linked representation
circular queue is same
the points to the first node. The first node is
that the last node in queue
node called head node. The memory representation
pointed to by a special
circular linked list is as follows:

rear
front

10 17 18

DOUBLE ENDED QUEUE


special form of queue in which insertion and deletion
A double ended queue is a
is possible from both the rear and front end. It is called as De-queue.
or linked list.
A double-ended queue can be represented using either arrays
Double ended queues are again divided into two types. They are input
restricted and output restricted double ended queue.
In output restricted queue, the deletion operation is only from the front end
whereas insertion is allowed from both the ends.

cONVERSION INFIX TOPOSTFIXx


An infix expression is the one in which the operator is present in between The
operands. for example a*b
postfix expression is the one in which the operator is present after the
operands. For example, ab (which means a+b).
A stack is suited very well to convert a infix expression to postfix expression.
For processor to convert from infix to postfix is as follows:

Processor or Algorithm to convert Infix to Postfix:


1. The infix expression is scanned from left to right And repeat step 2 to 6 for
each element of infix expression until the STACK is empty.
2.If an operand is encountered add it to Postfix.
3.If a left parenthesis is encountered push it onto the STACK.
10
KHADAR SIR 9966648277
Scanned character is an operator and stack is empty. push operaro
perators stack

If the operator' s stack is not


empty, there may be following possibilities.
a If the precedence of scanned operator is greater than the Top ost

operator of stack, push this operator into stack


b.if the equal to the
precedence of scanned operator is less than or
most operator of stack, Pop all the operators which are of higher or e
Top
al
precedence then the scanned operator and append them to the Postfix

txpression. After popping out all such operators, push the scanned
operator in to the stack.
6.If a right parenthesis is encountered, then
a.Repeatedly pop from the STACK and add to Postfix each operator
until a left parenthesis is encountered.
b. Remove the left parenthesis; do not add it to Postfix.
end is reached, then all the stack elements
are
7. If the infix
expressions
popped and append to the postfix expression.

Algorithm(Second Method) to convert infix to postfix: ,.


till nexttoken(infix) : NULL
1. repeat through 2 to 6
2.X nexttoken(infix):
3. if X is operand, Then
append X to postfix
4.if X = "( then

push(X)
5.if x is operator fhen
repeat while top l -1 and priority(stack[top]) =priority(X)
temp pop(0
append temp to postfix;
end while:
pushX):
end if
6.if X ") then
repeat while ((temp = pop())=()

temp popO
append temp to postfix;
end while
end if
11
KHADAR SIR 99
7.repeat while top -1
temp pop ()
append temp to postfix;
end while
8. display postfix;

Examples
Question Convert Following infix Expression Into Postfix using stack
Infix Expression asbc/d-f+a"e
Infix to Postfix conversion:
scanned symbol stack
Postfix

B
ab
ab
C
abc
abc*
D +/ abc*d
| abc*d/+
abcd/+f
A abc"d/+f-
abc d/+f-a
E abcd/f-a
End abc"d/+f-ae
abc*d/+f-ae
Example2: Infix to Postfix Conversion
Infix Expression: (A/(B-CyD»E)

Raenb poT pr m
KHADAR SIR 9966648277
mbol Scanned Stack Output

A
(/ A
B AB
AB
C
ABC
ABC-
ABC-/
ABC-/D
ABC-/D
E ABC-/DE
(
Empty ABC-/D'E+

Some of the examples for infix expressions and equivalent postfix


expressions
at(b+c) abc++
(a+b)+c ab+
a-b'c abc
(a/b)"(c/d) ab/cd
a/(b+cd-e) abcd+e-
a-b'c+d/e abc-de+
POSTFIX EXPRESSION EVALUATION
A postfix expression is the one in which the operator is present after the
operands. For example, ab+ (which means a+b). is also called reverse polish
notation
Processor or Algorithm for evaluating a postfix expression
1. Start
2. The postfix expression is scanned from left to right and the following actions
are taken depending on the types of the token scanned. And repeat step 3 for
each element of postfix expression.
3. for each character 'ch' of 'postfix expression
if(ch' is an operand)
push ch' in stack
else

n2 pop an operand from 'stack'


ni pop an operand from 'stack'

13
+
N w O+

N w
N
KHADAR SIR 9966648277

"base-class-
"derived-class-name" is derived from
that the
on( ) indicates and can be either public (or) private.
The
thevisibility mode is optional
ault visibility mode is private.
xample:
class ABC: private XY2 l/private derivation

//members of ABC

//public derivation
XY2
class ABC: public

//members of ABC

public members
inherited by a derived class,
When a base class is privately
members of derived class.
of the base class become private members of
inherited by a derived class, public
When a base class is publicly
members of derived class.
the base class become public
not inherited.
in both the cases, private members are
Anyhow,

TYPES OF INHERITANCE
1) Single inheritance:
inheritance". The
base class is called as "single
only one
A derived class with
of base class along with its
own
derived class will have all the properties
members.
Class-A

Class- B

Class A is called as base class


Class B is called as derived class
follows
A derived class declared as

class A

15
KHADAR SIR

class B: public A

class from a derived class is called ne


as
2) Multilevel inheritance: Deriving
a

multilevel inheritance

Base class
Class-A

Class-B
Intermediate base class

Class-C Derived class

Class A is called as base class


Class B is called as intermediate base class because it provides a link
between A and C
Class C is called as derived class
.The chain ABC is called as inheritance path
A derived class with
multilevel inheritance is declared as follows
class A

class B: public A

class C: public B

This process can be extended to any level


3) Multiple inheritance:
KHADAR SIR 9966648277

A class can inherit the properties of two or more classes. This is called as
multiple inheritance. The derived class then contains all properties of base
classes and its own members.

The syntax of derived class with multiple base classes is

class class-name: visibility Base1,visibility Base2. ....

I/body of class

The visibility can be either public or private. The base classes are separated by
commas
4) Hybrid inheritance: when more than one type of inheritance is applied or

used, then it is called as hybrid inheritance.

stu dernt

test sports

result

The general form for above relationship will be like this:


class student

classtest: public student


17
KRADAN SIR 99

class sports

sports
class results publictest, public

FUNCTION OVER RIDDING:


with the same name and
contains a function
derived class
When a base class and a the base class function.
This
function hides
parameters then
the derived class the
derived class redefines
It means that the
is called as function overriding.
base class function.

Example:
class Base

void display0

cout"\n Base class":

classDerived public Base

void display0

base::display():
cout" Derived class":

void main(0

clrscr(
Derived D
18
D.display() KHADAR SIR 9966648277

getch():

SUBTYPING:

Derived class is a
subtype of base class if every function that
the base
can be accessed by
object can also be accessed by the derived
object
Examplel:
class Base

public:
void funA(0
void funB(0

class Derived public Base

public:
void funC0
void funD(0

In the above example Derived class is a subtype of Base class. because all
methods of base class by derived class
can access
object.
Example2:
class Base

public:
void funAO:
void funB():

class Derived: public Base

19
Nic KHADAR SIR 9966648277

void funA(0
void funC()

In the example2 derived class is only subclass not


funA() of Base class is executed when we call by basesubtyping of Base. Because
class object. funA() of
Derived class is executed when we call by derived class
object.

20

You might also like