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.

Slides:



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

Chapter 17 Linked Lists.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
CSEB324 Data Structures & Algorithms
CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
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.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Data Structures Using C++ 2E
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
Pointers OVERVIEW.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
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.
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.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
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.
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.
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.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
LINKED LISTS.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Chapter 16: Linked Lists.
Computer Organization and Design Pointers, Arrays and Strings in C
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Linked lists.
Pointers Revisited What is variable address, name, value?
Dynamic Memory Allocation
Arrays and Linked Lists
Data Structures & Algorithms
Data Structures & Algorithms
Data Structures and Algorithms Memory allocation and Dynamic Array
Linked lists.
Presentation transcript:

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 due to the static nature of arrays i.e. the array size needs to be known by the compiler at compile-time.

A better implementation would employ a dynamic structure to represent the list i.e. a structure that expands and contracts as the list grows and shrinks. Such an implementation is possible using pointers and dynamic memory allocation in programming languages. This type of implementation is known as a linked list.

Pointers Pointers Revisited A pointer variable is a variable that contains a memory address. The size of memory that the pointer points to depends on the type of the pointer i.e. if a pointer points to a floating point number in memory the pointer must be of type float. e.g. float x=7.6; float *p = &x;

7.6 variable x memory address &375 &375 variable p Memory p contains the address of x in memory

For performance reasons it is much more efficient to pass pointers around a computer system than copies of the data it is pointing to. This is particularly important for large structures in procedural programming languages or objects in object-oriented programming languages. In these cases we almost always use pointers to the structures or objects e.g.

class student : public person { int ID; …public:student();…}; int main(void) { student *p = new student …} Name Age ID student() ….. p &476 Memory memory address &476

Linked Lists Linked List ADT The central notion to linked list implementations is that of the node. A node consists of an element to be stored and a pointer to the next node in the list e.g. element a node

The linked list L is simply a pointer to the first node in the list ( if it is an empty list L is NULL ) The last node in the list has a NULL pointer to indicate that no more nodes follow i.e. the list has terminated. element LNULLempty list L element NULL a list of length 2

6.5 L NULL Another Example

To traverse the list we only need to follow the pointers through the list until we reach the last node (NULL pointer). List Insertion To insert a new element in the list we need to create a new node and place it in the correct position in the linked list. To find the correct position in the list we first traverse to the node one before the position we wish to insert at e.g.

to insert a new element in position 3 we traverse the list L to position 2 i.e. the third node in the list ( remember we start from position 0 ) 5.6 L NULL position 3 traverse to position 2 position 0

To insert the new node we need to re-assign the next node pointers: 1. The next node pointer in the new node should be identical to the next node pointer of the node we stopped traversing at. 2. The next node pointer of the node we stopped traversing at should be assigned the address of the new node being inserted.

e.g. assume we wish to insert a new node with memory address b at position 3 below (with node memory address a) 10.4 a NULL position b new node

We insert the new node by performing the following node re-assignments; Simply the next node pointer in the node we stopped traversing at is copied into the new node next pointer and the address of the new node is copied into the next node pointer of the node we stopped traversing at b NULL 77.4 a

You should be careful that the node re-assignment is performed in the correct order i.e. the next node pointer of the node stopped at isn’t overwritten before it is copied across to the new node next node pointer. Inserting at position 0 The above algorithm for node insertion will work for all nodes but the first.

Position 0 insertion is a special case that needs to be handled differently by the algorithm e.g. suppose we wish to insert a new element 2.3 at position L new node position 0

To insert the new node we point the new node to the address pointed to by the list L and then point L to the new node; 6.5 L new node

List Deletion Node deletion also involves traversing of the list to the position one before the position to be deleted. To delete the node we simply re-assign the pointer at the node we stopped traversing at with the next node pointer of the node to be deleted.

6.5 L NULL e.g. suppose we wish to delete the element at position 1 below delete this node

6.5 L NULL memory leak

By directly re-assigning pointers during node deletion we get memory leaks i.e. the node to be deleted still remains in memory but is not pointed to which results in wasted resources. To avoid memory leaks we should always perform garbage collection i.e. de-allocate unused memory. In linked lists this can be achieved by using a temporary pointer to the node to be deleted.

6.5 L NULL temp

The temporary pointer to the node to be deleted is created before we re-assign any pointers (otherwise we would lose the ability to reference the deleted node) As soon as the deleted node has been by-passed we can de-allocate it using the temporary pointer (remember deleting dynamic memory in most programming languages requires the address of the memory to be deleted).

Linked List Implementation The following is a class declaration and constructor for a linked list in object-oriented C++; class Node { private: float element; Node *next; public: Node(float); float getElement(); void setElement(float); Node *getNext(); void setNext(Node *); … };

Node::Node(float f) : element(f), next(NULL) {} Node::Node(float f) : element(f), next(NULL) {} It is important that you familiarise yourself with the code and the implementations of the main linked list operations.