From:《Data Structures and Algorithm Analysis in C++》 chapter 3
This chapter discusses three of the most simple and basic data structures.
I will :
- Introduce the concept of Abstract Data Types.
- Show how to efficiently perform operatioins on lists.
- Introduce the stack ADT
- Introduce the queue ADT
1. Abstract Data Types (ADTs)
An ADT is a set of objects together with a set of operation.
- Object : such as list, set, graphs, integer, real, boolean...
- Operation: such as find, remove, insert, get....
The C++ class allows for the implementation of ADTs, with appropriate hiding of implementatioin details.
There is no rule telling us which operations must be supported for each ADT; this is a design decision.
2. The List ADT
we will deal with a general list of the form A0,A1,A2...AN-1;we say the size of list is N, we will canll the special list of size 0 an empty list.
For any list except the empty list, we say that Ai follows Ai-1(i < N )and that Ai-1 precedes Ai (i > 0). The first element of the list is A0,and the last element is An-1.
2.1 Simple Array Implementation of Lists
All of these instructions can be implemented just by using an array. Although arrays are created with a fixed capacity.
- printList is carried out in linear time;
- findKth takes constant time;
- insertion and deletion are potentially expensive, depending on where the insertions and deletetions.
2.2 Simple Linked Lists
In order to avoid the linear cost of insertion and deletion, we need to ensure that the list is not stored contiguously, since otherwise entire parts of thte list will need to be moved.
The linked list consists of a series of nodes, which are not necessarily adjacent in memory. Each node contains the element and a link to a node containing its successor. we call this the next link. The last cell's next link points to NULL.
- printList() and find(x) take a linear-time;
- remove() take a constant time;
- insert() take a constant time.
3. The Stack ADT
3.1 Stack Model
A stack is a list with the restriction that insertions and deletions can be performed in only one position, namely, the end of the list, called the top.
There are two main operations:
- Push : insert an element into stack;
- Pop : delete the most recently inserted element.
A pop or top on an empty stack is generally considered an error in the stack ADT.
On the other hand, running out of space when performing a push is an implementation limit but not an ADT Error.
Stack is sometimes known as LIFO(last in , first out) lists.
4. The Queue ADT
Like stacks, queues are list. with a queue, however, insertion is done at one end, whereas deletion is erformed at the other hand.
The basic operatioins on a queue :
- enqueue : inserts an element at the end of the list;
- dequeue : deletes the element at the start of the list.