1. Trang chủ
  2. » Công Nghệ Thông Tin

Data Structures and Algorithms – C++ Implementation ppt

53 675 2
Tài liệu được quét OCR, nội dung có thể không chính xác
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Data Structures and Algorithms – C++ Implementation
Trường học Ho Chi Minh City University of Technology
Chuyên ngành Computer Science and Engineering
Thể loại Lecture Slides
Thành phố Ho Chi Minh City
Định dạng
Số trang 53
Dung lượng 167,88 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Linked Lists LJ 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 XI empty

Trang 1

đề

s

Faculty of Computer Science and Engineering

BK TP.HCM 2

Trang 2

OA pointer usage pir

printf(“Data in node: %dqd”, ptr->data); `

Trang 3

Pointer in C++

_} Be careful in these cases:

Trang 4

Parameter Passing Techniques

void func(int* a, int* b){ void main() {

Trang 5

Parameter Passing Techniques

void func(int* &a, int* b){ void main() {

Trang 6

Parameter Passing Techniques

void func(int **a, int **b){ void main() {

Trang 7

Linked Lists

LJ 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

XI

empty

linked list

Faculty of Computer Science and Engineering —- HOMUT Slide 7

Trang 8

Nodes

A node with one data field

Trang 9

Nodes

count <integer> data <dataType> key <keyT ype>

head <pointer> link <pointer> field1 < >

end list end node field2 < >

Trang 10

Nodes — Implementation in C++

Trang 12

Nodes — Implementation in C++

Trang 15

Linked List — Implementation in C++

template <class List_ItemType>

list

Trang 16

Linked List Algorithms

Trang 17

Linked List Implementation

template <class List_ItemType>

Node<List_ItemType>* ploc),;

&pPre, Node<List_ItemType>* &pLoc);

Trang 18

Linked List Implementation

template <class List_ItemType>

class LinkedList {

public:

~LinkedList();

void InserthLast (List_ItemType value);

int InsertItem(List_ItemType value, int Position);

List_ItemType DeleteFirst();

List_ItemType DeleteLast();

int DeleteItem(int Postion);

Trang 19

Linked List Implementation

_] How to use Linked List data structure’

Trang 20

Linked List Implementation

Trang 22

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

3 return

Trang 23

Linked List Implementation

template <class List_ItemType>

Trang 24

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

Trang 25

Insert into Empty List

Trang 26

Insert at the Beginning

pNew -> link = list.head

list head = pNew

Trang 28

Insert at End

pPre -> link = pNew

4 > 39 > 52 > 715 ⁄

list _l ›| 134 Nx]

pPre pNew

Trang 29

Insert Node Algorithm

Algorithm insertNode (ref list <metadata>,

val pPre <node pointer>,

val dataln <datalype>)

Inserts data into a new node in the linked list

Pre list is metadata structure to a valid list

oPre Is pointer data's logical predecessor dataln contains data to be inserted

Post data have been inserted in sequence

Return true if successful, false if memory overflow

Faculty of Computer Science and Engineering —- HOMUT Slide 29

Trang 30

if (oPre = null) Adding before first node or to empty list

1 pNew -> link = list.head

2 list.head = pNew else

Adding in middle or at end

1 pNew -> link = pPre -> link

2 pPre -> link = pNew list.count = list.count + 1

return true

End _ insertNode

Trang 31

Insert Node

template <class List_ItemType>

Node<List_ItemType> *pNew = new Node<List_ItemType>();

_ II II MIIHII HH NHI gaiaaạaỤ

Faculty of Computer Science and Engineering — HOCMUT Slide 31

Trang 32

Delete Node

_] Locate the node to be deleted

_] Point the node predecessor's link to its Successor

_] Release the memory for the deleted node

Trang 33

Delete First Node

Trang 34

General Delete Case

Trang 35

Delete Node Algorithm

Algorithm deleteNode (ref list <metadata>,

val pPre <node pointer>,

val pLoc <node pointer>

ref dataOut <dataType>) Delete data from a linked list and returns it to calling

module

Pre list is metadata structure to a valid list

oPre is a pointer to predecessor node opLoc is a pointer to node to be deleted dataOut is variable to receive deleted data

Post data have been deleted and returned to caller

Faculty of Computer Science and Engineering — HOCMUT Slide 35

Trang 36

Delete Node Algorithm

1 dataOut = pLoc -> data

2 if (pPre = null)

Delete first node

1 list.nead = pLoc -> link

3 else

Delete other nodes

1 pPre -> link = pLoc -> link

4 list.count = list.count - 1

5 recycle (pLoc)

6 return

Trang 37

Delete Node

template <class List_ItemType>

List_ItemType LinkedList<List_ItemType>: :DeleteNode (

Node<List_ItemType> *pPre, Node<List_ItemType> *ploc) {

Trang 38

Traverse List

_] Traverse module controls the loop:

calling a user-supplied algorithm to process data

oWalker = list.head

loop (oWalker not null)

process (pWalker -> data}

oWalker = pWalker -> link

Trang 40

Searching in Linked List

template <class List_ItemType>

Trang 41

Destroy List Algorithm

Algorithm desiroyList (val list <metadata>)

Deletes all data in list

Pre list is metadata structure to a valid list

Post all data deleted

1 loop (list.head not null}

Trang 43

int InsertLast (List_ItemType value) ;

int InsertItem(List_ItemType value, int Position);

List_ItemType DeleteFirst();

List_ItemType DeleteLast();

int DeleteItem(int Postion);

Trang 44

Pointer vs Object Variable

Trang 45

Pointer vs Object Variable

_Ì What are the pros and cons?

Faculty of Computer Science and Engineering — HOCMUT Slide 45

Trang 46

sample Solution: Insert

template <class List_ItemType>

return O;

Node<List_ItemType>* newPtr, *pPre;

newPtr = new Node<List_ItemType>();

Trang 47

sample Solution: Insert

Trang 48

sample Solution: Delete

template <class List_ItemType>

1f (position < O || position > this->count)

Trang 49

sample Solution: Clone

template <class List_ItemType>

LinkedList<List_ItemType>*

LinkedList<List_ItemType>::Clone() { LinkedList<List_ItemType>* result =

New LinkedList<List_ItemType>(); Node<List_ItemType>* p = this-—>head;

Trang 50

Homework

_Ì Reverse a linked list

head +

a ! b || a >| b LS |

Result:

A

Ngày đăng: 06/03/2014, 17:20

TỪ KHÓA LIÊN QUAN