Stacks queues

50
Stacks and Queues

Transcript of Stacks queues

Stacks and Queues

Prepared by Teddy Yap, Jr. 01062003 2

Stacks

• Dynamic sets in which the elementremoved from the set by the DELETEoperation is pre-specified.

• The element deleted from the set is theone most recently inserted.

• Implements a last-in, first-out (LIFO)policy.

Prepared by Teddy Yap, Jr. 01062003 3

Stacks (cont’d.)

• An ordered list in which all insertions anddeletions are made at one end called theTOP.

• Only the top element is accessible.• E.g. a stack/pile of books, a spring-loaded

stacks of plates in cafeterias.

Prepared by Teddy Yap, Jr. 01062003 4

Stacks (cont’d.)

Operations1. CREATE(STACK) – creates an empty stack.2. PUSH(STACK, ITEM) – inserts an element

item into the stack (INSERT operation).3. POP(STACK) – removes and then returns the

top element of the stack (DELETE operation).4. TOP(STACK) – returns the top element of the

stack.

Prepared by Teddy Yap, Jr. 01062003 5

Stacks (cont’d.)

Operations5. STACK_EMPTY(STACK) – determines

whether the stack is empty or not.a. If TOP = 0, stack is empty.

6. STACK_FULL(STACK) – determines whetherthe stack is full or not.a. If TOP = n, stack is full.

Prepared by Teddy Yap, Jr. 01062003 6

Stacks (cont’d.)

Note: TOP is used to index the most recentlyinserted element in the stack. The element atindex 1 is the bottom of the stack while theelement at index TOP is at the top of the stack.

This NOTE is just our convention when discussingstacks in class so that we will know what is the

bottom or top element.

Prepared by Teddy Yap, Jr. 01062003 7

Stacks (cont’d.)

Representations1. One-Dimensional Array

2. Singly Linked-List

8 9 0 13 99 -7 -1 ? ……

1 2 3 4 5 6 7 8

TOP

8 9 0 13 99 -7 -1

TOP

Prepared by Teddy Yap, Jr. 01062003 8

Stacks (cont’d.)

Declaration (Using Arrays)#define n <positive integer constant>

typedef <data type> elementType

typedef elementType Stack[n];

Prepared by Teddy Yap, Jr. 01062003 9

Stacks (cont’d.)

Declaration (Using Singly Linked-List)typedef <data type> elementType;

struct nodeTag {

elementType element;

struct nodeTag *link;

};

typedef struct nodeTag nodeType;

typedef nodeType *pointerType;

pointerType TOP = NULL;

Prepared by Teddy Yap, Jr. 01062003 10

Stacks (cont’d.)

Simulation1. CREATE(S) will produce

with the value of TOP = 0.

1 2 3 4 5 6 7 8

? ? ? ? ? ? ? ?

TOP

S

Prepared by Teddy Yap, Jr. 01062003 11

Stacks (cont’d.)

Simulation2. Stack S after STACK_EMPTY(S).

STACK_EMPTY(S) will return true since the stackis indeed empty (no elements).

1 2 3 4 5 6 7 8

? ? ? ? ? ? ? ?

TOP

S

Prepared by Teddy Yap, Jr. 01062003 12

Stacks (cont’d.)

Simulation3. Stack S after PUSH(S, 8), PUSH(S, 12),

PUSH(S, -7), and then PUSH(S, 1).

The top element is 1.

1 2 3 4 5 6 7 8

8 12 -7 1 ? ? ? ?

TOP

S

Prepared by Teddy Yap, Jr. 01062003 13

Stacks (cont’d.)

Simulation4. Stack S after PUSH(S, 3), PUSH(S, 17).

The new top element is 17.

1 2 3 4 5 6 7 8

8 12 -7 1 3 17 ? ?

TOP

S

Prepared by Teddy Yap, Jr. 01062003 14

Stacks (cont’d.)

Simulation5. Stack S after POP(S).

POP(S) will give us 17, which is the most recently pushedelement. Although 17 is still in the array, it is no longerin the stack and the new top element is 3.

1 2 3 4 5 6 7 8

8 12 -7 1 3 17 ? ?

TOP

S

Prepared by Teddy Yap, Jr. 01062003 15

Stacks (cont’d.)

Simulation6. Stack S after TOP(S).

TOP(S) will give us 3, the top element of the stack.It will not delete 3 from the stack.

1 2 3 4 5 6 7 8

8 12 -7 1 3 17 ? ?

TOP

S

Prepared by Teddy Yap, Jr. 01062003 16

Stacks (cont’d.)

Simulation7. Stack S after STACK_EMPTY(S).

STACK_EMPTY(S) will return false since the stackis not empty.

1 2 3 4 5 6 7 8

8 12 -7 1 3 17 ? ?

TOP

S

Prepared by Teddy Yap, Jr. 01062003 17

Stacks (cont’d.)

