You are on page 1of 33

Stacks

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

A list of item with a position


Top = indicates the top of the stack

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

An item for the stack

Stack is not full


Store the item on the top of the stack
None
The stack has a new element at the top

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:

Check whether the stack is full

Output:

Return true if stack is full else false

Post conditions:

None

Stack class
:

# 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();
int empty_stack();
int pop();
void push(int);
int full_stack();
};

.
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
;const int max_stack_size=4
class stack
{
private:
;]int stack_list[max_stack_size-1
;int top
public:
)(Int empty_Stack
{
;return Top== -1
}
;}


># 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

># 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:
;)(Int empty_Stack
{
;return Top== -1
}
;}

# 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:
void push(int item);
{
if (top== max_stack_size-1)
{cout < < \nstack is full
overflow/n;
Exit(1);}
++ top;
Stack_list [top] = item;}


)Push(
push(int item )

Top
max_stack_size
) (
top

13

# 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:
int peek ();
{
if (top== -1)
{cout < < /nstack is empty/n;
Exit(1);
}
Return Stack_list [top];
}


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

nitem:the number of entries in the


queue at given time

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:

Return the number of items in the


queue

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

An item to store in the queue


Queue is not full
Store the item on the rear of the queue
None
a new item is added to the queue
21

ADT queue
Operations
dtypedelete(void)
Input:

None

Preconditions:
Process:
Output:
Post conditions:
void Clearqueue(void)
Input:
Preconditions:
Process:

Stack is not empty

Output:

None

Post conditions:

The queue is empty

Remove an item from the front of the queue


Return the item that is removed from the queue

An item is deleted from the queue


None
None
revomed all the item from the queue and reset
the initial conditions

22

Queue class
:

# include <iostream .h>


.
# include <conio .h>
Empty_queue
const int max_queue_size=10;
Full_ queue
class queue
)Qdelete(
{
)Qinsert(
private:
(Qlength)
dtype queue_list[max_queue_size];
)Qclear(
Dtype rear,front,nitem;
)Qfront(
public:
queue();
dtype empty_queue();
Dtype Qdelete();
Void Qinsert(dtype);
int full_queue();
};

.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

# 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 empty_queue();
{if(nitem==0)
{
Cout<<queue is empty;
}
else
{Cout<<queue is not
empty;}};



empty queue( )

0 nitem
) True(1
) false( 0

25

# include <iostream .h>

# include <conio .h>



const int max_queue_size=10;
class queue{
Qdelete( )
private:
0= nitem
Dtype queue_list[max_queue_size];
:
int rear ,front,nitem;
public:
1 nitem Dtype Qqueue();
front = (front+1)%max_queue _ { dtype item;
size
if(nitem;==0){
Cout<<queue is empty;}
else{
item = queue_list[front];
nitem--;
Front=(front+1)%max_queue_size;
return item;}};
26

# include <iostream .h>



# include <conio .h>
Qinsert
const int max_queue_size=10;
class queue{
Qinsert(dtype item)
private:
nitem
Dtype queue_list[max_queue_size];
max_queue_size
nitem
int rear ,front,nitem;
rear=
public:
( rear+1)%max_queue_size
Void Qinsert(dtype item);

{if(nitem== max_queue_size)
{Cout<<queue is full;}
else
{ nitem++;
Queue_list[rear]=item;
Rear = (rear+1)%max_queue_size;}};

27

# 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:
dtype full_queue();
{if(nitem== max_queue_size)
{
Cout<<queue is full;
}
else
{Cout<<queue is not full;}};



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

# 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 Qfront();
{if(nitem==0)
{
Cout<<queue is empty;
}
else
{return queue_list[front];}};



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

You might also like