Linked Lists Damian Gordon.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Searching Damian Gordon. Google PageRank Damian Gordon.
Data Structures: Arrays Damian Gordon. Arrays Imagine we had to record the age of everyone in the class, we could do it declaring a variable for each.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Python: Linked Lists and Recursion Damian Gordon.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Recursion Damian Gordon. Recursion Recursion: Factorial Recursion is a feature of modularisation, when a module can call itself to complete a solution.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
PseudoCode: Revision Damian Gordon. Parameter Passing, Scope, Local and Global Variables Damian Gordon.
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.
Chapter 16: Linked Lists.
Review Array Array Elements Accessing array elements
Section 2.6 Linked List StringLog ADT Implementation
Python: Recursion Damian Gordon.
Stacks and Queues Chapter 4.
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Lists CS 3358.
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Linked lists.
classes and objects review
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Stack.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
CSCI 3333 Data Structures Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Chapter 18: Linked Lists.
Chapter 13 Collections.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Queues: Implemented using Arrays
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Stacks: Implemented using Linked Lists
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Circular Queues: Implemented using Arrays
CS148 Introduction to Programming II
LAB#3 Stacks Nora Albabtin nora albabtin.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Data Structures & Algorithms
Python: Stacks and Queues (as an Array)
General List.
Programming II (CS300) Chapter 07: Linked Lists
Queues: Implemented using Linked Lists
Linked Lists.
Module 13 Dynamic Memory.
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Linked Lists Damian Gordon

Linked Lists Imagine we wanted to have an array where we can dynamically add elements into it, and take them away. We can do this with a linked list.

Linked Lists A linked list is made up of nodes.

Linked Lists A linked list is made up of nodes. Each node has two parts to it:

Linked Lists A linked list is made up of nodes. Each node has two parts to it: Value Pointer

Linked Lists For example

Linked Lists For example 23

Linked Lists For example 23 62

Linked Lists For example 23 62 37

Linked Lists For example 23 62 37 31

Linked Lists For example NULL 23 62 37 31

Linked Lists For example NULL 23 62 37 31 Start of List End of List

Linked Lists To add a value in: NULL 23 62 37 31

Linked Lists To add a value in: NULL 23 62 37 31 26

Linked Lists To add a value in: NULL 23 62 37 31 26

Linked Lists To add a value in: NULL 23 62 37 31 26

Linked Lists To add a value in: NULL 23 62 26 37 31

Linked Lists To delete a value: NULL 23 62 26 37 31

Linked Lists To delete a value: NULL 23 62 26 37 31

Linked Lists To delete a value: NULL 23 62 37 31

Linked Lists So a Linked List is a series of Nodes And a Node has two parts Value Pointer A Linked List has a pointer to the start of the list called Head.

Linked Lists To add a value in: Value Pointer NULL 23 62 26 37 31 HEAD

Linked Lists TYPE Node: INTEGER Value; NODE Pointer; ENDTYPE;

Linked Lists We will look at implementing the following modules: CreateList() Create an empty linked list DeleteList() Delete a linked list IsEmpty() Check if the linked list is empty

CreateList()

Linked Lists To create a list: NULL HEAD

Linked Lists MODULE CreateList(): Head <- NULL; END.

DeleteList()

Linked Lists To delete a list: Value Pointer NULL 23 62 26 37 31 HEAD

Linked Lists To delete a list: Value Pointer NULL 23 62 26 37 31 HEAD

Linked Lists MODULE DeleteList(): Head <- NULL; END.

IsEmpty()

Linked Lists To check if a linked list is empty: NULL HEAD

Linked Lists MODULE IsEmpty(): Boolean Empty; IF Head = NULL THEN Empty <- True; ELSE Empty <- False; ENDIF; RETURN Empty; END.

Linked Lists Or MODULE IsEmpty(): RETURN Head = NULL; END.

Linked Lists We will look at implementing the following modules: DisplayList() print out all the nodes FindNode(N) Find a node with a given value (N) InsertNode(Pos, N) Insert a node at a particular position DeleteNode(N) Delete a node with a given value (N)

DisplayList()

NULL Current HEAD Linked Lists To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To display all the values, start at the Head, and end at NULL. NULL 23 62 26 37 31 Current HEAD

Linked Lists MODULE DisplayList(): Node Current <- Head; WHILE (Current.Pointer != NULL) DO Print Current.Value; Current <- Currrent.Pointer; ENDWHILE; END.

Linked Lists To print out a count of the number of nodes:

Linked Lists MODULE DisplayList(): Node Current <- Head; Integer CountNodes <- 0; WHILE (Current.Pointer != NULL) DO Print Current.Value; Current <- Currrent.Pointer; CountNodes <- CountNodes + 1; ENDWHILE; Print “Number of Nodes:”, CountNodes; END.

Linked Lists MODULE DisplayList(): Node Current <- Head; Integer CountNodes <- 0; WHILE (Current.Pointer != NULL) DO Print Current.Value; Current <- Currrent.Pointer; CountNodes <- CountNodes + 1; ENDWHILE; Print “Number of Nodes:”, CountNodes; END.

