Data Structure HKOI training /4/2010 So Pak Yeung
What is data structure? a particular way of storing and organizing data in a computer so that it can be used efficiently. From wikipedia
Outline Stack Stack Queue Queue Linked list Linked list
Stack Last In First Out (LIFO) Last In First Out (LIFO) Operations: Operations: PushPush PopPop
Stack - Push Put an element on the top of the stack Put an element on the top of the stack if (top==MAX_SIZE) //stack is full output error: stack overflow output error: stack overflow else stack[top++] = data; stack[top++] = data;
Stack - Pop Remove the element on the top of the stack Remove the element on the top of the stack if (top==0) //the stack is empty output Error: stack underflow output Error: stack underflow else{ // tmp = stack[--top]; --top;}
Queue First In First Out (FIFO) First In First Out (FIFO) Operations: Operations: EnqueueEnqueue DequeueDequeue
Queue - Enqueue Add an element at the end of the queue Add an element at the end of the queue if (e == MAX) output Error output Error else queue[e++] = data;
Queue: Dequeue Remove the element at the front of the queue Remove the element at the front of the queue if (f==e) //empty queue output error output error else{ // tmp = queue[f]; ++f;}
Queue Not efficient in terms of memory Not efficient in terms of memory Improvement: Improvement: Using linked list to implementUsing linked list to implement Circular QueueCircular Queue
Linked List Data + Pointers Data + Pointers Operations: Operations: InsertionInsertion DeletionDeletion
Linked List data1 data2data3data4 null head
Linked List - Insertion Assume we add a node after node 2 data1 data2data3data4 null head
Linked List - Insertion data1 data2data3data4 null new data head
Linked List - Insertion data1 data2data3data4 null new data head
Linked List - Insertion data1 data2data3data4 null new data head
Linked List - Insertion node* tmp = new node; tmp->data = new_data; tmp->next = target->next; target->next = tmp;
Linked List - Deletion Assume we delete node 3 data1 data2data3data4 null head
Linked List - Deletion data1 data2data3data4 null tmp head
Linked List - Deletion data1 data2data3data4 null tmp head
Linked List - Deletion data1 data2data4 null head
Linked List - Deletion node* tmp = target->next; target->next = tmp->next; delete tmp;
Linked List It is so troublesome when we try to delete the first node / insert before the first node It is so troublesome when we try to delete the first node / insert before the first node Dummy node Dummy node Doubly Linked List Doubly Linked List
Linked List data1 data2data3data4 null head
Types of Linked List Singly-Linked List Singly-Linked List Doubly-Linked List Doubly-Linked List Circularly-Linked List Circularly-Linked List
Questions?