stacks n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Stacks PowerPoint Presentation
Download Presentation
Stacks

Loading in 2 Seconds...

play fullscreen
1 / 46

Stacks - PowerPoint PPT Presentation


  • 143 Views
  • Uploaded on

Stacks. CS 3358 – Data Structures. What is a stack?. It is an ordered group of homogeneous items of elements. Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack).

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Stacks' - avani


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
stacks

Stacks

CS 3358 – Data Structures

what is a stack
What is a stack?
  • It is an ordered group of homogeneous items of elements.
  • Elements are added to and removed from the top of the stack (the mostrecently added items are at the top of the stack).
  • The last element to be added is the first to be removed (LIFO: Last In, First Out).
stack specification
Stack Specification
  • Definitions: (provided by the user)
    • MAX_ITEMS: Max number of items that might be on the stack
    • ItemType: Data type of the items on the stack
  • Operations
    • makeEmpty
    • bool isEmpty
    • bool isFull
    • push (ItemType newItem)
    • pop ()
push itemtype newitem
push (ItemType newItem)
  • Function: Adds newItem to the top of the stack.
  • Preconditions: Stack has been initialized and is not full.
  • Postconditions: newItem is at the top of the stack.
itemtype pop
ItemType pop ()
  • Function: Removes topItem from stack and returns it.
  • Preconditions: Stack has been initialized and is not empty.
  • Postconditions: Top element has been removed from stack and itemis a copy of the removed element.
slide7

Stack overflow

  • The condition resulting from trying to push an element onto a full stack.

if(!stack.isFull())

stack.push(item);

Stack underflow

  • The condition resulting from trying to pop an empty stack.

if(!stack.isEmpty())

stack.pop();

example convert number bases
Example: Convert number bases
  • Use the stack to remember
    • Let’s convert 3510 to binary
example postfix expressions
Example: Postfix expressions
  • Postfix notation is another way of writing arithmetic expressions.
  • In postfix notation, the operator is written after the two operands.

infix: 2+5 postfix: 2 5 +

  • Expressions are evaluated from left to right.
  • Precedence rules and parentheses are never needed!!
postfix expressions algorithm using stacks
Postfix expressions:Algorithm using stacks

WHILE more input items exist

Get an item

IF item is an operand

stack.Push(item)

ELSE

stack.Pop(operand2)

stack.Pop(operand1)

Compute result

stack.Push(result)

stack.Pop(result)

the n queens problem
The N-Queens Problem
  • Suppose you have 8 chess queens...
  • ...and a chess board
the n queens problem1
The N-Queens Problem

Can the queens be placed on the board so that no two queens are attacking each other

?

the n queens problem2
The N-Queens Problem

Two queens are not allowed in the same row...

the n queens problem3
The N-Queens Problem

Two queens are not allowed in the same row, or in the same column...

the n queens problem4
The N-Queens Problem

Two queens are not allowed in the same row, or in the same column, or along the same diagonal.

the n queens problem5
The N-Queens Problem

The number of queens, and the size of the board can vary.

N Queens

N columns

N rows

the n queens problem6
The N-Queens Problem

We will write a program which tries to find a way to place N queens on an N x N chess board.

how the program works
How the program works

The program uses a stack to keep track of where each queen is placed.

how the program works1

ROW 1, COL 1

How the program works

Each time the program decides to place a queen on the board, the position of the new queen is stored in a record which is placed in the stack.

how the program works2

ROW 1, COL 1

1

How the program works

We also have an integer variable to keep track of how many rows have been filled so far.

filled

how the program works3

ROW 2, COL 1

ROW 1, COL 1

1

How the program works

Each time we try to place a new queen in the next row, we start by placing the queen in the first column...

filled

how the program works4

ROW 2, COL 2

ROW 1, COL 1

1

How the program works

...if there is a conflict with another queen, then we shift the new queen to the next column.

filled

how the program works5

ROW 2, COL 3

ROW 1, COL 1

1

How the program works

If another conflict occurs, the queen is shifted rightward again.

filled

how the program works6

ROW 2, COL 3

ROW 1, COL 1

2

How the program works

When there are no conflicts, we stop and add one to the value of filled.

filled

how the program works7

ROW 3, COL 1

ROW 2, COL 3

ROW 1, COL 1

2

How the program works

Let's look at the third row. The first position we try has a conflict...

filled

how the program works8

ROW 3, COL 2

ROW 2, COL 3

ROW 1, COL 1

2

How the program works