Possible Errors• Underflow – occurs when an empty stack is

popped.• Overflow – occurs when pushing an element to

a fully-filled stack (i.e. TOP > n).

Prepared by Teddy Yap, Jr. 01062003 18

Stacks (cont’d.)

Operations DefinedCREATE(S)

TOP = 0;

STACK_EMPTY(S)if (TOP == 0)

return 1; /* true */

else

return 0; /* false */

Prepared by Teddy Yap, Jr. 01062003 19

Stacks (cont’d.)

Operations DefinedSTACK_FULL(S)

if (TOP == n)

return 1; /* true */

else

return 0; /* false */

Prepared by Teddy Yap, Jr. 01062003 20

Stacks (cont’d.)

Operations DefinedPUSH(S, x)

if (STACK_FULL(S))

printf(“Overflow Error!\n”);

else {

TOP = TOP + 1;

S[TOP] = x;

}

Prepared by Teddy Yap, Jr. 01062003 21

Stacks (cont’d.)

Operations DefinedPOP(S)

if (STACK_EMPTY(S))

printf(“Underflow Error!\n”);

else {

x = S[TOP];

TOP = TOP – 1;

return x;

}

Prepared by Teddy Yap, Jr. 01062003 22

Stacks (cont’d.)

Operations DefinedTOP(S)

if (STACK_EMPTY(S))

printf(“Stack Empty!\n”);

else

return S[TOP];

Prepared by Teddy Yap, Jr. 01062003 23

Stacks (cont’d.)

Did you know that…Computing machines use stacks to evaluate the

value of a given expression (as in C).

a + b * c – d / e % f * (g – h + i)

a b c * + d e / f % g h – i + * -

From infix notation topostfix form of the expression

Then use a stack to evaluatethe expression in postfix form

Prepared by Teddy Yap, Jr. 01062003 24

Queues

• Dynamic sets in which the elementremoved from the set by the DELETEoperation is pre-specified.

• The element deleted is always the onethat has been in the set for the longesttime.

• Implements a first-in, first-out (FIFO)policy.

Prepared by Teddy Yap, Jr. 01062003 25

Queues (cont’d.)

• An ordered list in which all insertions takeplace at one end, called the TAIL, while alldeletions take place at the other end,called the HEAD.

• Elements are processed in the same orderas they were received. The first elementinserted in the queue will be the first oneto be removed.

Prepared by Teddy Yap, Jr. 01062003 26

Queues (cont’d.)

Operations1. CREATE(QUEUE) – creates an empty queue.2. ENQUEUE(QUEUE, ITEM) – inserts an

element into the queue (INSERT operation).3. DEQUEUE(QUEUE) – removes and then

returns the head of the queue (DELETEoperation).

Prepared by Teddy Yap, Jr. 01062003 27

Queues (cont’d.)

Operations4. QUEUE_HEAD(QUEUE) – determines the

element at the head of the queue.5. QUEUE_TAIL(QUEUE) – determines the

element at the tail of the queue.

Prepared by Teddy Yap, Jr. 01062003 28

Queues (cont’d.)

Operations6. QUEUE_EMPTY(QUEUE) – determines if the

queue is empty or not.a. If HEAD = TAIL, queue is empty.

7. QUEUE_FULL(QUEUE) – determines if thequeue is full or not.a. If HEAD = (TAIL + 1) % n, queue is full.

Prepared by Teddy Yap, Jr. 01062003 29

Queues (cont’d.)

Note: HEAD is used to index or point to the headof the queue. TAIL is used to index or point tothe next location at which a newly arrivingelement will be inserted into the queue.Therefore, the elements of a given queue Qare Q[HEAD], Q[HEAD + 1], Q[HEAD + 2], …,Q[TAIL – 1]. For circular queues, location 1immediately follows location n.

Prepared by Teddy Yap, Jr. 01062003 30

Queues (cont’d.)

Representations1. One-dimensional array

2. Singly linked-list

8 9 0 13 99 -7 -1 ? ……

1 2 3 4 5 6 7 8

TAILHEAD

8 9 0 13 99 -7 -1

TAILHEAD

Prepared by Teddy Yap, Jr. 01062003 31

Queues (cont’d.)

Declaration (Using Arrays)#define n <positive integer constant>

typedef <data type> elementType

typedef elementType Queue[n];

Prepared by Teddy Yap, Jr. 01062003 32

Queues (cont’d.)

Declaration (Using Singly Linked-List)typedef <data type> elementType;

struct nodeTag {

elementType element;

struct nodeTag *link;

};

typedef struct nodeTag nodeType;

typedef nodeType *pointerType;

pointerType HEAD = TAIL = NULL;

Prepared by Teddy Yap, Jr. 01062003 33

Queues (cont’d.)

Simulation1. CREATE(Q) will produce

with the value of HEAD = 1 and TAIL = 1.

1 2 3 4 5 6 7 8

