Linked Lists.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
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.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Linked Lists based on the original work of Dr. Roger deBry Version 1.0
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.
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
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
UNIT-II Topics to be covered Singly linked list Circular linked list
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
Lecture 6 of Computer Science II
Chapter 4 Linked Structures.
UNIT – I Linked Lists.
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.
Lists CS 3358.
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Linked Lists.
Lists.
LINKED LISTS CSCD Linked Lists.
Top Ten Words that Almost Rhyme with “Peas”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
Lists.
Chapter 16-2 Linked Structures
Topic 11 Linked Lists -Joel Spolsky
C++ Classes and Data Structures Jeffrey S. Childs
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Chapter 18: Linked Lists.
Linked List Intro CSCE 121 J. Michael Moore.
Further Data Structures
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Linked Lists Chapter 4.
Linked List and Selection Sort
Chapter 17: Linked Lists.
Introduction to C++ Linear Linked Lists
CENG 218 Classes and Data Structures
Data Structures & Algorithms
Linked Lists.
Linked List Intro CSCE 121.
CS148 Introduction to Programming II
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked List Improvements
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Presentation transcript:

Linked Lists

Objectives At the conclusion of this lesson, students should be able to: Describe the basic operation and structure of a linked list * Inserting into the list * Removing from the list * Iterating through the list Write code that implements a singley linked list

The Linked List is an important data structure that every computer scientist should understand.

Motivation Suppose you wanted to write this application …

Motivation To keep a grocery list like this you probably need a data structure that * is easy to keep in “some” order (by aisle) * is easy to insert items into the middle of * is easy to delete items from the middle of * is easy to iterate through * grows and shrinks as items are added and deleted

What’s Wrong With an Array? Apples Bread Milk Hamburger Carrots

What’s Wrong With an Array? Apples Bread Milk Hamburger Carrots Chicken Add Chicken to the list

What’s Wrong With an Array? Apples Bread Milk Hamburger Carrots Chicken But I really want Chicken by Hamburger (both in the meat dept)

What’s Wrong With an Array? Apples Bread Milk Hamburger Chicken Carrots If there is a lot of stuff in the array, moving elements to free up a space in the middle is expensive!

What’s Wrong With an Array? Apples Bread Milk Carrots Hamburger Corn Remove milk from the list (your spouse bought some on the way home from work)

What’s Wrong With an Array? Apples Bread Can’t have “empty” slots in an array Carrots Hamburger Corn Remove milk from the list (your spouse bought some on the way home from work)

What’s Wrong With an Array? Apples Bread Milk Carrots Hamburger Corn Add popcorn to the list An array is fixed in size. Once it is full we cannot add more.

We need a data structure ... That can grow and shrink as needed That is not in contiguous memory That has fast insertion/deletion in the middle

The ideal data structure is a linked list. Linked lists (and variations) are used in all kinds of computer science problems. I good understanding of how a linked list works is essential for every CS student.

A linked list is an ideal candidate! this is called a node. Each node in the list contains some data and a pointer to the next node. Nodes are dynamically allocated as new items are added to the list. head rolls A linked list has a “head” that points to the first node in the list. 24 butter 1 lb eggs 1 dz nullptr Note that the pointer in the last node in the list is set to nullptr.

Adding a new item to the front of the grocery list! Create a new node using the new operator, Nodes are always dynamically allocated. head nnnnn rolls xxxxx milk 24 newNode 2 gals butter nullptr 1 lb eggs 1 dz null Node* newNode = new Node(“milk”, “2 gals”);

Adding a new item to the front of the grocery list! milk head 2 gals nnnnn nnnnn rolls 24 xxxxx butter newNode 1 lb eggs 1 dz nullptr 2. copy the address stored in head into the pointer in the new node.

Adding a new item to the front of the grocery list! milk head 2 gals rolls nnnnn 24 xxxxx xxxxx butter newNode 1 lb eggs 1 dz nullptr 3. Store the address of the new node in the head.

