COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Stacks, Queues, and Linked Lists
Linked Lists.
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists CENG 213 Data Structures.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
COMP103 - Linked Lists (Part B)1 Chapter 17 Linked List as Objects.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Data Structures( 数据结构 ) Chapter3:Linked List. 2 西南财经大学天府学院 Vocabulary Linear List 线性表 Linked List 链表 Retrieval 检索 Traversal 遍历 Node 结点 Circularly Linked.
Chapter 12 – Data Structures
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure and Algorithms
List ADT & Linked Lists.
Linked lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCI 3333 Data Structures Linked Lists.
Chapter 4 Linked Lists
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Arrays and Linked Lists
Linked List Intro CSCE 121 J. Michael Moore.
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
Linked Lists.
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Linked List Intro CSCE 121.
Linked lists.
Linked Lists.
Presentation transcript:

COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management

COMP103 - Linked Lists (Part A)2 Motivation A “List” is a very useful structure to hold a collection of data. Examples: List of students marks List of temperatures for a period of time Implementation in C++ using Static memory (array) – int marks[10]; Note: we need to know the size of the array “Dynamic” memory – int n, *marks; Cin >> n; Marks = new int[n]; Note: once memory is allocated, the size of the array is fixed (i.e. static) Linked list – truly dynamic

COMP103 - Linked Lists (Part A)3 Motivation Common operations on lists: Traverse the list (useful in printing the list) Search for an item Add an item Delete an item Note: Algorithms of these operations based on static arrays are not very efficient. Adding or deleting an item in the array involves a lot of copying of items in the list. And the actual memory “size” of the array remains fixed. Using linked lists improves the efficiency. The size of the list can grow and shrink as we add or delete items from the list.

COMP103 - Linked Lists (Part A)4 Linked Lists: Basic Idea A linked list is an ordered collection of data Each element of the linked list has Some data A link to the next element The link is used to chain (or connect) the data Example: A linked list of integers: Data Link

COMP103 - Linked Lists (Part A)5 Linked Lists: Basic Idea The list can grow and shrink add(75), add(85) delete(85), delete(45), delete(75) List

COMP103 - Linked Lists (Part A)6 Linked List Structure Node - Data + Link Data - contains useful information Link – chains(connects) the data together Head pointer - points to the first element Empty List - head pointer equals NULL node =NULL

COMP103 - Linked Lists (Part A)7 Nodes Data may be simple… …or more complex

