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

Slides:



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

Linked Lists Geletaw S..
Lists CS 3358.
Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
CHP-5 LinkedList.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
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.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
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.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
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.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked List, Stacks Queues
Chapter 16: Linked Lists.
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Unit – I Lists.
Pointers and Linked Lists
Lectures linked lists Chapter 6 of textbook
Data Structure Interview Question and Answers
Data Structures and Algorithms
Chapter 4 Linked Lists.
Linked lists.
Data Structures Interview / VIVA Questions and Answers
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Data Structures and Algorithms
Chapter 4 Unordered List.
LAB#4 Linked List.
Linked Lists.
Data Structures & Algorithms
Linked lists.
Presentation transcript:

Chapter 1 Object Oriented Programming

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.

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.

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

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 can’t be the address of any location, it should be a null address, which is simply 0.

Chapter 3 Linked Lists

Linked list An array is a very useful data structure in programming languages. However, it has at least two limitations that can be overcome by using linked structure. LL is a collection of nodes storing data and links (using addresses) to other nodes. 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 structure’s 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.

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.

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

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);

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. 1. If you missed a node in the chain, then a wrong assignment is made 2. The flexibility of using linked lists is diminished. 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).

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

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

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

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

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.

3.1.2 Deletion (cont’) 1. 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.

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.

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 node’s 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.

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

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.

3.2 Doubly Linked Lists 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.

To insert a node to the end of integer-doubly linked list : 1. A new node is created, and then its three data members are initialized ( info member to a number, next member to null, and the 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

To delete a node from the end of integer-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. Note : before deleting, check if the list is empty.

3.3 Circular Lists 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.

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.

Insert a node at the front of CSLL Insert a node at the end of CSLL

A circular doubly linked list can avoid the implementation problems in deletion and insertion to circular singly linked list