...so we shift to column 2. But another conflict arises...

filled

how the program works9

ROW 3, COL 3

ROW 2, COL 3

ROW 1, COL 1

2

How the program works

...and we shift to the third column.

Yet another conflict arises...

filled

how the program works10

ROW 3, COL 4

ROW 2, COL 3

ROW 1, COL 1

2

How the program works

...and we shift to column 4. There's still a conflict in column 4, so we try to shift rightward again...

filled

how the program works11

ROW 3, COL 4

ROW 2, COL 3

ROW 1, COL 1

2

How the program works

...but there's nowhere else to go.

filled

how the program works12

ROW 2, COL 3

ROW 1, COL 1

1

How the program works

When we run out of

room in a row:

  • pop the stack,
  • reduce filled by 1
  • and continue working on the previous row.

filled

how the program works13

ROW 2, COL 4

ROW 1, COL 1

1

How the program works

Now we continue working on row 2, shifting the queen to the right.

filled

how the program works14

ROW 2, COL 4

ROW 1, COL 1

2

How the program works

This position has no conflicts, so we can increase filled by 1, and move to row 3.

filled

how the program works15

ROW 3, COL 1

ROW 2, COL 4

ROW 1, COL 1

2

How the program works

In row 3, we start again at the first column.

filled

pseudocode for n queens
Pseudocode for N-Queens
  • Initialize a stack where we can keep track of our decisions.
  • Place the first queen, pushing its position onto the stack and setting filled to 0.
  • repeat these steps
    • if there are no conflicts with the queens...
    • else if there is a conflict and there is room to shift the current queen rightward...
    • else if there is a conflict and there is no room to shift the current queen rightward...
pseudocode for n queens1

Increase filled by 1. If filled is now N, then

the algorithm is done. Otherwise, move to

the next row and place a queen in the

first column.

Pseudocode for N-Queens
  • repeat these steps
    • if there are no conflicts with the queens...
pseudocode for n queens2

Move the current queen rightward,

adjusting the record on top of the stack

to indicate the new position.

Pseudocode for N-Queens
  • repeat these steps
    • if there are no conflicts with the queens...
    • else if there is a conflict and there is room to shift the current queen rightward...
pseudocode for n queens3

Backtrack!

Keep popping the stack, and reducing filled

by 1, until you reach a row where the queen

can be shifted rightward. Shift this queen right.

Pseudocode for N-Queens
  • repeat these steps
    • if there are no conflicts with the queens...
    • else if there is a conflict and there is room to shift the current queen rightward...
    • else if there is a conflict and there is no room to shift the current queen rightward...
pseudocode for n queens4

Backtrack!

Keep popping the stack, and reducing filled

by 1, until you reach a row where the queen

can be shifted rightward. Shift this queen right.

Pseudocode for N-Queens
  • repeat these steps
    • if there are no conflicts with the queens...
    • else if there is a conflict and there is room to shift the current queen rightward...
    • else if there is a conflict and there is no room to shift the current queen rightward...
summary of n queens
Summary of N-Queens
  • Stacks have many applications.
  • The application which we have shown is called backtracking.
  • The key to backtracking: Each choice is recorded in a stack.
  • When you run out of choices for the current decision, you pop the stack, and continue trying different choices for the previous decision.
example using templates
Example using templates

// Client code

StackType<int> myStack;

StackType<float> yourStack;

StackType<StrType> anotherStack;

myStack.Push(35);

yourStack.Push(584.39);

implementing stacks using templates
Implementing stacks using templates

template<class ItemType>

class StackType {

public:

StackType();

void makeEmpty();

boolisEmpty() const;

boolisFull() const;

void push(const ItemType &);

ItemType pop();

private:

int top;

ItemType items[MAX_ITEMS];

};

implementing stacks using templates1
Implementing stacks using templates
  • Templates allow the compiler to generate multiple versions of a class type or a function by allowing parameterized types.
  • It is similar to passing a parameter to a function (we pass a data type to a class !!)
implementing stacks using dynamic array allocation
Implementing stacks using dynamic array allocation

template<class ItemType>

class StackType {

public:

StackType(int);

~StackType();

void makeEmpty();

bool isEmpty() const;

bool isFull() const;

void push(const ItemType &);

ItemType pop();

private:

int top;

int maxStack;

ItemType *items;

};

let s implement a simple stack
Let’s implement a simple stack
  • Using arrays as memory
    • Stacks grow “up”
    • Make sure to test copy constructor!
  • Using a linked structure
    • Always insert at head