Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Chapter 25 Lists, Stacks, Queues, and Priority Queues
TK1924 Program Design & Problem Solving Session 2011/2012
TK1924 Program Design & Problem Solving Session 2011/2012
Linked Lists Geletaw S..
1
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
Chapter 3: Linked List Tutor: Angie Hui
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
Lecture 15 Linked Lists part 2
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Turing Machines.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Topic 12 The List ADT. 9-2 Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
Linked Lists Chapter 4.
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Data Structures Using C++
Data Structures Part 2 Stacks, Queues, Trees, and Graphs Briana B. Morrison CSE 1302C Spring 2010.
Double-Linked Lists and Circular Lists
CHP-5 LinkedList.
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSE Lecture 12 – Linked Lists …
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Review Pseudo Code Basic elements of Pseudo code
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Analyzing Genes and Genomes
Types of selection structures
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
Essential Cell Biology
PSSA Preparation.
Essential Cell Biology
Immunobiology: The Immune System in Health & Disease Sixth Edition
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Physics for Scientists & Engineers, 3rd Edition
Energy Generation in Mitochondria and Chlorplasts
Data Structures Using C++ 2E
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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.
Presentation transcript:

Chapter 1 Object Oriented Programming 1

OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques may include features such as Data abstraction. Encapsulation. Polymorphism. Inheritance. Many modern programming languages now support OOP. 2

Abstract data types An outline of the program containing its requirements should precede the coding process for a project. Then, in the later stage, the implementation may start with specific data structure. First, specify each task in terms of inputs and outputs. Be concerned with what the program should do. For example, if an item is needed to accomplish some tasks, the item is specified by the operations performed on it not by its structure. When the implementation starts, it decides which data structure to use to make the execution more efficient. 3

An item specified in terms of operations is called an abstract data type. ADT- An abstract data type can be defined by the operations that are done on the data regardless of its type. ADT- A set of data values and associated operations that are precisely specified independent of any particular implementation. ADT- Mathematical description of an object with set of operations on the object 4

Some information before starting You already learned that addresses of variables can be assigned to pointers. However, pointers can refer to unknown locations that are accessible only through their addresses not by names. These locations must be set by the memory manager dynamically during the run of the program. To dynamically allocate and de-allocate memory, two functions are used: 1. new – takes from memory as much space as needed to store an object. Ex : p = new int; 2. delete – return the space that is accessible from p and is no longer needed. Ex : delete p; Note : an address has to be assigned to a pointer, if it cant be the address of any location, it should be a null address, which is simply 0. 5

Chapter 3 Linked Lists 6

Linked list An array is a very useful data structure in programming languages. However, it has at least two limitations: Fixed size: the size has to be known at compile time data is separated in the computer by the same distance. Insertion is a problem (shifting is required) that can be overcome by using linked structure. Linked List (LL): is a collection of nodes storing data and links (using addresses) to next nodes. 7

Linked list A linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence. The most flexible linked structures implementation is by using pointers. Linked lists are among the simplest and most common data structures; they provide an easy implementation for several important abstract data structures, including stacks, queues and arrays. 8

3.1 Singly Linked Lists Linked list – a data structure that composed of nodes, each node holding some information and a pointer to another node in the list. Singly linked list – a node has a link only to its successor in the sequence of nodes in the list. Note : Only one variable is used to access any node in the list. The last node on the list can be recognized by the null pointer. 9

Example : class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int i, IntSLLNode *ptr = 0) { info = i; next = ptr; } int info; IntSLLNode *next; }; 10

Now, how to create the linked list 1. Create a new node by executing the declaration and assignment : IntSLLNode *p = new IntSLLNode(10); This statement create a node on the list and make p points to it. The constructor assigns the number 10 to the info member of this node. The constructor assigns null to its next member. 2. Then any new node is included in the list by making the next member of the first node a pointer to the new node. 3. The second node is created by : p -> next = new IntSLLNode (8); 11

Where p -> next is the next member of the node pointed to p (Figure 3.1d). As before four steps are executed: 1. New node is created (Figure 3.1e) 2. The constructor assign the number 8 to the info member of this node (Figure 3.1f) 3. The constructor assign null to the next member. (Figure 3.1g). 4. The new node is included in the list by making the next member of the first node a pointer to the node (Figure 3.1h). p -> next -> next = new IntSLLNode (50); 12

FIGURE

Problem : The longer the linked list, the longer the chain of next s to access the nodes at the end of the list when using pointers. In this example p -> next -> next -> next allow us to access the next member of the 3 rd node on the list. 1. If you missed a node in the chain, then a wrong assignment is made 2. The flexibility of using linked lists is diminished. 14

So, other ways of accessing nodes are needed. One of them is : To keep two pointers: one to the first node(Head), and one to the last(Tail). Returen to figure 3.2 page 79. Figure 3.3 a singly list of integers 15

16