Adding a new item to the front of the grocery list! milk head 2 gals rolls nnnnn 24 newNode xxxxx butter 1 lb eggs 1 dz nullptr

Adding a new item in the middle of the grocery list! milk head 2 gals rolls 24 1. Create another new node using the new operator butter 1 lb eggs 1 dz nullptr newNode soda 12 cans nullptr

Adding a new item in the middle of the grocery list! milk head 2 gals rolls 24 2. Locate the node you want to insert after. butter 1 lb eggs 1 dz nullptr newNode soda 12 cans nullptr

Adding a new item in the middle of the grocery list! milk head 2 gals rolls 24 3. Store the pointer from this node in the new node. butter 1 lb nnnnn eggs 1 dz nullptr newNode soda 12 cans

Adding a new item in the middle of the grocery list! milk head 2 gals rolls 24 4. Store the address of the new node in this node. butter 1 lb eggs 1 dz xxxxx xxxxx nullptr newNode soda 12 cans nnnnn

Adding a new item in the middle of the grocery list! milk head 2 gals rolls 24 4. Store the address of the new node in this node. butter 1 lb eggs 1 dz xxxxx xxxxx nullptr newNode soda 12 cans nnnnn

Delete an item from the front of the grocery list! head nnnnn rolls 24 xxxxx butter 1 lb eggs 1 dz nullptr

Delete an item from the front of the grocery list! head nnnnn rolls 24 xxxxx butter 1 lb oldHead eggs 1 dz nullptr 1. Save the address stored in head.

Delete an item from the front of the grocery list! head temp nnnnn rolls 24 xxxxx butter 1 lb oldHead eggs 1 dz nullptr 2. Get the pointer contained in the first node.

Delete an item from the front of the grocery list! head temp nnnnn rolls xxxxx xxxxx 24 xxxxx butter 1 lb oldHead eggs 1 dz nullptr 3. Store it in head.

Delete an item from the front of the grocery list! head temp nnnnn rolls 24 xxxxx xxxxx xxxxx butter 1 lb oldHead eggs 1 dz nullptr 4. Delete the dynamically allocated node object.

Let’s Develop Some Code

The Node Class Node - item: string what are the - quantity: string - next: Node* what are the data members? +Node(:string, :int) +setQuantity(:string) :void +getQuantity( ) :string +setItem(:string) :void +getItem( ) :string +setNext(:Node*) :void +getNext( ) :Node* what are the operations?

The Head Class Head what are the - first: Node* data members? +Head( ) +push_front(:Node*) :void +pop_front( ): void what are the operations?

Add a new item to the front of the list head node size first item rolls quantity 24 next void Head::push_front ( Node* n ) { n->setNext(first); first = n; }

Delete an item from the front of the list head node size first item rolls quantity 24 node item milk next quantity 1 gal next void Head::pop_front ( ) { Node* byeByeNode = first; first = first->getNext( ); delete byeByeNode; }

Lab #14: Design a Linked List class Project #9: Implement a Linked List

Your Node Class A Book* that points to a Book object A pointer to the next Node in the list. Initially this pointer should be set to nullptr. Constructor, getters and setters

The Book Class A string containing the book’s title A string containing the book’s author A double containing the price of the book Constructors, getters and setters

Your List Class A Node* that points to the first node in the list. Initially this pointer should be set to nullptr. A Node* that points to the last node in the list. An integer that keeps track of how many nodes are in the list.

Your List Class (Part II) A default constructor that creates an empty list object A destructor A push_front(Node*) function A pop_front( ) function You also need a stand-alone function that takes a List object as its parameter and outputs each book in the List.

The List Class (Part III) A push_back(Node* ) function A pop_back( ) function.

This project is part of your final exam. You are expected to do your own work. You only have one chance to submit your Project. Whatever you submit will be graded as-is. Projects cannot be corrected and re- submitted. You can turn your project in early for a 5 point bonus. projects will not be accepted after 11:59pm on Sunday April 24.