Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Data Types and Stacks

Similar presentations


Presentation on theme: "Abstract Data Types and Stacks"— Presentation transcript:

1 Abstract Data Types and Stacks
CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 10/2/2015

2 Abstract Datatypes (ADT)
ADT (Abstract Data Type) is a data type (a set of values and a collection of operations on those values) that can only be accessed through an interface. Client is a program that uses an ADT. E.g.: walmart.cpp Will have: #include “list.h” Implementation is a program that specifies the data type and the operations for it. E.g.: list.cpp Function definitions and struct definitions (or class definitions) Interface a list of operations available for that datatype. E.g.: list.h (notice, a header file, not a c file) It will contain headers of functions and typedef for data types, It is opaque: the client can not see the implementation through the interface.

3 Generalized Queues A generalized queue is an abstract data type that stores a set of objects. Let's use Item to denote the data type of each object. The fundamental operations that such a queue must support are: void insert(Queue q, Item x): adds object x to set q. Item delete(Queue q): choose an object x, remove that object from q, and return it to the calling function.

4 Generalized Queues Basic operations:
void insert(Queue q, Item x) Item delete(Queue q) The meaning of insert is clear in all cases: we want to add an item to an existing set. However, we note that delete does NOT take as an argument the item we want to delete, so the function itself must choose.

5 Generalized Queues - Delete
How can delete choose which item to delete? Choose the item that was inserted last. Choose the item that was inserted first. Choose a random item. If each item contains a key field: remove the item whose key is the smallest. You may be surprised as you find out, in this course, how important this issue is. We will spend significant time studying solutions corresponding to different choices.

6 The Pushdown Stack The pushdown stack behaves like the desk of a busy (and disorganized) professor. Work piles up in a stack. Whenever the professor has time, he picks up whatever is on top and deals with it. We call this model a LIFO (last-in, first-out) queue. The object that leaves the stack is always the object that was inserted last (among all objects still in the stack). Most of the times, instead of saying "pushdown stack" we simply say "stack". By default, a "stack" is a pushdown stack.

7 Push and Pop The pushdown stack supports insert and delete as follows:
insert push: This is what we call the insert operation when we talk about pushdown stacks. It puts an item "on top of the stack". delete pop: This is what we call the delete operation when we talk about pushdown stacks. It removes the item that was on top of the stack (the last item to be pushed, among all items still on the stack).

8 Examples of Push and Pop
15

9 Examples of Push and Pop
20 15

10 Examples of Push and Pop
pop() – returns 20 push(30) push(7) push(25) pop() push(12) 15

11 Examples of Push and Pop
30 15

12 Examples of Push and Pop
7 30 15

13 Examples of Push and Pop
25 7 30 15

14 Examples of Push and Pop
pop() – returns 25 push(12) 7 30 15

15 Examples of Push and Pop
12 7 30 15

16 Examples of Push and Pop
pop() – returns 12 7 30 15

17 Examples of Push and Pop
pop() – returns 7 30 15

18 Uses of Stacks Modeling a busy professor's desk is NOT the killer app for stacks. Examples of important stack applications:

19 Uses of Stacks Modeling a busy professor's desk is NOT the killer app for stacks. Examples of important stack applications: Function execution in computer programs: when a function is called, it enters the calling stack. The function that leaves the calling stack is always the last one that entered (among functions still in the stack). Interpretation and evaluation of symbolic expressions: stacks are used to evaluate things like (5+2)*(12-3), or to parse C code (as a first step in the compilation process). Search methods. Search is a fundamental algorithmic topic, with applications in navigation, game playing, problem solving… We will see more later in the course.

20 The Stack Interface (Textbook Version)
The textbook defines a stack interface that supports four specific functions: void STACKinit(int max_size): Initialize the stack. Argument max_size declares the maximum possible size for the stack. int STACKempty(): Returns 1 if the stack is empty, 0 otherwise. void STACKpush(Item item): Pushes the item on top of the stack. Item STACKpop(): Removes from the stack the item that was on top, and returns that item.

21 Problems With Textbook Interface?

