Docsity

CS 331: Stacks and Queues - Lecture Notes - Prof. Alan Mattox Beckman, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CS 331 — Stacks and Queues Februrary 18, 2009
1 Introduction and Objectives
2 Introduction
2.1 Objectives
2.2 Objectives
•Be able to describe the stack operations.
•Describe two stack implementations (array and linked).
•Be able to describe the queue operations.
•Describe four queue implementations
–Array
–Linked with no end pointer
–Linked with end pointer
–Linked with two lists (amortized).
•Know how long each operation takes in each implementation.
•Vocabulary: FIFO, LIFO, Amortized Cost
3 Stacks
•What are the stack operations?
–Add things by placing them on the top. “Pushing”
–Remove things by taking them off the top. “Popping”
–Look at the top of the stack.
Stacks are called LIFO for this reason.
•How long do they take? O(1). Always.
•Idea: if you know how to write a list, you know how to write a stack.
3.1 Array Stacks
•Consider the following sequence of operations:
push 2
push 3
push 5
push 8
pop
pop
pop
pop
Mattox Beckman Page 1 Illinois Institute of Technology
pf3
pf4
pf5

Partial preview of the text

Download CS 331: Stacks and Queues - Lecture Notes - Prof. Alan Mattox Beckman and more Study notes Data Structures and Algorithms in PDF only on Docsity!

1 Introduction and Objectives

2 Introduction

2.1 Objectives

2.2 Objectives

  • Be able to describe the stack operations.
  • Describe two stack implementations (array and linked).
  • Be able to describe the queue operations.
  • Describe four queue implementations
    • Array
    • Linked with no end pointer
    • Linked with end pointer
    • Linked with two lists (amortized).
  • Know how long each operation takes in each implementation.
  • Vocabulary: FIFO, LIFO, Amortized Cost

3 Stacks

  • What are the stack operations?
    • Add things by placing them on the top. “Pushing”
    • Remove things by taking them off the top. “Popping”
    • Look at the top of the stack. Stacks are called LIFO for this reason.
  • How long do they take? O(1). Always.
  • Idea: if you know how to write a list, you know how to write a stack.

3.1 Array Stacks

  • Consider the following sequence of operations: push 2 push 3 push 5 push 8 pop pop pop pop
  • The stack pointer is the first empty space in the array!
  • What should the class definition look like?

3.2 Stack Class

1 public class Stack { 2 int[] data; 3 int top; 4 5 public Stack() { 6 data = new int[100]; 7 } 8 9 public Stack(int size) { 10 data = new int[size]; 11 } 12 }

  • Now write the push, pop, and top methods.

3.3 Linked Stacks

3.4 Using a Linked List

  • We can use linked lists for a stack. Why would we want to do that, instead of using an array?
  • Where should new items be placed?
  • Consider the following sequence of operations:

4 Queues

  • What are the queue operations?
    • Add things by placing them at the back. “Enqueue”
    • Remove things by taking them off the front. “Dequeue”
    • Look at the front of the queue.

Queues are called FIFO for this reason.

  • How long do they take? O(1). Always.
  • Idea: if you know how to write a list, you know how to write a queue. Only it’s a bit more complicated now.

4.1 Array Queues

  • Consider the following sequence of operations:

enqueue 2 enqueue 3 enqueue 5 enqueue 8 dequeue dequeue enqueue 13 enqueue 21 dequeue enqueue 34

4.2 Queue Class

1 public class Queue { 2 int[] data; 3 int front; 4 int back; 5

6 // and add constructor here... 7 }

  • Try writing enqueue and dequeue.

4.3 Linked Queues

Consider this sequence. enqueue 2 enqueue 3 enqueue 5 enqueue 8 dequeue dequeue enqueue 13 enqueue 21 dequeue enqueue 34

  • What structure is appropriate for the following models?
    • Exploring a maze with only one entrance
    • A dictionary
    • A line at a theater
    • A telephone support center
    • A shopping cart for an online retailer
    • An AI system that breaks a goal into subgoals