Você está na página 1de 21

Data Structures

DS Lecture 06 Stack

Dr. Kazi A Kalpoma


Associate Professor
Faculty of Science and Information Technology

American International University Bangladesh (AIUB)


Contact: kalpoma@gmail.com

3/9/2014

Dr. Kazi A. Kalpoma

Stack data structure


Stack data structure is like a container with a single opening. Can only access element at the top Add new items at the top Remove an item from the top Stack in real life: collection of elements arranged in a linear order. stack of books, stack of plates, stack of chairs.

Stack
push A stack is open at one end (the top) only. You can push entry onto the top, or pop the top entry out of the stack. Note that you cannot add/extract entry in the middle of the stack.

pop

top
C B

A
bottom

Last-in First-out (LIFO)


Insert(A); Insert(B); Insert(C);

B
A A

C B A

The last one pushed in is the first one popped out! (LIFO)

Remove(); Remove(); Remove();

B A

When we push entries onto the stack and then pop them out one by one, we will get the entries in reverse order.
4

Stack Operations
Push(X) insert X as the top element of the stack Pop() remove the top element of the stack and return it. Top() return the top element without removing it from the stack.

Stack Operations
top
top top top top 2 push(2) 5 2 push(5) 7 5 2 1 7 5 2 push(1)

push(7)

top top 7 5 2 1 pop()

21 7 5 2 push(21) 21 top 7 5 top

5
2 7 pop() top 5 2 pop()

2
pop()

Stack using an Array

top

1 7 5 2

1
top = 3

Array implementation
? ? ? ? B A
stack

#define stack_size 6;

int top; char stack[stack_size];

top = 2

stack
8

Initialization of Stack
stack_initialize() { top = -1; }
top

? ? ? ? ? ? entry top = 0

Push()
void push(char i) { if(top==stack_size - 1) cout<<"stack is full; else { top++; stack[top] = i; } }

top

? ? ? ? ? A entry
top= 0 1

10

Pop()
void pop() { if(top<0) cout<<"underflow: stack is empty;

else
{ x = stack[top]; top--; return x; } }
top

? ? ? ? ? ? entry
top= 10

11

Traversing a stack
void show() { int i; for(i=0;i<=top;i++) cout<< stack << i << = <<stack[i]; }
? ? D C B A
stack

top

3 2 1 0

12

Other operations
int top() { return stack[top]; } int IsEmpty() { return ( top == -1 ); } int IsFull() { return ( top == stack_size-1); }
13

Example of PUSH() and POP()


void main() { push(7); push (8); push (9); pop ( ); push (5); pop ( ); pop ( ); } 5 4 3 2 1 0

stack[0]=7 and top = 0 stack[1]=8 and top = 1 stack[2]=9 and top = 2 x = 9 and top = 1 stack[2]=5 and top = 2 x = 5 and top = 1 x = 8 and top = 0

top

stack

3/9/2014

Dr. Kazi A. Kalpoma

14

Stack Using Linked List

We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements.

Stack Using Linked List


For a singly-linked list, insert at start or end takes constant time using the head and current pointers respectively. Removing an element at the start is constant time but removal at the end required traversing the list to the node one before the last. Make sense to place stack elements at the start of the list because insert and removal are constant time.

Stack Using Linked List


No need for the current pointer; head is enough.

head top 1 7 5 2 1 7 5 2

Stack Operation: List


int pop() { int x = head->data; Node* p = head; head = head->Next; delete p; return x; }
head top 1 7 5 2

7 5 2

Stack Operation: List


void push(int x) { Node* newNode = new Node(); newNode->data = x; newNode->Next = head; head = newNode; }
head top 9 7 5 2 7 5 2

newNode

push(9)

Stack Operation: List


int top() { return head->data; }

int IsEmpty() { return ( head == NULL ); }

IsFull() is not needed anymore.

Stack: Array or List


Since both implementations support stack operations in constant time, any reason to choose one over the other? Allocating and de-allocating memory for list nodes does take more time than pre-allocated array. List uses only as much memory as required by the nodes; array requires allocation ahead of time.

List pointers (head, next) require extra memory.


Array has an upper limit; List is limited by dynamic memory allocation.

Você também pode gostar