? ? ? ? ? ? ? ?

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 34

Queues (cont’d.)

Simulation2. Queue Q after QUEUE_EMPTY(Q) and

QUEUE_FULL(Q).

QUEUE_EMPTY(S) will return true since thequeue is indeed empty (no elements) whileQUEUE_FULL will return false.

1 2 3 4 5 6 7 8

? ? ? ? ? ? ? ?

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 35

Queues (cont’d.)

Simulation3. Queue Q after ENQUEUE(Q, 8), ENQUEUE(Q,

12), ENQUEUE(Q, -7), and then ENQUEUE(Q, 1).

The first element is 8 and the last element is 1.

1 2 3 4 5 6 7 8

8 12 -7 1 ? ? ? ?

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 36

Queues (cont’d.)

Simulation4. Queue Q after DEQUEUE(Q).

The first element is now 12. Though 8 is still in thearray, it is not anymore part of the queue.

1 2 3 4 5 6 7 8

8 12 -7 1 ? ? ? ?

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 37

Queues (cont’d.)

Simulation5. Queue Q after ENQUEUE(Q, 3), ENQUEUE(Q,

17), ENQUEUE(Q, 20).

The first element is 12 and the last element is 20.

1 2 3 4 5 6 7 8

8 12 -7 1 3 17 20 ?

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 38

Queues (cont’d.)

Simulation6. Queue Q after QUEUE_EMPTY(Q) and

QUEUE_FULL(Q).

QUEUE_EMPTY(Q) and QUEUE_FULL(Q) willboth return false.

1 2 3 4 5 6 7 8

8 12 -7 1 3 17 20 ?

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 39

Queues (cont’d.)

Simulation7. Queue Q after ENQUEUE(Q, 55).

The first element is 12 and the last element is 55.

1 2 3 4 5 6 7 8

8 12 -7 1 3 17 20 55

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 40

Queues (cont’d.)

Simulation8. Queue Q after QUEUE_EMPTY(Q) and

QUEUE_FULL(Q).

QUEUE_EMPTY will return false whileQUEUE_FULL will return true.

1 2 3 4 5 6 7 8

8 12 -7 1 3 17 20 55

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 41

Queues (cont’d.)

Simulation9. Queue Q after DEQUEUE(Q) and

DEQUEUE(Q).

The first element is 1 and the last element is 55.

1 2 3 4 5 6 7 8

8 12 -7 1 3 17 20 55

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 42

Queues (cont’d.)

Simulation10. Queue Q after ENQUEUE(Q, 16) and

ENQUEUE(Q, 18).

The first element is 1 and the last element is 18.

1 2 3 4 5 6 7 8

16 18 -7 1 3 17 20 55

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 43

Queues (cont’d.)

Simulation11. Queue Q after DEQUEUE(Q) x 7.

The queue is empty and there is no first nor lastelements.

1 2 3 4 5 6 7 8

16 18 -7 1 3 17 20 55

TAIL

Q

HEAD

Prepared by Teddy Yap, Jr. 01062003 44

Queues (cont’d.)

Note: Our queue is full if it contains n – 1elements. Therefore, a queue cancontain at most n – 1 elements.

Prepared by Teddy Yap, Jr. 01062003 45

Queues (cont’d.)

Possible Errors• Underflow – occurs when an empty queue is

being dequeued.• Overflow – occurs when enqueueing an

element to a queue that is full.

Prepared by Teddy Yap, Jr. 01062003 46

Queues (cont’d.)

Operations DefinedCREATE(Q)

HEAD = TAIL = 1;

QUEUE_EMPTY(Q)if (HEAD == TAIL)

return 1; /* true */

else

return 0; /* false */

Prepared by Teddy Yap, Jr. 01062003 47

Queues (cont’d.)

Operations DefinedQUEUE_FULL(Q)

if (HEAD == ( TAIL + 1) % n)

return 1; /* true */

else

return 0; /* false */

Prepared by Teddy Yap, Jr. 01062003 48

Queues (cont’d.)

Operations DefinedENQUEUE(Q, x)

if (QUEUE_FULL(Q))

printf(“Overflow Error!\n”);

else {

Q[TAIL] = x;

if (TAIL == n)

TAIL = 1;

else

TAIL = TAIL + 1;

}

Prepared by Teddy Yap, Jr. 01062003 49

Queues (cont’d.)

Operations DefinedDEQUEUE(Q)

if (QUEUE_EMPTY(Q))

printf(“Underflow Error!\n”);

else {

x = Q[HEAD];

if (HEAD == n)

HEAD = 1;

else

HEAD = HEAD + 1;

return x;

}

Prepared by Teddy Yap, Jr. 01062003 50

Queues (cont’d.)

Did you know that…Queues are used by line printers in a computer

system. In a computer system, jobs are sentto the printer queue. The first job sent to theprinter queue will be processed first while thelast job sent to the printer queue will beprocessed last.