Chapter 5 Linked List by www.asyrani.com. Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Singly linked lists Doubly linked lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Data Structures Using C++
CHP-5 LinkedList.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CSE Lecture 12 – Linked Lists …
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Data Structures & Algorithms
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Data Structures Using C++ 2E
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Data Structures Using Java1 Chapter 4 Linked Lists.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked List by Chapter 5 Linked List by
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Data Structures Using C++1 Chapter 5 Linked Lists.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
LINKED LISTS.
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.
C++ Programming:. Program Design Including
Pointers and Linked Lists
Pointers and Linked Lists
Review Deleting an Element from a Linked List Deletion involves:
UNIT-3 LINKED LIST.
Linked lists.
Stack and Queue APURBO DATTA.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Data Structures & Algorithms
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Data Structures & Algorithms
Linked lists.
Presentation transcript:

Chapter 5 Linked List by

Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please make sure to properly and slowly digesting the topics. We are going to take a deep breath now

List Definition List – A sequence of elements in certain linear order [English] List is where you put what to do one by one in sequence

Basic Operations TraversingSearching/retrievingInserting Removing/deleting an element given its position

Basic Operations [English] Traversing – Where you navigate your shopping list one by one Searching/Retrieving – Where you starting to find out specific items that you want to buy in your shopping list Inserting – Insert new stuff to buy in your shopping list Removing/Deleting – Where you strike out the things that you have bought

Types of common lists Stacks and queues, where insertions and deletions can be done only at the head or the tail of the sequence. That is the rules!!! HeadTail

POINTER

Let’s Learn First, how do you declare an integer? int nombor;

Let’s Learn First, how do you declare an integer?

Let’s Learn Output

Next Step Ok, that is how you declared an integer. But now I want to declare one more integer but I put * at the beginning of the variable name int *getdata;

Next Step Now, let say that I want to put that *getdata a value. So what the output??? int *getdata; *getdata = 100;

Let’s Learn Coding

Let’s Learn Output

Let’s Learn Output How come that you get an address???

Let’s Learn Back to coding

Let’s Learn So, actually, I do not put * at the beginning of variable getdata. So, let’s change

Let’s Learn New Output

Let’s Learn So, it looks like an integer but why we want to use *?

Explaination *getdata variable is a pointer based integer It points to an address. So, actually, it is not the value of getdata that change, it is another address.

Explaination Address0xFF30xFF40xFF5 Variable Name nomborNULL Inside This Address NULL Our First Implementation

Explaination Address0xFF30xFF40xFF5 Variable Name nomborNULL Inside This Address 900NULL We put an input “cin >> nombor” Let say we put 900 as an input

Explaination Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 9000xFF5NULL Then, we declared a pointer based integer type

Explaination Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 9000xFF5NULL If you guys see, getdata actually store an address instead of value. If we try to print out it, we will get an address

Explaination Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 9000xFF5NULL Then, if we try to set *getdata to some value. It actually set up the 0xFF5 with a value. Let say *getdata = 100

Explaination Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 9000xFF5100 Then, if we try to set *getdata to some value. It actually set up the 0xFF5 with a value. Let say *getdata = 100

So, what is happening? Declare *getdata will only store address forever and not a value. You cannot declare “getdata = 100” = ERROR You guys can only declare “*getdata = 100” You guys can also assign nombor to *getdata value (which is an address) using ampersand symbol

Explanation Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 9000xFF5100 getdata = &nombor

Explanation Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 900 0xFF3 100 getdata = &nombor

Explanation Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 900 0xFF3 100 *getdata = &nombor ERROR!!!

Explanation Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 900 0xFF3 100 int *getdata = &nombor RIGHT!!!

Explanation Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 900 0xFF3 100 WHAT IF nombor = *getdata

Explanation Address0xFF30xFF40xFF5 Variable Name nomborgetdataNULL Inside This Address 900 0xFF3 100 “nombor” still display 900 as “getdata” point to the variable “nombor”

Conclusion *getdata display a value of variable address & (ampersand) is used to set getdata to get an address Pointers is used to parse a value from a function to another function

LINKED LIST

What is Linked List? A linked list is a series of nodes Node 0Node 4Node 1

