Rossella Lau Lecture 3, DCO20105, Semester A,2005-6 DCO 20105 Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

CHP-5 LinkedList.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Kernighan/Ritchie: Kelley/Pohl:
Linked Lists
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
Rossella Lau Lecture 11, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 11: Template and Operator overload  Template.
Rossella Lau Lecture 5, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
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.
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Rossella Lau Lecture 2, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 2: Vector  Array and vector  Internal structure.
Rossella Lau Lecture 4, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 4: C++ and list  Usage of Vector and List  C++
1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.
Rossella Lau Lecture 1, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 1: Introduction What this course is about:  Data.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming 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.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 Data Structures and Algorithms Outline This topic will describe: –The concrete data structures that can be used to store information –The basic forms.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
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.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure and Algorithms
Chapter 4 Linked Lists.
Lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Lists.
Chapter 18: Linked Lists.
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Data Structures & Algorithms
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Rossella Lau Lecture 3, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 3: Basics of Linked List  C++ pointer revision  Basics of a linked list -- By Rossella Lau

Rossella Lau Lecture 3, DCO20105, Semester A, A reason for using a Linked List  Array  It allows storing of multiple occurrences of items consecutively  The number of items should be specified ahead  Linked list  As an array may occupy too many spaces or may be too small to store all items, a linked list allows storing of the exact number of items  Storage for an item is allocated dynamically on demand  Example Program List.h

Rossella Lau Lecture 3, DCO20105, Semester A, C++ pointer  Revision: slide 6-21, Lecture 8 in DCO10105  Another point of view: Ford’s Slides: 5: 2-14

Rossella Lau Lecture 3, DCO20105, Semester A, An example of pointer and array  Ford’s revision exercise: 5.2: What are the final values in the array arr?  int arr[] = {45, 24, 16, 3, 2, 8};  int *parr = arr;  *parr = 3;  parr +=4;  *parr = 5;  parr = parr - 3;  *parr = 15;  parr[4] *= 2; Final values in the array arr = { ____, ____, ____, ____, ____, ____}

Rossella Lau Lecture 3, DCO20105, Semester A, Dynamic storage requests for items  When an item needs to be stored, new is used, and a pointer is needed to point to the new item  When a second item is required to be stored, new is used again. An additional pointer is needed.  It is unwise to use an array to store such pointers – as we cannot determine the size of the array ahead item1 ptr item2next  Therefore, the pointer which points to the second element is better stored with the first element.

Rossella Lau Lecture 3, DCO20105, Semester A, Nodes of a Linked list  When more items are requested, one can see elements are linked as a list.  To formalize all items in the linked list, a null pointer (value 0) is added to the last item. 0 nextitem1 ptr item2next itemn … itemnext  Now, each node (item + next) has the same format

Rossella Lau Lecture 3, DCO20105, Semester A, Typical structures of a Linked List template class Node { T item; Node *next; friend class List ; }; template class List { Node *head, *tail; }; head tail nextitem10itemN ……

Rossella Lau Lecture 3, DCO20105, Semester A, C++ Notes – friends  Friend classes can access the private members of the class which is declaring  Node and List are hand by hand classes but should be implemented as two independent classes  In order to allow for class List to direct use the private members in Node, List is declared as a friend class inside class Node  Similarly, a class may declare its friend function(s)  However, friends should be avoided as much as possible because they are contradicted with encapsulation!

Rossella Lau Lecture 3, DCO20105, Semester A, C++ notes on header and implementation source  Traditional C uses a header file (.h) to define data members and function prototypes and implementation codes (.cpp) will be written in another file  The “.h” should be included if a program uses the class, and lines in “.h” file should be compiled again and again  The “.cpp” is only required to be compiled once and allow for “link” up during link time after the code compilation.  It saves time to compile for the “.cpp” file  C++ uses only one header file (.h) to define a template classe since a template’s type is not determined until the compilation time.  It cannot save time by separating the header and implementation  It sacrifices compilation time for flexibility

Rossella Lau Lecture 3, DCO20105, Semester A, Typical operations of a linked list node  Creation of a node: nodePtr = new Node(item);  Member identification of a node: nodePtr  item  Care should be taken to avoid identification from a null pointer or a pointer without properly being assigned a value  Nodes “linked” up: node1Ptr  next = node2Ptr;  List pointer assignment: head = firstNodePtr;

Rossella Lau Lecture 3, DCO20105, Semester A, Typical operations of a linked list  push_back() -- append an item at the end  push_front() -- insert an item at the front  find() -- a linear search  insert() -- insert an item before or after a node and usually performed after a find()  remove() -- remove an item

Rossella Lau Lecture 3, DCO20105, Semester A, Examples of push_back() and push_front() push_back(10), next10 headtail next20 Initial : headtail (null) Initial : headtail (null) push_front(10), next10 tailhead next20 push_back(20) push_front(20),

Rossella Lau Lecture 3, DCO20105, Semester A, An example of insert_before() next10 headtail next20 Initial : Insert_before(20, 15) next10 head tail next20 next15 prev curr

Rossella Lau Lecture 3, DCO20105, Semester A, Examples of remove() next 7 9 head next 4 6 delete 9: head next delete 6: next 7 head next 4 6 prevcurr next 9 curr next 6

Rossella Lau Lecture 3, DCO20105, Semester A, Pointer traversal on a linked list  A typical traversal of an array: for (size_t i =0; i != SIZE; i++) { array[i]......}  As head points to the first element, all the items can be traversed through the link, i.e., Node *next. Treating head similar to a[0], iteration through  next similar to i++, null pointer similar to the end of an array, a typical traversal on linked list: for (Node * it=head; it; it=it->next) { it->item......} Example in List.h: search()

Rossella Lau Lecture 3, DCO20105, Semester A, Pointer “look ahead” traversal  The above loop is simple but would end up with the iterating pointer pointing to null  We may also use a look ahead traversal method to examine contents of a node from its previous node on the linked list for (Node * it=head; it->next; it=it->next) { it->next->item } It is used more often and examples are List.h: print(), find()

Rossella Lau Lecture 3, DCO20105, Semester A, Summary  A pointer stores an address and can apply de- referencing and object member identification  Pointer is a powerful but also a dangerous feature  Linked list is a typical dynamic memory structure in which it uses pointers to link up its nodes  Nodes on a linked list are created on demand and are more space efficient than an array in which unwanted space is usually presented

Rossella Lau Lecture 3, DCO20105, Semester A, Reference  Ford:  STL online references    Example programs: List.h, testList.cpp -- END --