1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.

Slides:



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

Linked Lists Mohammed Almashat CS /03/2006.
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.
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
Classes with dynamic members. int x; void f() { x=10; } main() { x=0; f(); } ‘Class’ matters! int x; void f() { int x; x=10; } main() { x=0; f(); } class.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Concept of lists and array implementations of lists.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
Abstract Data Type: List (optional, not required).
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.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Classes with dynamic objects List as a (dynamic) class.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 Object-Oriented Design Inheritance and Class Examples.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
ENEE150 – 0102 ANDREW GOFFIN Linked List/Project 3.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Programming Circular Linked List.
UNIT – I Linked Lists.
Data Structure and Algorithms
Doubly Linked List Review - We are writing this code
Chapter 4 Linked Lists.
Linked lists.
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Summary on linked lists
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
Programming Abstractions
Chapter 16 Linked Structures
Linked Lists.
General List.
Linked lists.
Presentation transcript:

1 CMPT 117 Linked Lists (singly linked)

2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has to be adjusted  If the number of elements is more than what has been allocated, a new array has to be used

3 Linked list  Advantages Easy to insert and delete data Easy to insert and delete data Dynamically allocate data Dynamically allocate data  Disadvantages Random access is slow Random access is slow Need some space overhead to store pointers Need some space overhead to store pointers

4 Pointer based linked list itemnext Can be a pointer to a class Pointer to the next node Each node has two parts:

5 Simple examples using linked list 10

6 Simple examples using linked list 10 30

7 Simple examples using linked list

8 Insertion

9 Deletion Delete this node

10 C++ definition struct Node { PStudent item; Node* next; }; typedef Node* PNode; struct Node { int item; Node* next; }; typedef Node* PNode;  The definition is different for different types of items

11 Allocate space for a node PNode p; p = new Node; p

12 Basic operations  To reference the member Item p->item p->item  To access the next node (if present) p->next p->next  To determine the end of list if (p->next == NULL) if (p->next == NULL)  To access the head of list Needs to define a head pointer, i.e. PNode head Needs to define a head pointer, i.e. PNode head head

13 Class LinkedList  A LinkedList class should be defined to encapsulate all operations of linked list class LinkedList { protected: protected: PNode head; PNode head; public: public: void InsertFirst(...); void InsertFirst(...); void DeleteFirst(...); void DeleteFirst(...); void Insert(...); void Insert(...);......}

14 To delete the first node  Is this code correct? if (head!=NULL) head = head->next;  This code works in Java, but not in C++, because Java has garbage collection  Is this correct? if (head!=NULL) { delete head->item; delete head->item; delete head; delete head; head = head->next; }  This code may work but is dangerous. How should we change it?

15 To insert as the first node  A new node needs to be allocated  The new node should point to the first node of the linked list  head should point to the newly created node  Please write the code for InsertFirst(PStudent pst)?

16 To display the contents of a linked list A high level Pseudocode: 1. Let current pointer point to the first node in the linked list. 2. While (current pointer is not NULL) { Display the data portion of the current node Set current pointer to the Next pointer of the current node }

17 Solution  Use Cur to keep track of the current node and initialise it to Head  To display the data cout item item <<endl;  To advance to the next node Cur = Cur->next;

18 Solution (cont’d) The above can be combined: for (PNode Cur = head; Cur!= NULL; Cur=Cur->next) cout item<<endl;

19 Deleting a node (not the first one) head CurPre Return this to the system

20 Deleting a node (cont’d)  Locate the node  Adjust the next pointer of the previous node, to point to the next node pointed to by next of the current node Need to have a pointer to point to the previous node! Need to have a pointer to point to the previous node!  Return the deleted node to the system delete or simply collect them all! delete or simply collect them all!

21 To locate a node while( Cur!=NULL && Cur->item!=value) { Pre = Cur; Cur= Cur->next; }

22 To adjust the pointer if (Cur!= NULL) { if (Pre == NULL) { head = Cur->next; //The first node is deleted } else { // not the first node Pre->next = Cur->next; } return Cur; }

23 Insert a node and keep the order of data ascending 2040 PreCur2040 PreCur30 NewPtr Locate the insertion location Adjust the pointers NewPtr->Next=Cur; Pre->Next=NewPtr;

24 Head PreCur NewPtr For the first node: NewPtr->Next = Cur; Head = NewPtr; Insert a node (cont’d)

25 To locate a node Pre = NULL; Cur = Head; while(NewValue>Cur->item) { Pre = Cur; Cur = Cur->next; } Will this work? What if NewValue>the value of the last element? Then Cur = Cur->next equals to NULL. So, Cur->item does not exist.

26 To locate the position (revised) Pre= NULL; Cur = Head; while(Cur!= NULL && NewValue>Cur->item) { Pre= Cur; Cur = Cur->next; }