DS UNIT 2 Part 2
DS UNIT 2 Part 2
KHADARSIR 9966648277
DATA STRUCTURE
Data structure is a method of organizing large amount of data more efficiently so
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.
push
Pop
top
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
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)
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
int data
struct node *next:
stacktop
100
NULL
25 200 60 30030 300| 40040 400
100 200
Vahues
A linked stack with some
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
if(rear size-1)
if(rear-1)
front=rear-0:
else
rear+
g[rear x;
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
) 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
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
glrear-x
Display "empty"
return;
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
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.
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+
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
15
KHADAR SIR
class B: public A
multilevel inheritance
Base class
Class-A
Class-B
Intermediate base class
class B: public A
class C: public B
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.
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
stu dernt
test sports
result
class sports
sports
class results publictest, public
Example:
class Base
void display0
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
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():
19
Nic KHADAR SIR 9966648277
void funA(0
void funC()
20