intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Lecture Data Structures & Algorithms: Chapter 5

Chia sẻ: Na Na | Ngày: | Loại File: PPTX | Số trang:72

81
lượt xem
3
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Lecture Data Structures & Algorithms: Chapter 5 Linked Lists presented the Concept Of The Linked List, Single Linked List, Double Linked List, Circular Linked List.

Chủ đề:
Lưu

Nội dung Text: Lecture Data Structures & Algorithms: Chapter 5

  1. DONG NAI UNIVERSITY OF TECHNOLOGY Data Structures & Algorithms 1
  2. DONG NAI UNIVERSITY OF TECHNOLOGY Linked Lists 2
  3. DONG NAI UNIVERSITY OF TECHNOLOGY The Concept Of The Linked List Single Linked List Double Linked List Circular Linked List 3
  4. DONG NAI UNIVERSITY OF TECHNOLOGY The Concept Of The Linked List - The size requirement need not be known at compile time. - A linked list is a data structure that is used to model such a dynamic list of data items, so the study of the linked lists as one of the data structures is important. 4
  5. DONG NAI UNIVERSITY OF TECHNOLOGY ARRAY Linked List - Sequential - An element is mapping, elements required to be are fixed distance linked with a apart. previous element - Makes insertion or of the list. deletion at any - Done by storing arbitrary position in the address of the an array a costly next element operation 5
  6. DONG NAI UNIVERSITY OF TECHNOLOGY M[0] M[1] M[2] M[3] M[4] M[5] M[6] 5 2 9 7 6 0 8 Data pNext Data pNext Data pNext Data pNext 6
  7. 4 things when build Linked DONG NAI UNIVERSITY OF TECHNOLOGY 1. List - Data element - Link field element Structure 2. Make linking between elements: - First, Last, Middle element 3. How many node to take all list elements, How to take all list. 4. Basic operations: - Insert new element (every position) - Delete (every position) - Count, Search, Sort, DeAllocated… 7
  8. DONG NAI UNIVERSITY OF TECHNOLOGY Single Linked List Data pNext Data pNext NULL Data pNext Data pNext 8
  9. DONG NAI UNIVERSITY OF TECHNOLOGY 1. Structure struct Node { int data; Node *pNext; }; 9
  10. DONG NAI UNIVERSITY OF TECHNOLOGY How to use node? Node node1; Node *node2; node2 =new Node(); node1.data=10; if(node2==NULL)exit(0); node1.pNext=NULL; node2->data=20; node2->pNext=NULL; cout
  11. DONG NAI UNIVERSITY OF TECHNOLOGY 2. The way to make link between elements First, last, middle element data pNext data pNext data pNext data pNext NULL Head Middle Last 11
  12. DONG NAI UNIVERSITY OF TECHNOLOGY pTail 3. How many node to take all list elements, how to take all list?pNext data data pNext data pNext data pNext NULL Why? • +from pHead, can we list all items? pHead • +from pHead, can we do everything with list: insert new, delete? • +Do we need pTail? 12
  13. DONG NAI UNIVERSITY OF TECHNOLOGY 4. Basic operations: Initialize PrintList CreateNode SumOfList InsertFirst SizeOfList InsertLast SearchNode InsertMid SortList RemoveNode FreeMemory pHead only or pHead & pTail ??? 13
  14. DONG NAI UNIVERSITY OF TECHNOLOGY Using pHead only 14
  15. DONG NAI UNIVERSITY OF TECHNOLOGY Initialize typedef struct Node { int data; Node *pNext; }; typedef struct SingleList { Node *pHead; }; void Initialize(SingleList &list) { list.pHead=NULL; } 15
  16. DONG NAI UNIVERSITY OF TECHNOLOGY Node *CreateNode(int d) CreateNode { Node *pNode=new Node; if(pNode!=NULL) pNode { pNode->data=d; Data pNext NULL pNode->pNext=NULL; } else { cout
  17. DONG NAI UNIVERSITY OF TECHNOLOGY InsertFirst pHead 8 pNext 9 pNext 5 pNext NULL pNode 3 pNext NULL pHead 8 pNext 9 pNext 5 pNext NULL pNode 3 pNext pHead pNode 3 pNext 8 pNext 9 pNext 5 pNext NULL 17
  18. DONG NAI UNIVERSITY OF TECHNOLOGY InsertFirst void InsertFirst(SingleList &list,int d) { Node *pNewNode=CreateNode(d); if(list.pHead==NULL) { list.pHead=pNewNode; } else { pNewNode->pNext=list.pHead; list.pHead=pNewNode; } } 18
  19. DONG NAI UNIVERSITY OF TECHNOLOGY pHead InsertLast 8 pNext 9 pNext 5 pNext NULL pNode 3 pNext NULL pHead 8 pNext 9 pNext 5 pNext pNode 3 pNext NULL pHead pNode 8 pNext 9 pNext 5 pNext 3 pNext NULL 19
  20. DONG NAI UNIVERSITY OF TECHNOLOGY InsertLast void InsertLast(SingleList &list,int d) { Node *pNewNode=CreateNode(d); if(list.pHead==NULL) { list.pHead=pNewNode; } else { Node *pTmp=list.pHead; while(pTmp->pNext!=NULL) { pTmp=pTmp->pNext; } pTmp->pNext=pNewNode; } 20
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2