Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Singly linked lists Doubly linked lists
Stacks, Queues, and Linked Lists
Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structures: A Pseudocode Approach with C
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Chapter 3: Arrays, Linked Lists, and Recursion
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
Linked Lists Objects->Connected->by->Pointers. What is a Linked List? List: a collection Linked: any individual item points to another item to connect.
Linked List by Chapter 5 Linked List by
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.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
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 Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
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 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
STACKS & QUEUES for CLASS XII ( C++).
Lecture 6 of Computer Science II
CSCI-255 LinkedList.
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Stack and Queue APURBO DATTA.
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
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.
Chapter 16 Linked Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists.
Presentation transcript:

Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04

Abstract Data Type (ADT) a set of objects a set of operations Same set of objects, different sets of operations => different ADTs ADTs are implemented using classes, hiding implementation details: encapsulation 2

LIST ABSTRACTION Definition: A linear configuration of elements, called nodes. 3

Basic operations To create/destroy a list To expand/shrink the list Read/Write operations Changing the current node (moving along the list) To report current position in the list To report status of the list 4

Specifications for the ADT List Operations on lists ▫Add new entry – at end, or anywhere ▫Remove an item ▫Remove all items ▫Replace an entry ▫Look at any entry ▫Look for an entry of a specific value ▫Count how many entries ▫Check if list is empty, full ▫Display all the entries 5

Limitations of Arrays Limitations of an array are: Size of array is fixed; even dynamically allocated arrays' size cannot be changed. Adding element to front or middle of array is still extremely difficult. 6

Potential Problem Operations add, remove, replace work OK when valid position given Remove not meaningful on empty lists A list could become full, what happens to add ? 7

A collection of connected nodes with a set of operations. Nodes are divided into to bit of information: 1.Data 2.Pointer 8 Linked List ABSTRACTION

Characteristics Insert and delete nodes in any order The nodes are connected Each node has two components ▫ Information (data) ▫ Link to the next node The nodes are accessed through the links between them 9

Understanding the linked list Linked list is not a continuous memory location as array so each node in linked list is stored at different memory location, in order to access the whole list or to weave them in one list each node should have the address of the next respective node. And to access the list we should always keep safe the address of first node so that we can access the whole list with the help of the first node, and if in case we lose the address of the first node then the whole list will become a garbage as we cannot find or access this list again. So before we create any node we first create a pointer which will hold the address of the first node in the list 10

Linked list consists of 1.A sequence of nodes used to hold individual data Elements 2.Each node will have two Sections a)The value to hold b)The Address of next node in the list 3.A pointer which will hold the address of the first node in the list 11

Head Predecessor of X Node X Success- or of X tail For each node the node that is in front of it is called predecessor. The node that is after it is called successor. 12

Insertion and Deletion A. Insertion To insert a node X between the nodes A and B:.Create a link from X to B..Create a link from A to X, 13

Insertion X A B 14

Insertion and Deletion B. Deletion To delete a node X between A and B: Create a link from A to B, Remove node X 15

Deletion A X B 16

Keep It Simple Let us understand the Linked Structure with the help of this example Following picture depicts linked list holding sequence of numbers (87, 42, 53, 4): Each node include: The data element The link or address to the next node / / means Null,this is used to specify that this is the last node in the list 17

Defining the structure of the linked list Structure of node in linked list here is implemented as class in C++ Here the Structure of a node will have 2 parts 1) The Data 2) The Pointer to next node ( i.e the address of the next node) Let us understand with an example: class Node { public: int data; // the value to be stored Node* next; // pointer to next node }; Here the Data is defined as integer and named as data and the pointer to the next node is declared as a pointer and Named as next 18

Starting with the linked Structure Linked list is a collection of many individual nodes But before we actually create the nodes we should first create the pointer which will hold the address of the first node in the list so that with this pointer we can actually track the whole list With the following line of code we will create a pointer variable to point towards the first node in the list Example: Node *Head; At this point our list is empty- it has no elements and it is represented by a NULL pointer, i.e. a pointer whose value is 0 (signified by / OR NULL) / Head 19

Starting with the linked Structure Example: Head= new Node; Here with this one line of code we did two things 1)we created a new first node with the new operator, 2)and we also saved the address of this new node in the pointer variable knows as Head ( one can give any name ) Head. First node in the list is created and its address is stored in the pointer named Head 20

Starting with the linked Structure Now we will insert data into this new node with the following code Head>data = 87; Head. 87 This part is Head->data This part is Head->next We have till now successfully created the structure for the node and added the first node and a pointer which will hold the address of the first node 21

Implementing a Linked Stack If we are implementing a linked stack then the stack does all the operations from the front side so the deletion and insertion is from the front of the list. Appending the value 42 into the linked list: Initially we have a pointer named Head and our first node with 87 value Head. 87 / Head> next =Null; // set the next element to null as this is the last now 22

We will be adding a node to the linked Stack Node * temp = new Node ; Adding a new node at the front of the linked list Head. 87 With the above code we create a new node, and assign its address to the temporary pointer variable temp. temp / 23

Adding a node to the front Assign value to the newly added node Temp-> data=43; Head. 87 temp. 43 / 24

Adding a node to the front Set next data member of new node to point to first node in original list temp -> next = Head; Head. 87 temp. 43 /. 25

Adding a node to the front Now we can Update original List pointer to point to the new first node as the List pointer always points towards first node in the list Head= temp; Head. 87 temp. 43 /. 26

Adding a node to the front Summarized, as a function: void add2front( int val, Node* &Head) { Node *temp = new Node; temp -> data = val; temp -> next = Head; Head= temp; } 27

Adding a node at end To add a new node at the back we need to reach at the last node and to go to the last node we will assign a new temp pointer which will hop through each node till it reaches the node which is having a null value. It will check each node if it is == Null, until Null is found / Head. temp. 28

Adding a node at end void add2end( int val, Node* Head) { Node *temp ; Node *ptr = new Node; ptr->data = val; ptr -> next = NULL; temp = Head; while(temp->next!=NULL) temp= temp -> next; temp->next= ptr; } 29

Adding a node in the middle void add2mid( int val, int newval, Node* H) { Node *temp; Node *ptr = new Node; ptr->data = newval; ptr -> next = NULL; temp = H; while(temp->data!=val && temp->next != NULL) temp= temp -> next; ptr->next=temp->next; temp->next= ptr; } 30

Node Linking 1.Single linked lists : Each node contains two links - to the previous and to the next node 2.Double linked lists : Each node contains a link only to the next node 3.Circular lists: The tail is linked to the head. 31

32

33

34

List Implementation Static – using an array Dynamic – using linear nodes 35

Array Implementation Two parallel arrays are used:  Index array - the number stored in the i-th element shows the index of the "next" node, i.e. node that follows the i-th node  Data array - used to store the informational part of the nodes. 36

37