Professional Documents
Culture Documents
Stack
-
.
.
( )BOTTOM
(.)TOP
TOP
)Last In First Out( LIFO .
24
TOP= 6
31
61
16
55
17
BOTTOM
TOP= -1
:Stack
-
.
. TOP
.
Top
1- .top =-1
ADT Stack
Data
Operations
Constructor stack
Initial values:
Process:
dtypeStack empty()
Input:
Preconditions:
Process:
Output:
Post conditions:
None
Initialize the top of the stack
None
None
Check whether the stack is
empty
Return true if stack is empty
and false otherwise
none
ADT Stack
Operations
dtype Pop(void)
Input:
Preconditions:
Process:
Output:
Post conditions:
void Push(dtype)
Input:
Preconditions:
Process:
Output:
Post conditions:
None
Stack is not empty
Remove the item from the top of the stack
Return the element from the top of the stack
Element at the top of the stack is removed
ADT Stack
Operations
dtypePeek(void)
Input:
Preconditions:
Process:
Output:
Post conditions:
void Clearstack(void)
Input:
Preconditions:
Process:
Output:
Post conditions:
None
Stack is not empty
Retrieve the value of the item on the top
Return the value of the item from the top
The stack is unchanged
None
None
Delete all the item of the stack and reset the top
None
The stack is reset to the initial conditions
ADT Stack
Operations
dtypeStackfull()
Input:
None
Preconditions:
None
Process:
Output:
Post conditions:
None
Stack class
:
.
Empty_stack
Full_ stack
)POP(
.)Push(
.1
.2
.3
.4
.5
# include <iostream .h>
# include <conio .h>
const int max_stack_size=4;
class stack
{
private:
int stack_list[max_stack_size-1];
int top;
public:
Stack();
{
Top== -1;
}
};
stack()
Top =-1
) ( empty stack
Top 1-
)True(1
)false( 0
10
># include <iostream .h
># include <conio .h
()POP
;const int max_stack_size=4
) ( pop
{class stack
Top 1-
private:
(
;]int stack_list[max_stack_size-1
) :
;int top
.1
.2 top
public:
.3
{ )(Int pop
)if (top==-1
{cout < < \nattemp to pop an empty
;stack
};)Exit(1
else
;]{int item = stack_list[top
;top--
}};return item
11
) ( int full_ stack
Top 1-
)True(1
)false( 0
12
)Push(
push(int item )
Top
max_stack_size
) (
top
13
peek()
dtype peek()
Top
14
.1 C++
.2 C++
15
)( void main
;{ stack s,s1,temp
;int I
))(while (!s.empty_stack
{
;)(i =s.pop
;)temp .push(i
}
{))(while (!temp.empty_stack
;)(i= temp.pop
;)s1 .push(i
};)s.push(i
}
Queue
Front Rear
)First In First Out( FIFO .rear
16
front
= () Rear=size
size=rear = 6
61
size= 6
16
Rear=4
55
front=2
17
61
16
55
17
17
Size=6
front=0
-
> =0
-
0
Rear=front=0
= = 0
0
90 65
54
38
88
90 65
54
01 2
11
9
3
8 765 4
12
38
88
77
18
> 13 >6 =0
0
01 2
11
9
3
8 765 4
22
12
77
33
99
44
55
66
= 13 =13 = 0
front = rear+1
ADT Queue
Data
A list of item
Rear:a position that references the first
item in the queue
front:a position that references the last
item in the queue
19
ADT Queue
Operations
Constructor queue
Initial values:
Process:
None
Initialize the front,rear and nitem of
the queue
dtype Qlength(void)
Input:
Preconditions:
Process:
None
Output:
Post conditions:
none
None
Determine the number of items in
the queue
20
ADT queue
Operations
dtype Qempty(void)
Input:
Preconditions:
Process:
Output:
Post conditions:
void Qinsert(dtype)
Input:
Preconditions:
Process:
Output:
Post conditions:
None
None
Check whether the queue is empty
Return 1 is empty 0 otherwise
None
ADT queue
Operations
dtypedelete(void)
Input:
None
Preconditions:
Process:
Output:
Post conditions:
void Clearqueue(void)
Input:
Preconditions:
Process:
Output:
None
Post conditions:
22
Queue class
:
.1
.2
.3
.4
.5
.6
.7
.8
23
# include <iostream .h>
# include <conio .h>
const int max_queue_size=10;
class queue
{
private:
Dtype
queue_list[max_queue_size];
int rear,front,nitem;
public:
queue();
{
Rear ==0;
Front ==0;
Nitem==0;}
};
queue()
:
Rear=0 , front =0 ,nitem=0
24
empty queue( )
0 nitem
) True(1
) false( 0
25
27
full queue( )
nitem
max_queue_size
) True(1
) false( 0
28
# include <iostream .h>
# include <conio .h>
const int max_queue_size=10;
class queue
{
private:
Dtype
queue_list[max_queue_size];
int rear ,front,nitem;
public:
Int Qlength()
{return nitem;}
};
Qlength( )
29
Qfront()
30
Priority Queue
:
( )
-
:
23
rear
12
front 3
15 12 23 :
.1 rear
.2
.3 .
rear
31
23
15
12
front 3
(+)
( )
32
.1 C++
33