Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Lecture 4 Stack-Array and Liked List

This document discusses stacks and their implementation using arrays and linked lists. It begins by defining stacks as an abstract data type (ADT) that supports push, pop, top, size and isEmpty methods. It then provides examples of stacks in web browsers and text editors. The document goes on to describe implementations of stacks using arrays and linked lists, analyzing their time complexities. It also shows how a stack can be used to reverse an array.

Uploaded by

Keira Soarin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Lecture 4 Stack-Array and Liked List

This document discusses stacks and their implementation using arrays and linked lists. It begins by defining stacks as an abstract data type (ADT) that supports push, pop, top, size and isEmpty methods. It then provides examples of stacks in web browsers and text editors. The document goes on to describe implementations of stacks using arrays and linked lists, analyzing their time complexities. It also shows how a stack can be used to reverse an array.

Uploaded by

Keira Soarin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Lecture 4

ADT Stack
Array- and linked list-
based implementation
Dr. Wael Zakaria

1
Abstract data type (ADT)-Stack
• Formally, a stack is an abstract data type (ADT) that supports the following
two update methods:
• push(e): Adds element e to the top of the stack.
• pop( ) : Removes and returns the top element from the stack (or null if the
stack is empty).
• Additionally, a stack supports the following accessor methods for
convenience:
• top( ) : Returns the top element of the stack, without removing it (or null if
the stack is empty).
• size( ) : Returns the number of elements in the stack.
• isEmpty( ): Returns a boolean indicating whether the stack is empty.

4
Abstract data type (ADT)-Stack
• A stack is a collection of objects that are inserted and removed
according to the last-in, first-out (LIFO) principle.
• A user may insert objects into a stack at any time,
but may only access or remove the most recently
inserted object that remains
(at the so-called “top” of the stack).

5
Abstract data type (ADT)-Stack (Examples)
Example 6.1:
• Internet Web browsers store the addresses of recently visited sites on a stack.
• Each time a user visits a new site, that site’s address is “pushed” onto the
stack of addresses.
• The browser then allows the user to “pop” back to previously
visited sites using the “back” button.
Example 6.2:
• Text editors usually provide an “undo” mechanism that cancels recent editing
operations and reverts to former states of a document.
• This undo operation can be accomplished by keeping text changes in a stack.

6
Abstract data type (ADT)-Stack (Examples)
Example 6.3:
• The following table shows a series of stack operations and their
effects on an initially empty stack S of integers.

• Stack animation
https://yongdanielliang.github.io/animation/web/Stack.html

7
Abstract data type (ADT)-Stack (Examples)
Example 6.3:
• The following table shows a series of stack operations and their
effects on an initially empty stack S of integers.

• Stack is empty

top
8
Abstract data type (ADT)-Stack (Examples)
Example 6.3:
• The following table shows a series of stack operations and their
effects on an initially empty stack S of integers.

• Push (5)

5 top
9
Abstract data type (ADT)-Stack (Examples)
Example 6.3:
• The following table shows a series of stack operations and their
effects on an initially empty stack S of integers.

• Push (7)

7 top
5

10
Abstract data type (ADT)-Stack (Examples)
Example 6.3:
• The following table shows a series of stack operations and their
effects on an initially empty stack S of integers.

• pop ()
Remove the top
Element 7 and return it

5 top
11
Array-Based Stack Implementation
• As our first implementation of the stack ADT, we store elements in
• an array, named data,
• with capacity N for some fixed N.

12
Array-Based Stack Implementation
• In java, that arrays start at index 0 in Java, when the stack holds
elements from data[0] to data[t] inclusive, it has size t +1.
• By convention, when the stack is empty it will have t equal to −1 (and
thus has size t +1, which is 0).

13
Array-Based Stack Implementation
• The structure of storage is
typedef int ElementType;
typedef int Position;
class Stack
{
private:
int capacity;// max stack size = size - 1
Position TOP;// current top of stack
ElementType* elements;// element array

public:
Stack(int size = 10){// constructor
elements=new ElementType[size];
TOP =-1;
capacity=size;
}
~Stack() { delete [] elements; }// destructor
14
Array-Based Stack Implementation
bool empty() { return TOP ==-1; }
bool full() {return TOP == capacity-1; }

void push(ElementType x){


if (full()) // if stack is full, print error
cout << "Error: the stack is full." << endl;
else
elements[++TOP] = x;
}

15
Array-Based Stack Implementation
ElementType pop(){
if (empty()) { //if stack is empty, print error
cout << "Error: the stack is empty." << endl;
return -1;
}
else {
return elements[TOP--];
}
}

ElementType top(){
if (empty()) {
cout << "Error: the stack is empty." << endl;
return -1;
}
else
return elements[TOP];
} 16
Output of
ArrayStackUsage class

17
A Drawback of This Array-Based Stack
Implementation
• The array implementation of a stack is simple and efficient.
• Nevertheless, this implementation has one negative aspect—
• it relies on a fixed-capacity array, which limits the ultimate size of the stack.

• If user of a stack specifies the capacity =10


• If the application needs much less space than the reserved capacity, memory is wasted.
• If the application needs to push an item onto a stack that has already reached its
maximum capacity, the implementation throws an IllegalStateException, refusing to
store the new element.
• Thus, even with its simplicity and efficiency, the array-based stack implementation is not
necessarily ideal.

18
Analyzing the Array-Based Stack
Implementation
Table 6.2 shows the running times for methods of this array-based
stack implementation.

20
Reversing an Array Using a Stack
void reverse(ElementType a[],int n) {
Stack buffer(n);
for (int i=0; i < n; i++)
buffer.push(a[i]);
for (int i=0; i < n; i++)
a[i] = buffer.pop( );
}

21
Reversing an Array Using a Stack
/** Tester routine for reversing arrays */
public static void main(String args[ ]) {
int a[] = {4, 8, 15, 16, 23, 42};
reverse(a,6);
cout<<“a=[”
for (int i=0; i < 6-1; i++)
cout<<a[i]<<“,” ;
cout<<a[5] << “]”; Reversing...
} a = [42, 23, 16, 15, 8, 4]

22
ADT Stack
linked list-
based implementation

23
Abstract data type (ADT)-Stack
• Recall that, Formally, a stack is an abstract data type (ADT) that supports
the following two update methods:
• push(e): Adds element e to the top of the stack.
• pop( ): Removes and returns the top element from the stack (or null if the
stack is empty).
• Additionally, a stack supports the following accessor methods for
convenience:
• top(): Returns the top element of the stack, without removing it (or null if the
stack is empty).
• size() : Returns the number of elements in the stack.
• isEmpty(): Returns a boolean indicating whether the stack is empty.

24
Linked-Based Stack Implementation
• The structure of storage is

25
Linked-Based Stack Implementation

26
Linked-Based Stack Implementation

27
Linked-Based Stack Implementation

28
Linked-Based Stack Implementation

29
Linked-Based Stack Implementation

30
ADT Stack
Implement stack using Linked List

31
32
Applications

33
34

You might also like