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

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

Embed Size (px)

Citation preview

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

Data StructureData StructureHKOI training 2009HKOI training 2009

3/4/20103/4/2010So Pak YeungSo Pak Yeung

Page 2: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

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

From wikipediaFrom wikipedia

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

OutlineOutline

StackStack QueueQueue Linked listLinked list

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

StackStack

Last In First Out (LIFO)Last In First Out (LIFO) Operations:Operations:

• PushPush• PopPop

Page 5: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Stack - PushStack - Push

Put an element on the top of the stackPut an element on the top of the stack

if (top==MAX_SIZE) //stack is fullif (top==MAX_SIZE) //stack is full output “error: stack overflow”output “error: stack overflow”

elseelse stack[top++] = data;stack[top++] = data;

Page 6: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Stack - PopStack - Pop

Remove the element on the top of the stackRemove the element on the top of the stack

if (top==0) //the stack is emptyif (top==0) //the stack is emptyoutput “Error: stack underflow”output “Error: stack underflow”

else{ else{ // tmp = stack[--top]; // tmp = stack[--top]; --top;--top;

}}

Page 7: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

QueueQueue

First In First Out (FIFO)First In First Out (FIFO) Operations:Operations:

• EnqueueEnqueue• DequeueDequeue

Page 8: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Queue - EnqueueQueue - Enqueue

Add an element at the end of the queueAdd an element at the end of the queue

if (e == MAX)if (e == MAX)output “Error”output “Error”

elseelsequeue[e++] = data;queue[e++] = data;

Page 9: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Queue: DequeueQueue: Dequeue

Remove the element at the front of the queueRemove the element at the front of the queue

if (f==e) //empty queueif (f==e) //empty queueoutput “error”output “error”

else{else{// tmp = queue[f];// tmp = queue[f];++f;++f;

}}

Page 10: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

QueueQueue

Not efficient in terms of memoryNot efficient in terms of memory Improvement:Improvement:

• Using linked list to implementUsing linked list to implement• Circular QueueCircular Queue

Page 11: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked ListLinked List

Data + PointersData + Pointers Operations:Operations:

• InsertionInsertion• DeletionDeletion

Page 12: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked ListLinked List

data1 data2 data3 data4

nullhead

Page 13: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - InsertionLinked List - Insertion

Assume we add a node after node 2Assume we add a node after node 2

data1 data2 data3 data4

nullhead

Page 14: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - InsertionLinked List - Insertion

data1 data2 data3 data4

nullnew data

head

Page 15: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - InsertionLinked List - Insertion

data1 data2 data3 data4

nullnew data

head

Page 16: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - InsertionLinked List - Insertion

data1 data2 data3 data4

nullnew data

head

Page 17: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - InsertionLinked List - Insertion

node* tmp = new node;node* tmp = new node;tmp->data = new_data;tmp->data = new_data;tmp->next = target->next;tmp->next = target->next;target->next = tmp;target->next = tmp;

Page 18: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - DeletionLinked List - Deletion

Assume we delete node 3Assume we delete node 3

data1 data2 data3 data4

nullhead

Page 19: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - DeletionLinked List - Deletion

data1 data2 data3 data4

nulltmphead

Page 20: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - DeletionLinked List - Deletion

data1 data2 data3 data4

nulltmphead

Page 21: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - DeletionLinked List - Deletion

data1 data2 data4

nullhead

Page 22: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked List - DeletionLinked List - Deletion

node* tmp = target->next;node* tmp = target->next;target->next = tmp->next;target->next = tmp->next;delete tmp;delete tmp;

Page 23: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked ListLinked List

It is so troublesome when we try to It is so troublesome when we try to delete the first node / insert before delete the first node / insert before the first nodethe first node

Dummy nodeDummy node Doubly Linked ListDoubly Linked List

Page 24: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Linked ListLinked List

data1 data2 data3 data4

null

head

Page 25: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Types of Linked ListTypes of Linked List

Singly-Linked ListSingly-Linked List Doubly-Linked ListDoubly-Linked List Circularly-Linked ListCircularly-Linked List

Page 26: Data Structure HKOI training 2009 3/4/2010 So Pak Yeung

Questions?Questions?