Data Structures and Algorithms – C++ Implementation - Pdf 11

Data Structures and Algorithms –
C++ Implementation
Ho Chi Minh City University of Technology
Faculty of Computer Science and Engineering
BK
TP.HCM
BK
TP.HCM
Huỳnh Tấn Đạt
Email:
Home Page: />Pointer in C++
 Declaration
Node *ptr;
 Create an object
ptr = new Node();
 A pointer usage
printf
(“Data in node: %d”,
ptr
-
>data);
ptr
ptr
???
Slide 2Faculty of Computer Science and Engineering – HCMUT
printf
(“Data in node: %d”,
ptr
-
>data);
 Destroy an object

t = a;
a = b;
b = t;
}
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(p1, p2);
printf
(“%d”,
*
p1);
Slide 4Faculty of Computer Science and Engineering – HCMUT
}
void func(int* &a, int* &b){
int *t;
t = a;
a = b;
b = t;
}
printf
(“%d”,
*
p1);
printf(“%d”, *p2);
}
Parameter Passing Techniques
void func(int* &a, int* b){

*b = t;
}
void main() {
int *p1 = new int;
*p1 = 10;
int *p2 = new int;
*p2 = 20;
func(&p1, &p2);
printf
(“%d”, *p1);
Slide 6Faculty of Computer Science and Engineering – HCMUT
}
printf
(“%d”, *p1);
printf(“%d”, *p2);
}
Linked Lists
 A linked list is an ordered collection of data in which each
element contains the location of the next element
Element = Data + Link
head data link
Slide 7Faculty of Computer Science and Engineering – HCMUT
empty
linked list
Nodes
number name
A node with
one data field
number
A node with

>
link <pointer>
end
endend
end node
key <keyType>
field1 <…>
field2 <…>

fieldN <…>
end
endend
end dataType
Nodes – Implementation in C++
struct Node {
int data;
Node *next;
};
node
data <dataType>
link <pointer>
end node
Slide 10Faculty of Computer Science and Engineering – HCMUT
Nodes – Implementation in C++
Node *p = new Node();
p->data = 5;
cout<< p->data;
Node *q = p;
cout<< q->data;
Node *r = new Node();

Node<int> *q = p;
cout<< q->data;
Node<
int
> *r = new Node<
int
>();
p
5
q
10
Slide 13Faculty of Computer Science and Engineering – HCMUT
Node<
int
> *r = new Node<
int
>();
r->data = 10;
q->next = r;
cout<< p->next->data;
r
10
Nodes – Implementation in C++
template <class ItemType>
class Node{
public:
Node(){
this->next = NULL;
}
Node(

Linked List Algorithms
 Create list
 Insert node
 Delete node
 Traverse
 Destroy list
Slide 16Faculty of Computer Science and Engineering – HCMUT
Linked List Implementation
template <class List_ItemType>
class LinkedList{
public:
LinkedList();
~LinkedList();
protected:
int InsertNode(Node<List_ItemType>* pPre,
List_ItemType value);
List_ItemType
DeleteNode
(Node<
List_ItemType
>*
pPre
,
Slide 17Faculty of Computer Science and Engineering – HCMUT
List_ItemType
DeleteNode
(Node<
List_ItemType
>*
pPre

LinkedList<List_ItemType>* Clone();
protected:
//
Linked List Implementation
 How to use Linked List data structure?
int main(int argc, char* argv[]) {
LinkedList<int>* myList =
new LinkedList<int>();
myList->InsertFirst(15);
myList->InsertFirst(10);
myList
-
>
InsertFirst
(5);
Slide 19Faculty of Computer Science and Engineering – HCMUT
myList
-
>
InsertFirst
(5);
myList->InsertItem(18, 3);
myList->InsertLast(25);
myList->InsertItem(20, 3);
myList->DeleteItem(2);
printf("List 1:\n");
myList->Print2Console();
Linked List Implementation
//
int value;

list.count = 0
Create List
Algorithm createList (ref list <metadata>)
Initializes metadata for a linked list
Pre list is a metadata structure passed by reference
Post metadata initialized
1 list.head = null
2
list.count
= 0
Slide 22Faculty of Computer Science and Engineering – HCMUT
2
list.count
= 0
3 return
End createList
Linked List Implementation
template <class List_ItemType>
LinkedList<List_ItemType>::LinkedList(){
this->head = NULL;
this->count = 0;
}
Slide 23Faculty of Computer Science and Engineering – HCMUT
Insert Node
 Allocate memory for the new node and set up data
 Point the new node to its successor
 Point the new node's predecessor to it
Slide 24Faculty of Computer Science and Engineering – HCMUT
Insert into Empty List
Before


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status