Chapter 17 Linked List Saurav Karmakar Spring 2007.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Linked Lists.
JAVA & Linked List Implementation
Linear Lists – Linked List Representation
Data Structures ADT List
Linked Lists Linked Lists Representation Traversing a Linked List
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Data Structure Lecture-5
Linked Lists CENG 213 Data Structures.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
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.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 17 Linked List.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Data Structures Using Java1 Chapter 4 Linked Lists.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Data Structures Using C++1 Chapter 5 Linked Lists.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
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.
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.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
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.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
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.
C++ Programming:. Program Design Including
CSCI-255 LinkedList.
UNIT – I Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Circularly Linked Lists
Arrays and Linked Lists
Chapter 18: 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.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Problem Understanding
Chapter 17: Linked Lists.
CS148 Introduction to Programming II
Linked List Insert After
Problem Understanding
Presentation transcript:

Chapter 17 Linked List Saurav Karmakar Spring 2007

LISTS Easiest implementation of LIST --- ARRAY :: has Disadvantages. 1) To insert an item at the beginning or middle of an array  sliding lots of items over one place to make room :: ~ O(n) 2) Arrays have a fixed length :: can't be changed.  Adding items to a list  Allocation a whole new array if the array containing LIST is full and then move all the items from the old array to the new one.

Linked List Consists of connected, dynamically allocated nodes. A linked list is made up of "nodes". Each node has two components: an item, and a reference to the next node in the list.

Comparison with Array 1.Arrays contiguous direct access of elements insertion / deletion difficult 2.Linked Lists noncontiguous must scan for element insertion /deletion easy 1.Arrays contiguous direct access of elements insertion / deletion difficult 2.Linked Lists noncontiguous must scan for element insertion /deletion easy arrayname

for (int i = 0; i < length; i++) cout<< a[i]; for (ListNode p = theList.first; p != null; p = p.next) cout<< p.data ; for (int i = 0; i < length; i++) cout<< a[i]; for (ListNode p = theList.first; p != null; p = p.next) cout<< p.data ; a Iterating through the data structure

class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: Last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2 firstlast Adding an element

class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: Last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2 firstlast

class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2 firstlast

class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2x firstlast

class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: Last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2x firstlast

class ListNode { Object element; ListNode* next; } At any point, we can insert a new item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; Current->next = tmp; last Inserting an element A0A1A2 first current

class ListNode { Object element; ListNode* next; } At any point, we can insert a new item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; Current->next = tmp; Inserting an element A0A1A2 first last current tmp

class ListNode { Object element; ListNode* next; } At any point, we can insert a new item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; Current->next = tmp; Inserting an element A0A1A2 first last current x tmp

class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; current->next = tmp; Inserting an element A0A1A2 first last current x tmp

class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; Current->next = tmp; Inserting an element A0A1A2 first last current x tmp

Simplified version Current->next = new ListNode(x, current->next) tmp = new ListNode(); Current->next = tmp; Tmp->next = current->next; Tmp->element = x;

class ListNode { Object element; ListNode* next; } Current->next = current->next->next; Deleting an element A0A1A2 current last

class ListNode { Object element; ListNode* next; } Current->next = current->next->next; Memory leak! Deleting an element A0A1A2 last current

Delete a Node Node *deletedNode = current->next; Current- >next = current->next->next; Delete deletedNode;

Length Function The Length function takes a linked list and computes the number of elements in the list. /*Given a linked list head pointer, compute and return the number of nodes in the list.*/ int Length(struct node* head) { struct node* current = head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; }

abc header Header nodes allow us to avoid special cases [in the code] such as insertion of the first element and removal of the last element. The header node holds no data but serves to satisfy the requirement that every node have a previous node. Not necessarily a standard implementation. Header node

abc head tail class DoubleListNode { Object element; ListNode* next; ListNode* prev; } class DoubleListNode { Object element; ListNode* next; ListNode* prev; } A doubly linked list allows bidirectional traversal by storing two pointers per node. Doubly Linked Lists

head tail // constructor DoubleList() { head = new DoubleListNode (); tail = new DoubleListNode (); head->next = tail; tail->prev = head; } Empty Doubly Linked List

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail current

Inserting into a Doubly Linked List ac head tail newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current

Inserting into a Doubly Linked List ac head tail newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current

Inserting into a Doubly Linked List ac head tail newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail b current

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail b current

newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail b current

Circular Linked Lists abcd first

Sorted Linked List A sorted link list is one in which items are in sorted order. The major difference from linked list is the insertion operation.