22 Problems With Textbook Interface?
These functions do not refer to any specific stack object. What is the consequence of that?

23 Problems With Textbook Interface?
These functions do not refer to any specific stack object. What is the consequence of that? This interface can only support a single stack. If we need to use simultaneously two or more stacks, we need to extend the interface. In our implementation, we will pass the stack as an argument.

24 Implementing Stacks A stack can be implemented using:
lists or arrays Both implementations are fairly straightforward.

25 List-based Stacks List-based implementation: What is a stack?
A stack is essentially a list. push(stack, item) How? : O(??) pop(stack)

26 List-based Stacks … List-based implementation:
What is a stack? A stack is essentially a list. push(stack, item) How? : O(1) – ideally !!!! (frequent operation for this data structure) pop(stack) O(1) – ideally !!!! (frequent operation for this data structure) What type of insert and remove are fast for lists? How many ‘ways’ can we insert in a list? NULL

27 List-based Stacks … List-based implementation: What is a stack?
A stack is essentially a list. push(stack, item) How? : inserts that item at the beginning of the list. O(1) pop(stack) How? : removes (and returns) the item at the beginning of the list. NULL

28 Implementation Code See files posted on course website:
stack.h: defines the public interface. stack_list.cpp: defines stacks using lists. stack_array.cpp: defines stacks using arrays. Note that these files must be compiled with g++: Use stack_lists.cpp g o stacks stack_client.cpp stack_list.cpp list.cpp Use stack_arrays.cpp g++ -o stacks stack_client.cpp stack_array.cpp

29 The Stack Interface See file stack.h posted on the course website.
??? newStack(???); // this will be a bit tricky ??? destroyStack(???); ??? push(stack s, Item data); ??? pop(stack s); ??? isStackEmpty(???);

30 The Stack Interface See file stack.h posted on the course website.
stack newStack(int mx_sz = -1); void destroyStack(stack s); void push(stack s, Item data); Item pop(stack s); //NOTE: type Item, not link Item isStackEmpty(stack s);

31 Defining Stacks typedef struct stack_struct * stack; struct stack_struct { ???; };

32 Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; items Stack object List object length first NULL

33 Example typedef struct stack_struct * stack; struct stack_struct { list items; }; Empty stack. We will insert values in order: 20 7 15 pop() items Stack object List object length first NULL

34 Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; Insert values in given order: 20 7 15 pop() items Stack object List object length first 20 NULL

35 Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; Insert values in given order: 20 7 15 pop() items Stack object List object length first 7 20 NULL

36 Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; Insert values in given order: 20 7 15 pop() items Stack object List object length first 15 7 20 NULL

37 Defining Stacks typedef struct stack_struct * stack; struct stack_struct { list items; }; items Stack object List object length pop() will return 15 first 7 20 NULL

38 Creating a New Stack typedef struct stack_struct * stack; struct stack_struct { list items; }; stack newStack() ??? }

39 Creating a New Stack typedef struct stack_struct * stack; struct stack_struct { list items; }; stack newStack() stack result = malloc(sizeof(*result)); result->items = newList(); //mem alloc return result; }

40 Destroying a Stack typedef struct stack_struct * stack; struct stack_struct { list items; }; void destroyStack(stack s) ??? }

41 Destroying a Stack typedef struct stack_struct * stack; struct stack_struct { list items; }; void destroyStack(stack s) destroyList(s->items); free(s); }

42 Pushing an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; void push(stack s, Item content) ??? }

43 Pushing an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; void push(stack s, Item data) link L = newLink(data); insertAtBeginning(s->items, L); }

44 Popping an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; Item pop(stack s) ??? }

45 Popping an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; Item pop(stack s) link top = removeFirst(s->items); return linkItem(top); } What is wrong with this definition of pop?

46 Popping an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; Item pop(stack s) link top = removeFirst(s->items); return linkItem(top); } What is wrong with this definition of pop? Memory leak!!!

47 Popping an Item typedef struct stack_struct * stack; struct stack_struct { list items; }; Item pop(stack s) if (isStackEmpty(s)) ERROR. No item to remove!!! link top = removeFirst(s->items); Item item = linkItem(top); free(top); return item; }

