Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists.

Similar presentations


Presentation on theme: "Linked Lists."— Presentation transcript:

1 Linked Lists

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

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

4 Motivation Suppose you wanted to write this application …

5 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

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

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

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

9 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!

10 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)

11 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)

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

13 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

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

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

16 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 nullptr 1 lb eggs 1 dz null Node* newNode = new Node(“milk”, “2 gals”);

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

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

19 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

20 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

21 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

22 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

23 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

24 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

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

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

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

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

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

30 Let’s Develop Some Code

31 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?

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

33 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; }

34 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; }

35 Lab One: Design Your Linked List
Project Five: Implement Your Linked List

36 Your Node Class A string to contain a description of a grocery item,
for example, "bread". A string to contain a quantity for this item, for example "2 loaves". A pointer to the next Node in the list. Initially this pointer should be set to nullptr. Constructor, getters and setters

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

38 Your List Class A constructor that creates an empty list object.
A push_back(Node*) function A push_front(Node*) function A pop_back( ) function A pop_front( ) function


Download ppt "Linked Lists."

Similar presentations


Ads by Google