A linked list grows as data is added to it. In a linked list each item is packaged into a node.

Similar documents
Fun facts about recursion

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

Introduction. Reference

Binary Search Trees. EECS 214, Fall 2018

Autumn 2012 November 7 th 9 th, 2012 Section Solution

Linked Lists

Lesson 13: Exploring Factored Form

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CMPT 225. Lecture 6 linked list

File Directories Associated with any file management system and collection of files is a file directories The directory contains information about

Section 1: True / False (2 points each, 30 pts total)

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

CIS 190: C/C++ Programming. Linked Lists

Chapter 17: Linked Lists

CS 2113 Software Engineering

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Programming 2. Topic 8: Linked Lists, Basic Searching and Sorting

Introduction to Linked Data Structures

Text Box Frames. Format Text Box

Graphs. The ultimate data structure. graphs 1

speller.c dictionary contains valid words, one per line 1. calls load on the dictionary file

Hash-Based Indexes. Chapter 11

CMSC 341 Lecture 7 Lists

Implementing Linked Lists

Search Lesson Outline

Lecture 15 : Review DRAFT

Data Structures and Algorithms (DSA) Course 5 Lists. Iulian Năstac

Unit 3.2: Fractions, Decimals and Percent Lesson: Comparing and Ordering Fractions and Decimals

ITI Introduction to Computing II

COMPSCI 105 S Principles of Computer Science. 17 Linked List(1)

A List Implementation That Links Data

Linked Lists and other Dynamic Data Structures

Halting Measures and Termination Arguments

Hash Table and Hashing

Data Structures Brett Bernstein

ICOM 4035 Data Structures. Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department

We'll dive right in with an example linked list. Our list will hold the values 1, 2, and 3.!

Lecture 23: Binary Search Trees

CSC172 Lab: State Space Search

However, in C we can group related variables together into something called a struct.

CSE 373 Spring Midterm. Friday April 21st

Information Science 2

CS 103 Unit 11. Linked Lists. Mark Redekopp

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

How invariants help writing loops Author: Sander Kooijmans Document version: 1.0

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

Chapter 20: Binary Trees

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Data Structures. Instructor: Tsung-Che Chiang

Data Structures. Topic #7

Ch. 17: Linked Lists. Introduction to Linked Lists

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Introduction to Programming in C Department of Computer Science and Engineering

General Insert Operation

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

Searching Algorithms/Time Analysis

Chapter 17: Linked Lists

Working with Tables in Microsoft Word

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Ashish Gupta, Data JUET, Guna

+ Abstract Data Types

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

File Systems. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Exercise: Inventing Language

Chapter 1 Section 1 Lesson: Solving Linear Equations

BBM 201 DATA STRUCTURES

FILE SYSTEMS. CS124 Operating Systems Winter , Lecture 23

Skip Lists: Motivation

Data Structures and Algorithms

Graphs. The ultimate data structure. graphs 1

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List

Dynamic Programming Group Exercises

Topic 11 Linked Lists

Midterm Review. CS 211 Fall 2018

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

CS122 Lecture 3 Winter Term,

.:: UNIT 3 ::. LIST AND LINKED LIST

Have the students look at the editor on their computers. Refer to overhead projector as necessary.

CSC 1052 Algorithms & Data Structures II: Linked Lists

Section 05: Solutions

CMSC 341 Lecture 10 Binary Search Trees

Lab 1. In this first lab class, we will address the following topics:

Data Structure Series

Program development plan

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

COMP171. Hashing.

CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;

ECE 122. Engineering Problem Solving Using Java

CSC 1052 Algorithms & Data Structures II: Linked Queues

CS 103 Unit 11. Linked Lists. Mark Redekopp

LINKED IMPLEMENTATION OF LIST

ECE 650 Systems Programming & Engineering. Spring 2018

CMSC 341 Hashing (Continued) Based on slides from previous iterations of this course

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

You must include this cover sheet. Either type up the assignment using theory4.tex, or print out this PDF.

Introduction to C An overview of the programming language C, syntax, data types and input/output

Linked Structures Chapter John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Transcription:

Lesson 4 Data Structures What is a data structure? A data structure is a particular way of organizing data in a computer. A data structure that we have already encountered is the array. An array stores data in a contiguous piece of memory. This means that the data is all stored next to each other in sequence in memory. We are now going to look at another data structure, a linked list. Linked List A big problem with arrays are that they are of a fixed size meaning that you need to know how many items you will be storing in them when you create it. A linked list grows as data is added to it. In a linked list each item is packaged into a node. data is the payload, or the information you want to store. This can be any data type and needn t be limited to one field. next is a pointer to the next node in the list. If next = 0 in the diagrams (for this exercise we will assume 0 = NULL) it means that no node follows it in the list. Meaning that we are at the end of the list. Nodes are then linked into a list or chain. To keep track of our linked list we create a pointer to the first node, or a head pointer.

Iterating through a linked list As with an array we need some method of accessing the elements of the linked list. To do this we need to create a curr pointer. We set this pointer to point to the same place as the head pointer. If this node s next pointer does not equal NULL we can set the curr pointer to node next pointer. So that curr is now pointing at the second node.

We can continue this process to get to the third and fourth node. Finally we set curr pointer to the last node s next value which is NULL. At this point, when curr equals NULL we know that we have reached the end of the list and can stop iterating. Here is the code to iterate through the linked list.

Adding a node to a linked list Next we need to know how to add a node to a linked. The example we will go through adds a node to the end of the linked list but it is useful to be able to add a node anywhere in the linked list. For adding a node to a linked list there are two distinct situations that we need to deal with. The first is that the list is currently empty and the second is that there are already nodes in the list. First we need to create a new node and give it its values. We then check if the head pointer is NULL. This will tell us if the list is empty. If the list is empty we simply set head to point to our new node and return from the function. The second scenario is that the list isn t empty. If this is the case we need to iterate through the linked list in a similar manner as we did before except that we will check if current->next is equal to NULL instead of current. When current->next equals NULL we know that we are at the last node in the list. We then set current->next equal to the newnode we have just created.

Removing a node from a linked list To remove a node we need to Find the node. The node we are removing is the current node Make the previous node s next point to current->next Delete the node Removing the head node The simplest situation is that we just want to remove the first node in the list. In this situation we Need to set the head node to point to the second node in the list. We do this by letting the head node equal the next value of the current node (the node we are deleting) Set the head node equal to current->next Delete the head node

This code will delete the first node in the linked list. N.B. When you are removing a node always first make sure that the list isn t empty To remove a node from somewhere else in the list you need to first find the correct node. The most common ways of specifying a node to delete are by the data it stores are by its position in the list. The easiest way to correctly remove a node is to have a second pointer to the previous node. While searching for the correct node you need to keep track of the previous node. The code below will move the current and previous node through the list. N.B. The order of these instructions is very important