Download presentation
Presentation is loading. Please wait.
Published byAdelia Butler Modified over 9 years ago
1
Linked Lists based on the original work of Dr. Roger deBry Version 1.0
2
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 singly linked list copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
3
Motivation Consider a shopping list that you want to keep on
your computer. To keep such a list you probably want a data structure that * is easy to keep in “some” order * 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 copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
4
What’s Wrong With an Array?
Apples Bread Milk Hamburger Carrots copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
5
What’s Wrong With an Array?
Apples Bread Milk Hamburger Carrots Chicken Add Chicken to the list copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
6
What’s Wrong With an Array?
Apples Bread Milk Hamburger Carrots Chicken But I really want Chicken by Hamburger (both in the meat dept) copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
7
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! copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
8
What’s Wrong With an Array?
Apples Bread Remove milk from the list Milk Carrots Hamburger Corn copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
9
What’s Wrong With an Array?
Apples Bread Can’t have “empty” slots in an array Carrots Hamburger Corn copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
10
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. copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
11
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 copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
12
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 24 butter 1 lb eggs 1 dz null Note that the pointer in the last node in the list is set to null. copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
13
Adding a new item to the front of the grocery list!
1. Create a new node using the new operator head nnnnn rolls xxxxx milk 24 newNode 2 gals butter null 1 lb eggs 1 dz null Node* newNode = new Node(“milk”, “2 gals”); copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
14
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 null 2. copy the address stored in head into the pointer in the new node. copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
15
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 null 3. Store the address of the new node in the head. copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
16
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 null 3. Now we can throw away this pointer. Be careful, don’t write delete newNode; copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
17
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 null newNode soda 12 cans null copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
18
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 null newNode soda 12 cans null copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
19
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 null newNode soda 12 cans copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
20
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 null newNode soda 12 cans nnnnn copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
21
Delete an item from the front of the grocery list!
head nnnnn rolls 24 xxxxx butter 1 lb eggs 1 dz null copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
22
Delete an item from the front of the grocery list!
head temp nnnnn rolls 24 xxxxx butter 1 lb eggs 1 dz null 1. Get the pointer contained in the first node. copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
23
Delete an item from the front of the grocery list!
head temp nnnnn rolls xxxxx xxxxx 24 xxxxx butter 1 lb eggs 1 dz null 2. Store it in head. copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
24
Delete an item from the front of the grocery list!
head temp nnnnn rolls 24 xxxxx xxxxx xxxxx butter 1 lb eggs 1 dz null 3. Delete the dynamically allocated node object. copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
25
Let’s Develop Some Code
copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
26
The Node Class Node - item: string what are the - quantity: int
- 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? copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
27
The Head Class Head what are the - size: int data members?
- first: Node* Head( ) push_front(:Node*) :void delete_front(:Node*) insertNode(:Node*, Node*): void what are the operations? copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
28
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; } copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
29
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::delete_front ( ) { Node* byeByeNode = first; first = first->getNext( ); delete byeByeNode; } copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
30
Insert an item into the middle of the list
head node size first item rolls quantity 24 node item milk next quantity 1 gal next void Head::insertNode ( Node* here, Node* newNode ) { Node* tempPtr = here->getNext( ); newNode->setNext(tempPtr); here->setNext(newNode); } copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
31
Lab Three: Design Your Linked List
Lab Three: Implement Your Linked List copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.