What is Linked List? Each node holds an item of data and a pointer(s) to the next node in the list Node 0Node 4Node 1 Point to Node 1 Point to Node 3 Node 2Node 3

What is Linked List? The last node's pointer is set to null NULL means end of node/no more nodes Node 0Node 4Node 1 NULL

What is Linked List? In order to hold on to a list, a program holds a pointer to the first node of the list. Node 0Node 4Node 1 PROGRAM Point to

Advantages Contain data of any type, including objects of other classes. Dynamic, so the length of a list can increase or decrease as necessary Full only when the system has insufficient memory Can be maintained in sorted order by inserting each new element at the proper point in the list linked list allows efficient insertion operations anywhere in the list

Comparison Array The size of a “conventional” C++ array however cannot be altered because the array size is fixed at compile time. Cannot contain objects and classes Arrays can become full as it depends on our defined array Time consuming Existing elements need to be moved Linked List Linked lists are dynamic, so the length of a list can increase or decrease as necessary. Can contain objects and classes Never becoming full unless computer does not have enough memory Faster than array Linked lists can be maintained in sorted order by inserting each new element at the proper point in the list

SINGLE LINKED LIST

Singly Linked List Singly linked list is one of the most primitive data structures Each node that makes up a singly linked list consists of a value/data and a reference to the next node (if any) in the list

SINGLY LINKED LIST OPERATIONS Insertion Searching Deletion Traversing

Let us declare two Classes

We only have int data; no need others - Also declare an object of this class called Next (object pointer type)

Let us declare two Classes Functions where int Data() return data (an integer) and Node* Next() return next (which is also a data but in pointer type)

Let us declare two Classes Also call Node class by accessing through *head -Inside public:, we set up constructor of head = NULL (since it always NULL at first) -Along with function to delete, insert (append) and print the output

INSERTION

Insertion Adding a node to the tail/end of the list Node 0Node 4Node 1 Head Tail Node 2Node 3

Insertion Adding a node to a singly linked list has only two cases: – Head =  in which case the node we are adding is now both the head and tail of the list – We simply need to append our node onto the end of the list updating the tail reference appropriately

Insertion (Case) Case 1 : Empty List Case When list is empty, which is indicated by (head == NULL)condition, the insertion is quite simple. Algorithm sets both head and tail to point to the new node.

Insertion (Case) Case 2 : Add First In this case, new node is inserted right before the current head node.

Insertion (Case) Case 2 : Add First 1 st Step : Update the next link of a new node, to point to the current head node.

Insertion (Case) Case 2 : Add First 2 nd Step :Update head link to point to the new node.

Insertion (Case) Case 3 : Add Last In this case, new node is inserted right after the current tail node.

Insertion (Case) Case 3 : Add Last 1 st Step :Update the next link of the current tail node, to point to the new node.

Insertion (Case) Case 3 : Add Last 2 nd Step :Update tail link to point to the new node.

Insertion (Case) Case 4 : General Case In general case, new node is always inserted between two nodes, which are already in the list. Head and tail links are not updated in this case.

Insertion (Case) Case 4 : General Case 1 st Step :Update link of the "previous" node, to point to the new node.

Insertion (Case) Case 4 : General Case 2 nd Step :Update link of the new node, to point to the "next" node.

Singly Linked List: Insertion Algorithm

Here, we having an insertion algorithm for our linked list function - We parsed our data (inserted integer)

Singly Linked List: Insertion Algorithm Since we have declared Class Node, we create an newNode which is an object pointer. - Example, if we want to insert new data, we create new node or we can say it in easy word “create a new integer since we only have int data inside it.

Singly Linked List: Insertion Algorithm Ok, we assigned our newly added data to SetData function.

Singly Linked List: Insertion Algorithm SetNext is always set to NULL.

Example Output cin >>getinput; list.Append(getinput);

Example Output Node* newNode = new Node(); - We will create a newNode to access all the variables and functions in Class Node Node 0 Head

Example Output Node 0 Head 100 newNode->SetData(100);

Example Output Node 0 Head NULL100 newNode->SetNext(NULL);

