Chapter 2 Linear Table
List, Stacks, Queue
Abstract Data Type / ADT
Characteratics of ADT
- Separated from the representation and implementation
List
ADT
Objects item
Operations Find, Print, Making an empty, Finding, Insert. Delete…
Array Implementation
array[i] = item_i
Advan.
- Find quickly
Disadvan.
- MaxSize has to be estimated
- Insertion and Deletion slowly
Linked List Implementation
LinkedListNode = { ptrToNextNode, item_i }
[Eg]
typedef struct listNode *listPtr; |
tip Use Dummy Head Node to cut off extra codes!!
dummy: fake
Insert
tmp->next = node->next; |
Delete
curNode->next = node->next; |
tip Remember to free the trash node to save memory
Doubly Linked Circular Lists
[Eg]
typedef struct listNode *listPtr; |
Let tail node’s next point to head node, while let head node’s pre point to tail node.
Applications
- The Polynomial ADT
- Cursor Implementation of Linked Lists (no pointer)
The Stack ADT
ADT
- Last-In-First-Out list
- insertions and deletions made at the top only
Implementations
Linked List Implementation
- Push
Tmp -> Next = S -> Next;
S -> Next = Tmp; - Top
return S -> Next -> Element;
- Pop
Ret = S -> Next;
S -> Next = S -> Next -> Next;
free(Ret);
To keep an another recycle bin stack can reduce the expense of regular malloc() and free().
Array Implementation
struct StackRecord{ |
- Push
- Top
- Pop
Note
- The stack model must be well encapsulated.
- Error check must be done before Push, Top and Pop.
Application
-
Balancing Symbols
-
Postfix Evaluation
这个音效特别棒嗷
-
Infix to Postfix Conversion
-
Function Calls
The Queue ADT
ADT
Implementations
Array Implementation of Queues
struct QueueRecord{ |