Linked lists CSCI 2170.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Stacks, Queues, and Linked Lists
Linked Lists Definition of Linked Lists Examples of Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
CHP-5 LinkedList.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Data Structures: A Pseudocode Approach with C
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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,
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
 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.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
CS2006- Data Structures I Chapter 5 Linked Lists I.
Data Structure Dr. Mohamed Khafagy.
Data Structures and Algorithms
Dr. Bernard Chen Ph.D. University of Central Arkansas
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Data Structures Linked list.
Chapter 4 Linked Lists
Chapter 20: Binary Trees.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Chapter 4 Linked Lists.
Linked List (Part I) Data structure.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Data Structures and Algorithms
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Data Structures & Algorithms
Queues Definition of a Queue Examples of Queues
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Presentation transcript:

Linked lists CSCI 2170

Definitions ‘b’ ‘a’ ‘b’ ‘c’ ‘d’ NULL node (linked) list is a data structure for efficient dynamic data storage node - element of a list data part - holds information contained in the list pointer (reference) part - a pointer to type(class) node nodes are allocated dynamically list is formed by having the reference part of one node point to the next node head (node) - first node in the list tail (node) - last node in the list the reference part of the tail points to NULL data part pointer part (linked) list ‘a’ ‘b’ ‘c’ ‘d’ NULL head (node) tail (node) CSCI 2170

List manipulation ptr ptr ptr ptr ‘a’ ‘b’ ‘c’ ‘d’ NULL since head points to the next node and (transitively) to all the other nodes, all the information necessary to get the list data is a pointer to the head list traversal - going through list elements to collect information on the list’s structure or data stored to traverse 1. allocate pointer variable for traversal (ptr) 2. assign address of head (from pointer to head) to ptr 3. look up the pointer part of node and assign it to ptr 4. Repeat step 3 until NULL is encountered since nodes are allocated dynamically, they can be removed and added to the list with only minimum modifications required ptr ptr ptr ptr ‘a’ ‘b’ ‘c’ ‘d’ NULL head (node) tail (node) pointer to head CSCI 2170

Review what is a list? what is a node? what does data part of node contain? what does the reference part of node contain? what is the head of a list? what is the tail of a list? what does the reference part of the tail of the list points to? what is list traversal? why would you want to traverse a list? CSCI 2170

Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations of Stacks, Sets, etc. CSCI 2170

Definition of Linked Lists A linked list is a sequence of items (objects) where every item is linked to the next. Graphically: data head_ptr CSCI 2170

Definition Details Each item has a data part (one or more data members), and a link that points to the next item One natural way to implement the link is as a pointer; that is, the link is the address of the next item in the list It makes good sense to view each item as an object, that is, as an instance of a class. We call that class: Node The last item does not point to anything. We set its link member to NULL. This is denoted graphically by a slash in the link. CSCI 2170

Examples of Linked Lists (A Waiting Line) A waiting line of customers: John, Mary, Dan, Sue (from the head to the tail of the line) A linked list of strings can represent this line: John Mary Dan Sue head_ptr CSCI 2170

Examples of Linked Lists (A Stack of Numbers) A stack of numbers (from top to bottom): 10, 8, 6, 8, 2 A linked list of ints can represent this stack: 10 8 6 8 2 head_ptr CSCI 2170

Examples of Linked Lists (A Set of Non-redundant Elements) A set of characters: a, b, d, f, c A linked list of chars can represent this set: a b d f c head_ptr CSCI 2170

Examples of Linked Lists (A Sorted Set of Non-redundant Elements) A set of characters: a, b, d, f, c The elements must be arranged in sorted order: a, b, c, d, f A linked list of chars can represent this set: a b c d f head_ptr CSCI 2170

Examples of Linked Lists (A Polynomial) A polynomial of degree n is the function Pn(x)=a0+a1x+a2x2+…+anxn. The ai’s are called the coefficients of the polynomial The polynomial can be represented by a linked list (2 data members and a link per item): a0,0 a1,1 a2,2 an,n head_ptr CSCI 2170

Operations on Linked Lists Insert a new item At the head of the list, or At the end of the list, or Inside the list, in some designated position Search for an item in the list The item can be specified by position, or by some value Delete an item from the list Search for and locate the item, then remove the item, and finally adjust the surrounding pointers size( ); isEmpty( ) CSCI 2170

Insert– At the Head A Insert a new data A. Call new: newPtr List before insertion: After insertion to head: data data data data head_ptr A data head_ptr The link value in the new item = old head_ptr The new value of head_ptr = newPtr CSCI 2170

Insert – at the Tail A Insert a new data A. Call new: newPtr List before insertion After insertion at end: data data data data head_ptr data A head_ptr The link value in the new item = NULL The link value of the old last item = newPtr CSCI 2170

Insert – inside the List data Insert a new data A. Call new: newPtr List before insertion: After insertion in 3rd position: data data data data head_ptr data data A data data head_ptr The link-value in the new item = link-value of 2nd item The new link-value of 2nd item = newPtr CSCI 2170