48 WHY use a Stack ADT? Why should we have a Stack interface, when all we do is use list operations? Why not use a list directly? Protection: from performing unwanted ops (e.g. an insert at a random position in the list) Flexibility: To modify the current implementation To use another stack implementation Isolates the dependency of the Stack implementation on the list interface: if the list INTERFACE (.h file) is modified we will only need to go and change the STACK implementation (.cpp file), not all the lines of code when a stack operation is done in all the client programs. It makes the stack behavior explicit: what we can do and we can not do with a stack.

49 “Explicitly, why is this better than just using a list where I only insert and delete from the beginning?” Keep in mind that your goal here is to implement a stack datatype, for others to use, not for your own one-time usage. Directly providing a list, gives access to operations not allowed for a stack like reversing the list, or removing any item in it. Any client code that includes the stack.h file, can only use the functions provided in that file so they can only call: pop, push, isEmpty, newStack, destroyStack. They can not reverse the list because they do not have access to the stack_struct definition so they can not get the list object. A function in the stack_list.cpp file can call any list function but that is the point where you focus on implementing a stack and so you should not do operations that are not allowed. Notice that even in this file, you cannot access fields of the list_struct directly. It has to go through function calls (e.g. you cannot write my_list->length, but you can write getLength(my_list) ). Note that the stack_list.cpp is a client for list.cpp and so it does not have access to the list representation.

50 Array-Based Implementation of Stacks

51 Array-based Stacks Array-based implementation:
What is a stack? What will hold the data of the stack? push(stack, item) How? : O(1) - can we get this? pop(stack)

52 Array-based Stacks Array-based implementation: See stack_array.cpp
What is a stack? What will hold the data of the stack? An array. push(stack, item) How? : ‘insert’ at the end of the array. O(1) - Yes pop(stack) How? : ‘remove’ from the end of the array. See stack_array.cpp

53 Array-based Stacks index max_size -1 55 top -1

54 Array-based Stacks index max_size -1 push(5) 55 top 5

55 Array-based Stacks index max_size -1 push(5) push(8) 55 top 1 8 5

56 Array-based Stacks 20 8 5 index max_size -1 push(5) push(8) push(20)
55 top 20 2 8 5

57 Array-based Stacks 8 5 index max_size -1 push(5) push(8) push(20)
pop() 55 top 1 8 5

58 Defining Stacks Using Arrays
typedef struct stack_struct * stack; struct stack_struct { Item * items; int max_size; int top; }; 55 8 5

59 Creating a New Stack struct stack_struct { Item* items; int top; int max_size; }; stack newStack(???) { ??? }

60 Creating a New Stack struct stack_struct { Item * items; int top; int max_size; }; stack newStack(int max_size) { stack result = (stack)malloc(sizeof(*result)); result->items = (Item*)malloc(max_size * sizeof(Item)); result->max_size = max_size; result->top = -1; return result; }

61 Destroying a Stack struct stack_struct { Item * items; int top; int max_size; }; void destroyStack(stack s) { ??? }

62 Destroying a Stack struct stack_struct { Item * items; int top; int max_size; }; void destroyStack(stack s) { free(s->items); free(s); }

63 Pushing an Item struct stack_struct { Item * items; int top; int max_size; }; void push(stack s, Item data) { ??? }

64 Pushing an Item struct stack_struct { Item * items; int top; int max_size; }; void push(stack s, Item data) { if (s->top == s->max_size - 1) ERROR. No more room ( the array is full)!!! return; s->top = s->top + 1; s->items[s->top] = data; }

65 Popping an Item struct stack_struct { Item * items; int top; int max_size; }; ??? pop(stack s) { ??? }

66 Popping an Item struct stack_struct { Item * items; int top; int max_size; }; Item pop(stack s) { if (isStackEmpty(s)) ERROR. No data to pop!!! return 0; Item item = s->items[s->top_index]; s->top = s->top - 1; return item; }


Download ppt "Abstract Data Types and Stacks"

Similar presentations


Ads by Google