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.

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists Definition of Linked Lists Examples of Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Linked lists CSCI 2170.
CHP-5 LinkedList.
Ceng-112 Data Structures I Chapter 5 Queues.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Data Structures: A Pseudocode Approach with C
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Class 3: Linked Lists. cis 335 Fall 2001 Barry Cohen What is a linked list? n A linked list is an ordered series of ‘nodes’ n Each node contains some.
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.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
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 &
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Queues.
Programming Practice 3 - Dynamic Data Structure
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
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,
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Week 4 - Wednesday.  What did we talk about last time?  Started linked lists.
 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:
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.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
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.
Review Array Array Elements Accessing array elements
C++ Programming:. Program Design Including
Data Structure By Amee Trivedi.
Queues Chapter 4.
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
Stacks and Queues Chapter 4.
CSCI-255 LinkedList.
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 Structures and Algorithms
Queues Queues Queues.
Queues Chapter 4.
Data Structures Linked list.
Chapter 20: Binary Trees.
The Bag and Sequence Classes with Linked Lists
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Chapter 21: Binary Trees.
Chapter 4 Linked Lists.
Linked List (Part I) Data structure.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Review & Lab assignments
Data Structures and Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
CS148 Introduction to Programming II
Queues Definition of a Queue Examples of Queues
Linked Lists.
CS148 Introduction to Programming II
Presentation transcript:

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 of Stacks, Sets, etc.

CS 1032 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 tail_ptr

CS 1033 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 self-loop

CS 1034 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: JohnMaryDanSue head_ptr tail_ptr

CS 1035 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: head_ptr tail_ptr 8

CS 1036 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: abdc head_ptr tail_ptr f

CS 1037 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: abcf head_ptr tail_ptr d

CS 1038 Examples of Linked Lists ( A Polynomial ) A polynomial of degree n is the function P n (x)=a 0 +a 1 x+a 2 x 2 +…+a n x n. The a i ’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 ): a 0,0a 1,1a 2,2a n,n head_ptr tail_ptr

CS 1039 Operations on Linked Lists Insert a new item – At the head of the list, or – At the tail 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( )

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

CS Insert – at the Tail Insert a new data A. Call new: newPtr List before insertion After insertion to tail: data head_ptrtail_ptr Adata head_ptr tail_ptr A The link value in the new item = NULL The link value of the old last item = newPtr

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

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

CS Delete – the Tail Item List before deletion: List after deletion of the tail item: data head_ptr tail_ptr data head_ptr tail_ptr New value of tail_ptr = link-value of the 3 rd from last item New link-value of new last item = NULL. data

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

CS 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.

CS 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.