FindNode(N)

NULL Current HEAD Linked Lists To find a value (e.g. 37) in the list: 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To find a value (e.g. 37) in the list: 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To find a value (e.g. 37) in the list: 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To find a value (e.g. 37) in the list: 23 62 26 37 31 Current HEAD

Linked Lists MODULE FindNode(N): Node Current <- Head; WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Current <- Current.Pointer; ENDWHILE; IF Current.Pointer = NULL THEN Print “Value not found”; ELSE Print “Value found”; ENDIF; END. HEAD Current

InsertNode(Pos, N)

NULL Current HEAD Linked Lists To add in a value after position 4, of value N: NULL 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To add in a value after position 4, of value N: NULL 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To add in a value after position 4, of value N: NULL 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists To add in a value after position 4, of value N: NULL 23 62 26 37 31 Current HEAD

NULL Current HEAD Linked Lists N To add in a value after position 4, of value N: NULL 23 62 26 37 31 N Current HEAD

NULL Current HEAD Linked Lists N To add in a value after position 4, of value N: NULL 23 62 26 37 31 N Current HEAD

NULL Current HEAD Linked Lists N To add in a value after position 4, of value N: NULL 23 62 26 37 31 N Current HEAD

Linked Lists First declare the important variables, and then create a new node: MODULE InsertNode(Pos, N): Node Current <- Head; Integer PositionCounter; PositionCounter = 1; Node NewNode; NewNode.Value <- N; N

Linked Lists Next let’s find the position in the linked list: WHILE (Pos > PositionCounter) DO Current <- Current.Pointer; PositionCounter <- PositionCounter + 1; ENDWHILE;

Linked Lists Now add to the start if it’s Pos 0, otherwise add it in after the position pointed to by Current. IF Pos = 0 THEN NewNode.Pointer <- Head; Head <- NewNode; ELSE NewNode.Pointer <- Current.Pointer; Current.Pointer <- NewNode; ENDIF; NewNode HEAD Current

Linked Lists MODULE InsertNode(Pos, N): Node Current <- Head; Integer PositionCounter; Node NewNode; NewNode.Value <- N; WHILE (Pos > PositionCounter) DO Current <- Current.Pointer; PositionCounter <- PositionCounter + 1; ENDWHILE; IF Pos = 0 THEN NewNode.Pointer <- Head; Head <- NewNode; ELSE NewNode.Pointer <- Current.Pointer; Current.Pointer <- NewNode; ENDIF; END.

DeleteNode(N)

NULL Previous Current HEAD Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD

NULL Previous Current HEAD Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD

NULL Previous Current HEAD Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD

NULL Previous Current HEAD Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD

NULL Previous Current HEAD Linked Lists To delete a value (e.g. 37) in the list: NULL 23 62 26 37 31 Previous Current HEAD

Linked Lists Check if the required value is in the first node, if so, move the HEAD to the next node, and then it’s deleted: MODULE DeleteNode(N): Node Previous <- NULL; Node Current <- Head; IF Current.Value = N THEN Head <- Current.Pointer; ENDIF; END. HEAD

Linked Lists Otherwise find the node in the same way as the previous function: WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Previous <- Current; Current <- Current.Pointer; ENDWHILE; HEAD Previous Current

Linked Lists And then delete it: IF (Current.Value = N) THEN Previous.Pointer <- Current.Pointer; ELSE PRINT “Not found” ENDIF; HEAD Previous Current

Linked Lists MODULE DeleteNode(N): Node Previous <- NULL; Node Current <- Head; IF Current.Value = N THEN Head <- Current.Pointer; ELSE WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Previous <- Current; Current <- Current.Pointer; ENDWHILE; IF (Current.Value = N) THEN Previous.Pointer <- Current.Pointer; ELSE PRINT “Not found” ENDIF; END.

Linked Lists To print out a count of the node number where the delete was:

Linked Lists MODULE DeleteNode(N): Node Previous <- NULL; Node Current <- Head; Integer PositionCounter; IF Current.Value = N THEN Head <- Current.Pointer; ELSE WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Previous <- Current; Current <- Current.Pointer; PositionCounter <- PositionCounter + 1; ENDWHILE; IF (Current.Value == N) THEN Previous.Pointer <- Current.Pointer; Print “Node deleted at position:”, PositionCounter; ELSE PRINT “Not found” ENDIF; END.

Linked Lists MODULE DeleteNode(N): Node Previous <- NULL; Node Current <- Head; Integer PositionCounter; IF Current.Value = N THEN Head <- Current.Pointer; ELSE WHILE (Current.Value != N) AND (Current.Pointer != NULL) DO Previous <- Current; Current <- Current.Pointer; PositionCounter <- PositionCounter + 1; ENDWHILE; IF (Current.Value == N) THEN Previous.Pointer <- Current.Pointer; Print “Node deleted at position:”, PositionCounter; ELSE PRINT “Not found” ENDIF; END.

etc.