Upload
franklin-causby
View
215
Download
1
Tags:
Embed Size (px)
Citation preview
Data StructureData StructureHKOI training 2009HKOI training 2009
3/4/20103/4/2010So Pak YeungSo 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
OutlineOutline
StackStack QueueQueue Linked listLinked list
StackStack
Last In First Out (LIFO)Last In First Out (LIFO) Operations:Operations:
• PushPush• PopPop
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;
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;
}}
QueueQueue
First In First Out (FIFO)First In First Out (FIFO) Operations:Operations:
• EnqueueEnqueue• DequeueDequeue
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;
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;
}}
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
Linked ListLinked List
Data + PointersData + Pointers Operations:Operations:
• InsertionInsertion• DeletionDeletion
Linked ListLinked List
data1 data2 data3 data4
nullhead
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
Linked List - InsertionLinked List - Insertion
data1 data2 data3 data4
nullnew data
head
Linked List - InsertionLinked List - Insertion
data1 data2 data3 data4
nullnew data
head
Linked List - InsertionLinked List - Insertion
data1 data2 data3 data4
nullnew data
head
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;
Linked List - DeletionLinked List - Deletion
Assume we delete node 3Assume we delete node 3
data1 data2 data3 data4
nullhead
Linked List - DeletionLinked List - Deletion
data1 data2 data3 data4
nulltmphead
Linked List - DeletionLinked List - Deletion
data1 data2 data3 data4
nulltmphead
Linked List - DeletionLinked List - Deletion
data1 data2 data4
nullhead
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;
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
Linked ListLinked List
data1 data2 data3 data4
null
head
Types of Linked ListTypes of Linked List
Singly-Linked ListSingly-Linked List Doubly-Linked ListDoubly-Linked List Circularly-Linked ListCircularly-Linked List
Questions?Questions?