The document discusses abstract data types (ADTs) and several common ADTs including stacks and queues. It defines an ADT as a data type paired with meaningful operations for that data type, with the data and operations encapsulated and hidden from the user. Stacks are described as lists where insertion and deletion only occur at one end, following a LIFO order. Common stack operations like push, pop, and empty are explained. Queues are lists where insertion occurs at the rear and deletion at the front, following a FIFO order. Examples of using stacks and queues are provided.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
33 views
Present Csi Abstract Data Type
The document discusses abstract data types (ADTs) and several common ADTs including stacks and queues. It defines an ADT as a data type paired with meaningful operations for that data type, with the data and operations encapsulated and hidden from the user. Stacks are described as lists where insertion and deletion only occur at one end, following a LIFO order. Common stack operations like push, pop, and empty are explained. Queues are lists where insertion occurs at the rear and deletion at the front, following a FIFO order. Examples of using stacks and queues are provided.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25
CHAPTER 12
Abstract Data Type
Objectives Concept of an abstract data type (ADT). Applications and how implemented : - Stack, the basic operations on stacks.
- Queue, the basic operations on queues.
- General tree. - General linear list, the basic operations.
- Binary tree—a special kind of tree.
- Binary search tree (BST). - Graph. 12-1 BACKGROUND • Problem solving with a computer is processing data. • Define data type , operation to be performed to process data. • Definition of the data type and the operation to be applied to the data is part of the idea behind an abstract data type (ADT)—to hide how the operation is performed on the data 12.1.1 Simple ADTs • Some simple ADTs as integral parts of the language. • Example: - the C language : simple ADT called an integer. - C defines several operations :add, sub, multi, div, and so on. 12.1.2 Complex ADTs
are available for use in most language, many useful complex ADTs are not. • Example: - A list ADT, a stack ADT, a queue ADT, and so on. - ADTs should be created and stored in the library of the computer to be used.
The concept of abstraction means:
1. We know what a data type can do. 2. How it is done is hidden. 12.1.3 Definition • Is a data type packaged with the operations meaningful for the data type. • Then encapsulate the data and the operations on the data and hide them from the user.
Abstract data type:
1. Definition of data 2. Definition of operations 3. Encapsulation of data and operation 12.1.4 Model for an abstract data type • Inside the ADT are two different parts of the model: data structure and operations (public and private). Figure 12.1 The model for an ADT 12.1.5 Implementation
• Computer languages do not provide complex ADT
packages.
• To create a complex ADT, it is first implemented
and kept in a library. 12-2 STACKS • A stack is a restricted linear list, all additions and deletions are made at one end, the top. • If we insert a series of data items into a stack and then remove them, the order is reversed. Figure 12.2 Three representations of stacks 12.2.1 Operations on stacks • Four basic operations: stack, push, pop, and empty. The stack operation • Creates an empty stack.
Figure 12.3 Stack operation
The push operation • Inserts an item at the top of the stack.
Figure 12.4 Push operation
The pop operation • Deletes the item at the top of the stack.
Figure 12.5 Pop operation
The empty operation • Checks the status of the stack.
• Returns true if the stack is empty and false if the
stack is not empty. 12.2.2 Stack ADT • We define a stack as an ADT as shown below: Example 12.1 • Figure 12.6 shows a segment of an algorithm that applies the previously defined operations on a stack S. Figure 12.6 Example 12.1 12.2.3 Stack applications • Four broad categories: reversing data, pairing data, postponing data usage, and backtracking steps. Reversing data items • Requires a set of data items be reordered to the first and last items are exchanged. • For example, the list (2, 4, 7, 1, 6, 8) becomes (8, 6, 1, 7, 4, 2). Example 12.2
Algorithm 12.1 shows the pseudocode to convert a
decimal integer to binary and print the result. Pairing data items • Need to pair some characters in an expression. • For example, when we write a mathematical expression in a computer language, we often need to use parentheses to change the precedence of operators. Example 12.3 • Algorithm 12.2 shows how we can check if all opening parentheses are paired with a closing parenthesis. Algorithm 12.2 Continued 12.2.4 Stack implementation
• Stack ADTs can be implemented using either an
array or a linked list. • Figure 12.7 shows an example of a stack ADT with five items Figure 12.7 Stack implementations 12-3 QUEUES • A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called the front