Delete – the Head Item List before deletion: List after deletion of the head item: data data data data data head_ptr data data data data data head_ptr The new value of head_ptr = link-value of the old head item The old head item is deleted and its memory returned CSCI 2170

Delete – the Tail Item List before deletion: List after deletion of the last item: data data data data data head_ptr data data data data data head_ptr New value of link value of the next to last item New link value of new last item = NULL. CSCI 2170

Delete – an inside Item List before deletion: List after deletion of the 2nd item: data data data data data head_ptr data data data data data head_ptr New link-value of the item located before the deleted one = the link-value of the deleted item CSCI 2170

size() and isEmpty() We need to scan the items in the list from the head_ptr to the last item marked by its link-value being NULL Count the number of items in the scan, and return the count. This is the size(). Alternatively, keep a counter of the number of item, which gets updated after each insert/delete. The function size( ) returns that counter If head_ptr is NULL, isEmpty() returns true; else, it returns false. CSCI 2170

Searching for an Item Suppose you want to find the item whose data value is A You have to search sequentially starting from the head item rightward until the first item whose data member is equal to A is found. At each item searched, a comparison between the data member and A is performed. CSCI 2170

Time of the Operations Time to search() is O(L) where L is the relative location of the desired item in the List. In the worst case. The time is O(n). In the average case it is O(N/2)=O(n). Time for remove() is dominated by the time for search, and is thus O(n). Time for insert at head or at tail is O(1). Time for insert at other positions is dominated by search time, and thus O(n). Time for size() is O(1), and time for isEmpty() is O(1) CSCI 2170

Implementation of an Item Each item is a collection of data and pointer fields, and should be able to support some basic operations such as changing its link value and returning its member data Therefore, a good implementation of an item is a class The class will be called Node CSCI 2170

Class Node Design for Item The member variables of Node are: The data field(s) The link pointer, which will be called next The functions are: Function Action Why Needed getNext( ) returns the link. for navigation getData( ) returns the data for search setNext(Node *ptr) sets link to ptr for insert/delete setData(type x) sets data to x. to modify data contents CSCI 2170

Class Node Type class Node { private: int data; // different data type for other apps Node *next; // the link pointer to next item public: Node(int x=0;Node * ptr=NULL); // constructor int getData( ); Node *getNext( ); void setData(int x); void setNext(Node *ptr); }; CSCI 2170

Class Node Implementation Node::Node(int x, Node *p){ data=x; next=p;}; int Node::getData( ){return data;}; Node * Node::getNext( ){return next;}; void Node::setData(int x) {data=x;}; void Node::setNext(Node *ptr){next=ptr;}; CSCI 2170

Implementation of Linked List A linked list is a collection of Node objects, and must support a number of operations Therefore, it is sensible to implement a linked list as a class The class name for it is List CSCI 2170

Class Design for List The member variables are: Member functions Node *head_ptr; Member functions Node * search(int x); Node * itemAt(int position); void removeHead(); void removeTail(); void remove(int x); void insertHead(int x); void insertTail(int x); void insert(Node *p, int x) // inserts item after the item // pointed to by p int size( ); Node *getHead( ); Node getTail( ); bool isEmpty( ); CSCI 2170

Class List Type class List { private: Node *head_ptr; public: List( ); // constructor int size( ); Node *getHead( ); Node *getTail( ); bool isEmpty( ); Node *search(int x); Node *itemAt(int position); void removeHead(); void removeTail(); void remove(int x); // delete leftmost item having x void insertHead(int x); void insertTail(int x); void insert(Node *p, int x); }; CSCI 2170

Implementation of Class List List::List( ){head_ptr= NULL}; int List::size( ){return numOfItems;}; Node * List::getHead( ) {return head_ptr;}; Node * List::getTail( ) {……..}; bool List::isEmpty() {return (!head_ptr);}; CSCI 2170

Implementation of search( ) Node *List::search(int x){ Node * currentPtr = getHead( ); while (currentPtr != NULL){ if (currentPtr->getData( ) == x) return currentPtr; else currentPtr = currentPtr->getNext(); } return NULL; // Now x is not, so return NULL }; CSCI 2170

Implementation of itemAt( ) Node *List::itemAt(int position){ …………………… return currentPtr; }; CSCI 2170

Implementation of removeHead( ) void List::removeHead( ){ …..what if head is null…..??? Node * currentPtr = head_ptr( ); head_ptr=head_ptr->getNext( ); delete currentPtr; }; CSCI 2170

Implementation of removeTail( ) void List::removeTail( ){ ………………. }; CSCI 2170

Implementation of remove( ) void List::remove(int x){ ………………… }; CSCI 2170

Implementation of insertHead( ) void List::insertHead(int x){ Node * newHead = new Node(x,head_ptr); head_ptr= newHead; }; CSCI 2170

Implementation of insertTail( ) void List::insertTail(int x){ ……………… }; CSCI 2170

Implementation of insert( ) // inserts item x after the item pointed to by p void List::insert(Node *p, int x){ ……………………….. }; CSCI 2170