COMP103 - Linked Lists (Part A)8 Nodes in C++ Simple: data Define the node type: struct NODE { int data; NODE *link; }; In the program: void main() {// p_new is of type NODE NODE *p_new; // allocating memory for one node p_new = new NODE; // assigning value to the data field in the node p_new->data = 10; // link points to NULL p_new->link = NULL; } 10

COMP103 - Linked Lists (Part A)9 Nodes in C++ Complex node: Define the node type: struct DATA {char key; int ID; int tel; }; struct NODE { DATA data; NODE *link; }; In the program: void main() {// p_new is of type NODE NODE *p_new; // allocating memory for one node p_new = new NODE; // assigning values to the data field in the node p_new->data.key = ‘a’; p_new->data.ID = 123; p_new->data.tel = 5678; // link points to NULL p_new->link = NULL; } tel. no. ID key a

COMP103 - Linked Lists (Part A)10 Linked List Order Nodes in a linked list has an order Nodes are usually ordered by a key, such as studentID Unlike arrays, nodes in a linked list may not be stored in neighborhood memory cells The first node of a list is kept by a head pointer (pHead), which must be kept safely The last node of a list is a node that contains a NULL value in its link part

COMP103 - Linked Lists (Part A)11 Linked Lists: Basic Operations Transverse: visit each item in the list (useful for printing the list) Search for an item in the list Add an item to the list Delete an item from the list

COMP103 - Linked Lists (Part A)12 Linked List Traversal traverse (list) 1. Set pointer (pWalker) to the first node (pHead) in list 2. while (not end of the list) 2.1 process (current node, pWalker) 2.2 set pointer to next node end traverse pWalker When pWalker becomes NULL, it means that the end of the list is reached

COMP103 - Linked Lists (Part A)13 Linked List Traversal // Other statements NODE *pWalker; pWalker = pHead; while (pWalker) { cout data << endl; pWalker = pWalker->link; // points to next node } pHead

COMP103 - Linked Lists (Part A)14 Search for an item (target) in the list target is in the list  The search function returns true pCur points to the node which contains target pPre points to the preceding node 2 cases: (i) pPre==NULL, (ii) pPre!=NULL

COMP103 - Linked Lists (Part A)15 Search for an item (target) in the list target is NOT in the list  The search function returns false pCur points to the node which should be after target pPre points to the node which should be before target 3 cases: (i) pPre==NULL, (ii) pCur==NULL, (iii) pPre!=NULL AND pCur!=NULL

COMP103 - Linked Lists (Part A)16 Algorithm for searching pPre = NULL; pCur = pHead; // Search until target <= list data key while (pCur && (target > pCur->data.key)) { pPre = pCur; pCur = pCur->link; } if (pCur && (target == pCur->data.key)) found = true; else found = false; return found;

COMP103 - Linked Lists (Part A)17 Add an item: Array vs. Linked List Array: it involves many copying operations (Remember: Data must be ordered!) Peter Mary John Peter Ada Mary John Ada Peter956498Mary956894John955992Ada For linked list, no copying operation is necessary Data can be anywhere in memory Only links are altered

COMP103 - Linked Lists (Part A)18 Add an item, i.e. adding a Node Item to be added should NOT be in the list,  Search for this item will return a false value pPre points to the item “less than” the one to be added pCur points to an item “greater than” the one to be added Two cases:

COMP103 - Linked Lists (Part A)19 Add an item to an Empty List pHead==NULL, means the list is empty pPre==NULL, means we are adding to an empty list, OR at the beginning of a list

COMP103 - Linked Lists (Part A)20 Add an item at Beginning of the list Now, pHead!=NULL, so the list is not empty Also, pPre==NULL, so we are adding at the beginning of the list The code is identical!

COMP103 - Linked Lists (Part A)21 Add an item in Middle of the list pPre != NULL AND pPre->link != NULL, so we are adding in the middle of the list

COMP103 - Linked Lists (Part A)22 Add an item at the End of the list pPre != NULL AND pPre->link == NULL, so we are adding at the end of the list The code is identical to that of adding to the middle of the list

COMP103 - Linked Lists (Part A)23 Add an item to the list Node *pNew; pNew = new Node; // allocate memory to new node if (!pNew) exit(0); // Memory overflow pNew->data = item; if (pPre == NULL) { pNew->link = pHead; // add before the first node pHead = pNew; // or to an empty list } else { pNew->link = pPre->link; // add in the middle or pPre->link = pNew; // at the end }

COMP103 - Linked Lists (Part A)24 Delete an item (the first one) from the list Item to be deleted should be in the list  Search returns a True value; pCur points to the item; and pPre points to the one before item. pPre == NULL, means we are deleting the first node

COMP103 - Linked Lists (Part A)25 Delete - General Case pPre->link = pCur->link delete (pCur); pPre->link = pCur->link; delete (pCur); pPre!=NULL for all other cases

COMP103 - Linked Lists (Part A)26 Delete a Node from a linked list // Delete a node from a linked list // Other Statements if (pPre == NULL) pHead = pCur->link; // delete first node else // delete other node pPre->link = pCur->link; delete (pCur); // return memory

COMP103 - Linked Lists (Part A)27 Next Topic: Linked List Design We have completed the basic operations in a linked list (Part A). The last topic in the course (Part B of the notes) is to implement linked list as objects. To be covered in May after OO design and ADT.

COMP103 - Linked Lists (Part A)28 Go to Part B