Operations 17 Insert At the Beginning (before Head) At End (after Tail) middle Delete At the Beginning (before Head) At End (after Tail) middle Special cases: Empty List, single-node list Search

3.1.1 Insertion 1. inserting a new node at the beginning of a singly linked list 18 Figure 3.4

3.1.1 Insertion (cont) 2. inserting a new node at the end of a singly linked list 19 Figure 3.5

Insert At the beginning Insert at the end addToTail(int el) { if (tail != 0) { // if list is not empty; tail->next = new IntSLLNode(el); tail = tail->next; } else { // if list is empty; head = tail = new IntSLLNode(el); } } Complexity is O(1) addToHead(int el) { head = new IntSLLNode(el,head); if (tail == 0) // if list is empty tail = head; } Complexity is O(1) 20

3.1.2 Deletion 1. Deleting a node at the beginning of the list and returning the value stored in it. 21 Figure 3.6

There are two special cases to consider : 1. Attempt to remove a node from an empty linked list. 1. One way is to use assert statement( with the function isEmpty() ). 2. Or throw an exception by using throw clause( and having a matching try-catch clause). 2. The list has one node to remove 1. The list becomes empty and tail + head set to null. 22

3.1.2 Deletion (cont) 2. Deleting a node from the end of the list and returning the value stored in it. Problem : Tail has to be moved backward by one node? The predecessor has to be found, how? Use a temporary variable tmp that scans the list within for loop. 23

24 Figure 3.7

25 Cont.Figure 3.7

In removing the last node, the two special cases are the same as in deleteFromHead(). The list is empty or a single-node list becomes empty. (slide no. 21) 26

Delete from Head Delete from Tail int IntSLList::deleteFromTail() { int e1 = tail->info; if (head==tail) {// one node delete head; head = tail = 0; } else { IntSLLNode *tmp; for (tmp=head; tmp->next != tail; ) tmp = tmp->next; delete tail; tail=tmp; tmp->next=0; } return e1; } Complexity is O(n) int IntSLList::deleteFromHead() { int e1 = head->info; IntSLLNode *tmp= head; if (head==tail) // 1node only head = tail = 0; else head = head->next; delete tmp; return e1; } Complexity is O(1) 27

Finding and deleting a node with certain integer: In this case, a node is removed from the list by linking its predecessor to its successor. So, Either scan the list and then scan it again to find the nodes predecessor. Or use two pointers variable(ex, pred and tmp) using for loop so that they point to first and second nodes of the list. 28

29 Figure 3.8

30

Complexity of deleting a node with certain integer: 31

Other deletion cases : 1. Remove a node from an empty list – function is exited. 2. Delete a node from a one-node linked list - head =tail =null. 3. Remove the first node of the list – updating head. 4. Remove the last node of the list – updating tail. 5. Delete a node with a number that is not in the list – do nothing. 6. Last, the one we discussed, delete a node with a number that is in the list 32

3.1.3 Search The searching operation does not modify linked lists, it just scans the list to find a number. 1.If tmp is not null, el was found and true is returned. 2.If tmp is null, the search was unsuccessful and false is returned. 33

3.2 Doubly Linked Lists (DLL) To avoid singly linked list problem, the linked list is redefined so that each node in the list has two pointers, one to the successor and one to the predecessor. This list is called a doubly linked list. 34 Figure 3.9

Insert a node to the end of Doubly linked list : 1. A new node is created, and then its three data members are initialized : 1. info member to a number, 2. next member to null, 3. prev member to value of tail 2. tail is set to point to the new node 3. The next member of the predecessor is set to point to the new node Complexity is O(1) 35

36 Figure 3.11

Delete a node to the end of Doubly linked list: 1. A temporary variable (ex. el) is set to the value in the node. 2. tail is set to its predecessor. 3. The last node is deleted. 4. The next member of the tail node is set to null. 5. Return the copy of the object stored in the removed node. Complexity is O(1) Note : before deleting, check if the list is empty. 37

38 Figure 3.12 el = 10;

3.3 Circular Lists (CLL) A list where nodes form a ring ; each node has a successor. For example: Several processes are using the same resources for the same amount of time, and each process need a fair share of the resources. 39 Figure 3.13

Use only one permanent pointer, tail, to the list even though operations on the list require access to the tail and its successor, the head. 40 Complexity is O(1)

Insert a node at the front of CSLL Insert a node at the end of CSLL 41 Figure 3.14

Deletion of a node requires a loop to reach predecessor of Tail - O(n) A circular doubly linked list can avoid this problems in deletion and insertion to circular singly linked list and complexity becomes O(1) 42 Figure 3.15

43 OperationsSLLDLLCLL Insert At Beginning (before Head)O(1) At End (after Tail)O(1) middle Delete At Beginning (before Head)0(1) At End (after Tail)O(n) O(1)O(n) middleO(n) SearchO(n) Complexity Analysis Complete the missing cells