Data Structures Stack and Queues

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

function dequeue

ALGORITHM procedure dequeue if queue is empty return underflow end if data = queue[front] front ← front + 1 return true end procedure CODE int dequeue() { if(isempty()) return 0; int data = queue[front]; front = front + 1; return data; }

function enqueue

ALGORITHM procedure enqueue(data) if queue is full return overflow endif rear ← rear + 1 queue[rear] ← data return true end procedure CODE int enqueue(int data) if(isfull()) return 0; rear = rear + 1; queue[rear] = data; return 1; end procedure

IsFull

Checks if the queue is full. ALGORITHM begin procedure isfull if rear equals to MAXSIZE return true else return false endif end procedure CODE bool isfull() { if(rear == MAXSIZE - 1) return true; else return false; }

Queue

First in First out. Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to implement than that of stacks.

push

Places an object on the top of the stack. ALGORITHM begin procedure push: stack, data if stack is full return null endif top ← top + 1 stack[top] ← data end procedure CODE void push(int data) { if(!isFull()) { top = top + 1; stack[top] = data; } else { printf("Could not insert data, Stack is full.\n"); } }

pop

Removes an object from the top of the stack and produces that object. ALGORITHM begin procedure pop: stack if stack is empty return null endif data ← stack[top] top ← top - 1 return data end procedure CODE int pop(int data) { if(!isempty()) { data = stack[top]; top = top - 1; return data; } else { printf("Could not retrieve data, Stack is empty.\n"); } }

IsEmpty

Reports whether the stack is empty or not. ALGORITHM begin procedure isempty if top less than 1 return true else return false endif end procedure CODE bool isempty() { if(top == -1) return true; else return false; }

dequeue steps

Step 1 − Check if the queue is empty. Step 2 − If the queue is empty, produce underflow error and exit. Step 3 − If the queue is not empty, access the data where front is pointing. Step 4 − Increment front pointer to point to the next available data element. Step 5 − Return success.

Enqueue steps

Step 1 − Check if the queue is full. Step 2 − If the queue is full, produce overflow error and exit. Step 3 − If the queue is not full, increment rear pointer to point the next empty space. Step 4 − Add data element to the queue location, where the rear is pointing. Step 5 − return success.

Pop Steps

Step 1 − Checks if the stack is empty. Step 2 − If the stack is empty, produces an error and exit. Step 3 − If the stack is not empty, accesses the data element at which top is pointing. Step 4 − Decreases the value of top by 1. Step 5 − Returns success.

Push Steps

Step 1 − Checks if the stack is full. Step 2 − If the stack is full, produces an error and exit. Step 3 − If the stack is not full, increments top to point next empty space. Step 4 − Adds data element to the stack location, where top is pointing. Step 5 − Returns success.

peek

This function helps to see the data at the front of the queue/stack. ALGORITHM -

dequeue

dequeue() − remove (access) an item from the queue.

front

dequeue, front = front+1. Enqueue, front stays still.

enqueue

enqueue() − add (store) an item to the queue. ALGORITHM procedure enqueue(data) if queue is full return overflow endif rear ← rear + 1 queue[rear] ← data return true end procedure CODE int enqueue(int data) if(isfull()) return 0; rear = rear + 1; queue[rear] = data; return 1; end procedure

queueSize

function size() { return queueSize; }

Stack

last-in first-out. Push = put item in the first slot. Pop, remove the last item inserted.

top_of_stack

observes the top-most element without removing it from the stack.

MAX_SIZE

size of stack/queue

rear

whenever dequeue, rear stays still. whenever enqueue, rear = rear+1.


Kaugnay na mga set ng pag-aaral

GMAT Quantitative Problems - Incorrect

View Set

Chapter 12: Permutations and Combinations

View Set

Marketing Applications Final Exam Study Guide

View Set

BADM 310: Exam 2 (Chapter 9 Multiple Choice Questions)

View Set

ATI MedSurg Endocrine Practice Test

View Set

Contemporary Social Problem Chapter 1

View Set

RMI 4224- Test 1, RMI 4224 test 2, Schriefer Exam 3, Schriefer Exam 3

View Set

Intro to Sociology Ch. 13 Multiple Choice

View Set