stacks (data structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · stacks (data...

47
Stacks (Data Structure)

Upload: others

Post on 13-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

Page 2: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• A stack is a special type of structure that can be used to maintain data in an organized way.

• This data structure is commonly implemented in one of two ways: as an array or as a linked list.

• In either case, the important rule is that when data is added to the stack, it sits “on top,” and so if an element needs to be removed, the most recently added element is the only element that can legally be removed.• Last in, first out (LIFO)

Page 3: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• There are only two operations that may legally be performed on a stack.

• Push: Add a new element to the top of the stack.

• Pop: Remove the most recently-added element from the top of the stack.

Page 4: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

typedef struct _stack{

VALUE array[CAPACITY];int top;

}stack;

Page 5: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

typedef struct _stack{

VALUE array[CAPACITY];int top;

}stack;

Page 6: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

typedef struct _stack{

VALUE array[CAPACITY];int top;

}stack;

Page 7: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

typedef struct _stack{

VALUE array[CAPACITY];int top;

}stack;

Page 8: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

stack s;

Page 9: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

stack s;

s

Page 10: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

stack s;s.top = 0;

s

Page 11: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

stack s;s.top = 0;

0s

Page 12: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation• Push: Add a new element to the top of the stack.

In the general case, push() needs to:• Accept a pointer to the stack.

• Accept data of type VALUE to be added to the stack.

• Add that data to the stack at the top of the stack.

• Change the location of the top of the stack.

Page 13: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

void push(stack* s, VALUE data);

0s

Page 14: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

stack s;s.top = 0;push(&s, 28);

0s

Page 15: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

stack s;s.top = 0;push(&s, 28);

28

0s

Page 16: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

stack s;s.top = 0;push(&s, 28);

28

1s

Page 17: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

push(&s, 33);

28

1s

Page 18: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

push(&s, 33);

28 33

1s

Page 19: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

push(&s, 33);

28 33

2s

Page 20: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

push(&s, 19);

28 33

2s

Page 21: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

push(&s, 19);

28 33 19

2s

Page 22: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

push(&s, 19);

28 33 19

3s

Page 23: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation• Pop: Remove the most recent element from the top of the stack.

In the general case, pop() needs to:• Accept a pointer to the stack.

• Change the location of the top of the stack.

• Return the value that was removed from the stack.

Page 24: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

VALUE pop(stack* s);

28 33 19

3s

Page 25: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

int x = pop(&s);

28 33 19

3s

Page 26: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

int x = pop(&s);

28 33

3s

19

x

Page 27: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

int x = pop(&s);

28 33 19

3s

19

x

Page 28: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

int x = pop(&s);

28 33 19

2s

19

x

Page 29: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

int x = pop(&s);

28 33 19

2s

19

x

Page 30: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

int x = pop(&s);

28 33 19

2s

33

x

Page 31: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

int x = pop(&s);

28 33 19

1s

33

x

Page 32: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

push(&s, 40);

28 33 19

1s

Page 33: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

push(&s, 40);

28 40 19

1s

Page 34: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Array-based implementation

push(&s, 40);

28 40 19

2s

Page 35: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Linked list-based implementation

typedef struct _stack{

VALUE val;struct _stack *next;

}stack;

Page 36: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• Just make sure to always maintain a pointer to the head of the linked list!

• To push, dynamically allocate a new node, set its next pointer to point to the current head of the list, then move the head pointer to the newly-created node.

Page 37: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

list = push(list, 12);

15 9 13 10

list

Page 38: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

list = push(list, 12);

15 9 13 10

list

new

Page 39: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

list = push(list, 12);

12 15 9 13 10

list

new

Page 40: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

list = push(list, 12);

12 15 9 13 10

list

new

Page 41: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

list = push(list, 12);

12 15 9 13 10

list

new

Page 42: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

• To pop, traverse the linked list to its second element (if it exists), free the head of the list, then move the head pointer to the (former) second element.

Page 43: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

pop(list);

12 15 9 13 10

list

Page 44: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

pop(list);

12 15 9 13 10

list

trav

Page 45: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

pop(list);

12 15 9 13 10

list

trav

Page 46: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

pop(list);

15 9 13 10

list

trav

Page 47: Stacks (Data Structure)cdn.cs50.net/2015/fall/sections/7/stacks/stacks.pdf · Stacks (Data Structure) •A stack is a special type of structure that can be used to maintain data in

Stacks (Data Structure)

pop(list);

15 9 13 10

list

trav