Singly Linked List: Insertion Algorithm Declare on more object pointer of tmp = head; Where head is from a List Class.

Singly Linked List: Insertion Algorithm Check if tmp != NULL -If the first node is filled, we will move tmp to the Next Node. -So, Next Node is empty and we will create new node using tmp- >SetNext(newNode)

Singly Linked List: Insertion Algorithm First Input Inserted: - Jump to “First Node” statement as tmp == NULL

Singly Linked List: Insertion Algorithm Head = newNode

Singly Linked List: Insertion Algorithm Second Input Inserted - Jump to “SetNext(newNode)” statement as tmp- >Next is still equal to NULL

Singly Linked List: Insertion Algorithm Since tmp is no longer equal to NULL, set the tmp to next node to create new node

Singly Linked List: Insertion Algorithm Third Input Inserted -Right now, tmp- >Next is not equal to NULL since we have two input already. -So we move current point to new Next Node. And then we can set tmp- >SetNext(newNode)

Singly Linked List: Insertion Algorithm Since tmp-Next (current one) is no longer equal to NULL, set the tmp to point to a new Next newNode and then create newNode

Example Output newNode->SetData(100);

Example Output Second Input tmp->SetNext(newNode); Where it is a new data

Example Output Second Input tmp->SetNext(newNode); Where it is a new data

Example Output

DELETION

Deletion

Singly Linked List: Deletion Deleting a node from a linked list is also straightforward but there are a few cases we need to account for: – The list is empty; or – The node to remove is the only node in the linked- list; or – We are removing the head node; or – We are removing the tail node; or – The node to remove is somewhere in between the head and tail; or – The item to remove doesn’t exist in the linked-list

Singly Linked List: Deletion Algorithm The algorithm whose cases we have described will remove a node from anywhere within a list irrespective of whether the node is the head, etc.

Singly Linked List: Deletion Algorithm

SEARCHING

Singly Linked List: Searching Searching a linked-list is straightforward Traverse the list, checking the desired value/data with the value of each node in the linked-list

Singly Linked List: Searching Algorithm

TRAVERSING

Singly Linked List: Traversing the list Same as traversing a doubly linked list Start at the head and continue until come across a node that is . The two cases are as follows: – Node = , we have exhausted all nodes in the linked-list – Must update the node reference to be node.Next

Singly Linked List: Traversing Algorithm

REVERSE TRAVERSING

Singly Linked List: Traversing the list in reverse order Need to acquire a reference to the predecessor of a node (for singly linked list, this is an expensive operation) For each node, finding its predecessor is an O(n) operation. Over the course of traversing the whole list backwards the cost becomes O(n 2 )

Singly Linked List: Reverse Traversal Algorithm

Singly Linked List: Reverse Traversal The following figure depicts the previous reverse traversal algorithm being applied to a linked list with integers 5, 10, 1, and 40

Linked List: Reverse Traversal The algorithm is only of real interest when we are using singly linked list Actually double linked list make reverse list traversal simple and efficient

DOUBLE LINKED LIST

Doubly Linked List Is similar to singly linked list. The only difference is that each node has a reference to both the next and previous nodes in the list

Doubly Linked List The following algorithms for the doubly linked-list are exactly similar as those listed previously for singly linked-list: – Searching – Traversal

Doubly Linked-list: Insertion The only major difference with previous algorithm for singly linked-list is that we need to remember to bind the previous pointer of n to the previous tail node if n was not the first node to be inserted in the list

Doubly Linked-list: Insertion Algorithm

Example: adding the following sequence integers to a list: 1,45, 60 and 12 will result as follows:

Doubly Linked-list: Deletion It is exactly the same as those algorithm defined in previous for singly linked-list. Like insertion, we have the added task of binding an additional reference (previous) to the correct value

Doubly Linked-list: Deletion Algorithm

Doubly Linked-list: Reverse Traversal Singly linked-list have a forward design, which is why the reverse traversal algorithm defined previously required some creative invention Doubly linked-list make reverse traversal as simple as forward traversal, except that we start at the tail node and update the pointers in the opposite direction

Doubly Linked-list: Reverse Traversal Algorithm

The end Ref 1.