Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
CSE Lecture 12 – Linked Lists …
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
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.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
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.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Xiaoyan Li, CSC211 Data Structures Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Pointers OVERVIEW.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Linked List by Chapter 5 Linked List by
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lists Chapter.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
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.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
CSCI-255 LinkedList.
Linked Lists Chapter 6 Section 6.4 – 6.6
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
Programming Abstractions
Chapter 4 Linked Lists
LinkedList Class.
The Bag and Sequence Classes with Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 4 Linked Lists.
Linked List (Part I) Data structure.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Programming Abstractions
Chapter 4 Linked Lists.
Lecture 14 Linked Lists CSE /26/2018.
Data Structures & Algorithms
Data Structures & Programming
Presentation transcript:

Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects

2 Overview  Linked List Class Definition Add Node Delete Node Destructor  Namespace

3 Linked List  A linked list is a collection of data.  Each element of the linked list has Some data A link to the next element  The link is used to chain the data, usually they are pointers.  Example: A linked list of integers: Data Link

4 Linked List  A linked-list is a useful structure to hold a collection of data.  A linked-list is dynamic. It can grow and shrink to any size.  It can be implemented using dynamic class (dynamic objects). Dynamic objects are objects created and deleted by ‘ new ’ and ‘ delete ’.  The main task of a linked list class is to maintain the chain (pointer) between the items it holds.

5 Linked List  Original linked list of integers:  Insertion, add(60):  Deletion, delete(45): Old pointer Rearrange pointers

6 Linked List – class definition  This is the definition of class ListNode (holding data).  Pay attention to the techniques we have learned before. class ListNode { public: // no default constructor, user must call the // constructor explicitly ListNode(int i_data, ListNode* i_next=NULL): data(i_data), next(i_next) {} friend class List; // no destructor, let List class // handles memory deallocation (delete) private: int data; ListNode* next; };

7 Linked List – class definition  This is the definition of class List (organizing data). class List { public: List() : head(NULL), tail(NULL), num_node(0) {} ~List(); void print(); bool addNode(int new_data); bool deleteNode(int del_data); int getSize() { return num_node; } private: ListNode* head; ListNode* tail; int num_node; };

8 Linked List – add node  To insert a new node: Create a new node with ListNode* temp = new ListNode(new_data); Update the tail pointer, move its next pointer to the new node. Move the tail pointer to the new end.  In our example, new nodes are added to the end of list. Full code are showed in main.cpp, linked.h and linked.cpp  You can try to add nodes to the head or according to the number sequence. The procedure is similar.

9 Linked List – delete node  To delete a node: Locate the node to be deleted  temp points to the node.  prev points to its predecessor Disconnect node from list using: prev->next = temp->next; Delete unnecessary node, return memory to system: delete temp;  Be careful when deleting in data in special location, e.g. head. Depends on your implementation, sometime tail is another special case too.

10 Linked List - destructor  ListNode class don’t have a destructor, because it doesn’t have any dynamic data member.  But List class has dynamic members, therefore we have to write a destructor for it. List::~List() { ListNode* temp = head; while (head != NULL) { head = head->next; delete temp; temp = head; }  Pay attention to the terminating condition when you use this kind of loops. They will easily get wrong.

11 Comparison between Array and Linked List  Static Array: Size are fixed at compilation time. Elements can be random accessed.  Binary search is efficient when elements can be random accessed. Elements are continuously stored.  Dynamic Array: Size are not fixed at compilation time.  You may resize the array, but copying is needed. A slow process. Elements can be random accessed. Delete an element also needs copying. Elements are continuously stored.

12 Comparison between Array and Linked List  Linked List: Size are not fixed at compilation time. You may resize the linked list any time, no copying is needed.  Elements can only be sequentially accessed. Slow to use binary search. Easy to delete an element. Elements are not continuously stored.  Slow when caching is considered.

13 Namespace  Suppose you want to use two libraries, but some names collide, what can you do? // file: classA.h// file: classB.h class Service {…}; class ClassB {…}; class ClassA {…}; class Service {…}; int del_all();  Even if you don't use “Service” and “del_all”, you run into trouble: the compiler will complain about multiple definitions of “Service” the linker may complain about multiple definitions of “del_all()”

14 Namespace  Use namespace can solve multiple names problem. // file: classA.h namespace service_class_A { class Service {…}; class ClassA {…}; int del_all(); } // file: classB.h namespace service_class_B { class Service {…}; class ClassB {…}; int del_all(); }

15 Namespace  You can refer to names in a namespace with ::, it is called scope resoultion operator. service_class_A::del_all(); service_class_B::Service serB;  If the namespace is too long, you can define your own namespace alias, namespace cA = service_class_A; namespace cB = service_class_B; cA::del_all(); cB::Service serB;

16 Namespace  You can also import the whole namespace by a “using” declaration.  You can import a single name, using service_class_A::del_all;  Everytime you use del_all(), it will refer to the del_all() of “classA.h”

17 Namespace  Or you can import the whole namespace, using namespace service_class_B;  Everytime you use del_all() or Service class, they will refer to the one in “classB.h”.  One famous example is “ using namespace std; ”  Of course, after you imported the whole namespace, you can specify the whole name if you do want to use other functions, e.g. using namespace service_class_B; service_class_A::del_all();

18 Namespace  You can also bring all the names of a namespace into your program at once, but make sure it won't cause any ambiguities. namespace cA = service_class_A; namespace cB = service_class_B; using namespace cA; using namespace cB; Service ser_K;// error: ambiguous classB gun_dum;// ok, no problem.  If the above ambiguity is removed, the program above can be compiled.

19 Namespace  The following will cause error too, using service_class_A::Service; using service_class_B::Service;  Do you know why?