Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Stacks, Queues, and Linked Lists
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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 Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linear Data Structures
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
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.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
Week 4 - Monday CS221.
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Data structures and algorithms
MEMORY REPRESENTATION OF STACKS
Lists CS 3358.
CS 1114: Implementing Search
UNIT-3 LINKED LIST.
Data Structures and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Basic Data Structures.
Stacks and Queues CMSC 202.
Stacks and Queues.
Cinda Heeren / Geoffrey Tien
Stack and Queue APURBO DATTA.
Data Structures and Algorithms IT12112
LINKED LISTS CSCD Linked Lists.
Stacks and Queues.
Introduction to Linked Lists
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Stacks, Queues, and Deques
Arrays and Linked Lists
Doubly linked lists Idea: same as singly linked list, but each node also points to the previous: Can optionally also have a pointer to the tail, so we.
List Data Structure.
Stacks, Queues, and Deques
Lesson Objectives Aims
Linked List Intro CSCE 121 J. Michael Moore.
Array Based Collections
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Data Structures and Algorithms
More Data Structures (Part 1)
Stacks CS-240 Dick Steflik.
CSI 1340 Introduction to Computer Science II
CS210- Lecture 6 Jun 13, 2005 Announcements
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks, Queues, and Deques
Linked List Intro CSCE 121.
Presentation transcript:

Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted list

Inserting into a sorted linked list Similar to before, but we will remove the need to “append” nodes to our list while (cursor != nullptr && cursor->value < num) { previous = cursor; cursor = cursor->next; } Edge case: The “insertNode” method should check for the empty case If the list is not empty, insert the new data just before the first node with value “greater than” the new one Second edge case: if the node should be the new “head”

Sorted List benefits No ambiguity about the structure of the list I.e. no need for separate “append” and “insert” methods Much faster search! How?

Doubly linked lists Idea: same as singly linked list, but each node also points to the previous: Can optionally also have a pointer to the tail, so we can start scanning from the end, and make appending easier Also helps with binary search

Inserting into a DLL Find node before the new one, nextNode Set newNode.prev = nextNode.prev Set newNode.prev.next = newNode Set newNode.next = nextNode Set nextNode.prev = newNode

List-like structures Linked lists are of independent usefulness (replace messiness of arrays) However, they can be used as “backend” for other types of data structures Stacks Queues Heaps Graphs Trees Sometimes need slight modification, depending on program needs

Common needs of data storage Similarity to lists Insert Delete Difference Place in the list may depend on when it is added rather than the actual value Temporal vs. spacial May depend on both!

stacks Stores data in a list, but data can only be removed in the reverse order of which it was added Called “First in, last out” data structure (FILO)

Stack with arrays Adding an item to a stack (to keep with the metaphor) is called “pushing” Removing is called “popping” With an array, pushing would look like:

Stacks with arrays ”Popping” off a stack retrieves the last item added, and removes it from the stack Implemented with arrays, the stack must be a fixed size, enlarged when needed

Stacks with arrays Extra work because of static size Attributes Need “isFull” and “isEmpty” check If we want to resize, need a copy constructor Attributes Array of objects int stackSize; // containing current size int top; // for the index of the top element (starts at -1 when empty and moves up)

Stacks with arrays To push an element: Make sure the array isn’t full, if so, error. Move the top index up (top is at -1 for an empty stack) Copy the new value into the to index

Stacks with arrays To pop an element: Check to make sure the stack isn’t empty Return the value at the top counter Decrement top counter

Stacks with arrays Regular constructor Copy constructor Take size argument Dynamically allocate an array Set “top” to -1 Copy constructor Allocate space equal to old stack Copy attributes Copy array elements

Stacks with arrays isFull isEmpty return top == stackSize – 1;

Stacks with linked list Same principle as with arrays, but instead we adapt the standard list operations to behave as a stack Need a “push” and “pop” method ”Push” puts an element on the head (or tail) “Pop” removes and returns the head (or tail) element Using the head is more efficient, especially with a singly linked list