Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structure HKOI training 2009 3/4/2010 So Pak Yeung.

Similar presentations


Presentation on theme: "Data Structure HKOI training 2009 3/4/2010 So Pak Yeung."— Presentation transcript:

1 Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

2 What is data structure? a particular way of storing and organizing data in a computer so that it can be used efficiently. From wikipedia

3 Outline Stack Stack Queue Queue Linked list Linked list

4 Stack Last In First Out (LIFO) Last In First Out (LIFO) Operations: Operations: PushPush PopPop

5 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;

6 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;}

7 Queue First In First Out (FIFO) First In First Out (FIFO) Operations: Operations: EnqueueEnqueue DequeueDequeue

8 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;

9 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;}

10 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

11 Linked List Data + Pointers Data + Pointers Operations: Operations: InsertionInsertion DeletionDeletion

12 Linked List data1 data2data3data4 null head

13 Linked List - Insertion Assume we add a node after node 2 data1 data2data3data4 null head

14 Linked List - Insertion data1 data2data3data4 null new data head

15 Linked List - Insertion data1 data2data3data4 null new data head

16 Linked List - Insertion data1 data2data3data4 null new data head

17 Linked List - Insertion node* tmp = new node; tmp->data = new_data; tmp->next = target->next; target->next = tmp;

18 Linked List - Deletion Assume we delete node 3 data1 data2data3data4 null head

19 Linked List - Deletion data1 data2data3data4 null tmp head

20 Linked List - Deletion data1 data2data3data4 null tmp head

21 Linked List - Deletion data1 data2data4 null head

22 Linked List - Deletion node* tmp = target->next; target->next = tmp->next; delete tmp;

23 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

24 Linked List data1 data2data3data4 null head

25 Types of Linked List Singly-Linked List Singly-Linked List Doubly-Linked List Doubly-Linked List Circularly-Linked List Circularly-Linked List

26 Questions?


Download ppt "Data Structure HKOI training 2009 3/4/2010 So Pak Yeung."

Similar presentations


Ads by Google