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.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
C++ Programming: Program Design Including Data Structures, Third 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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
Chapter 17 Linked List.
Reference: Vinu V Das, Principles of Data Structures using C and C++
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Data Structures Using Java1 Chapter 4 Linked Lists.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Data Structures Using C++1 Chapter 5 Linked Lists.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 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.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
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.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 5 (continued)
Review Deleting an Element from a Linked List Deletion involves:
Linked List Stacks, Linked List Queues, Dequeues
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
Linked List Variations
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Linked List Sudeshna Sarkar.
LINKED LISTS CSCD Linked Lists.
Lists.
Chapter 16-2 Linked Structures
Linked Lists.
Chapter 18: Linked Lists.
Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
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 List Implementation
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
CS148 Introduction to Programming II
General List.
Doubly Linked List Implementation
BY PROF. IRSHAD AHMAD LONE.
Linked Lists Chapter 5 (continued)
Chapter 9 Linked Lists.
Presentation transcript:

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 the list. No “previous node”. Can we get rid of this special case?

CPSC 252 Linked Lists III Page 2 Variations on Singly Linked Lists Head Nodes / Dummy Nodes A dummy node is not used to store data, it is created when the list is created. An empty list with a dummy node looks like: head

CPSC 252 Linked Lists III Page 3 Insertions go after the dummy node: This code always works, assuming that previousNode is a pointer to the node after which the new node is to be inserted: Node* newNode = makeNode( item, previousNode->next ); previousNode->next = newNode; head item

CPSC 252 Linked Lists III Page 4 Deletions always have a previous node: This code always works, assuming that previousNode is a pointer to the node before the one we are deleting: previousNode->next = victim->next; or we can say previousNode->next = previousNode->next->next ; head item1 item2 victim

CPSC 252 Linked Lists III Page 5 Dummy node summary Simplifies our algorithms for insertion and deletion. Uses more memory, the empty list is bigger (and slower to create).

CPSC 252 Linked Lists III Page 6 Linked list application Logged in users on a shared computer. List size grows and shrinks as necessary. On logoff, we can remove a user from the middle. The sharing policy is time-slicing – each user gets access to the system’s resources for some fixed period of time. The “which user goes next” code has a special case for the end of the list. Could we get rid of this special case?

CPSC 252 Linked Lists III Page 7 Circular Linked Lists The node at the end of the list contains a link to the node at the head of the list: item1item2item3item4 head

CPSC 252 Linked Lists III Page 8 Another problem with our lists What if we want to traverse our list backwards? Say, to search for the last occurrence of an item. Or what if we want to delete an element when we have a pointer to it (from the find operation)?

CPSC 252 Linked Lists III Page 9 Doubly Linked Lists Each node has a pointer to the previous node as well as a pointer to the next node. Such a list looks as follows: head item1 item2item3item4 Doubly Linked Circular Lists First points (back) to last and last points (forward) to first.

CPSC 252 Linked Lists III Page 10 Let’s look at doubly linked circular lists with a dummy node typedef int Item_type; struct Node { Item_type item; Node *next, *prev; }; We need a makeEmpty() function that creates an empty list (one with only a dummy node, but no “real”nodes)

CPSC 252 Linked Lists III Page 11 Implementing the makeEmpty() function Node *makeEmpty() { Node *head = new Node; head->next = head; head->prev = head; return head; }

CPSC 252 Linked Lists III Page 12 Implementing the insertFirst() function void insertFirst( Node* head, const Item_type& item ) //Pre: head points to the dummy node of a list //Post: item is inserted at the front of the list { Node* newNode = new Node; newNode->item = item; newNode->next = ???; newNode->prev = ???; ???->prev = newNode; ???->next = newNode; }

CPSC 252 Linked Lists III Page 13 Implementing the insertLast() function void insertLast( Node* head, const Item_type& item ) //Pre: head points to the dummy node of a list //Post: item is inserted at the end of the list { Node* newNode = new Node; newNode->item = item; newNode->next = ???; newNode->prev = ???; ???->prev = newNode; ???->next = newNode; }

CPSC 252 Linked Lists III Page 14 Implementing the find() function – does the old version work? Node* find( Node* head, const Item_type& item ) //Pre: head points to the dummy node of a list //Post: if item is in the list, a pointer to the node //containing it is returned; otherwise a null pointer //is returned { Node* cursor = head; while( cursor != NULL && cursor->item != item ) cursor = cursor->next; return cursor; }

CPSC 252 Linked Lists III Page 15 Implementing the find() function –the new version Node* find( Node* head, const Item_type& item ) //Pre: head points to the dummy node of a list //Post: if item is in the list, a pointer to the node //containing it is returned; otherwise a null pointer //is returned { Node* cursor = head->next; while( cursor != head && cursor->item != item ) cursor = cursor->next; return cursor == head ? NULL : cursor; }

CPSC 252 Linked Lists III Page 16 Implementing the remove() function void remove( Node* victim ) //Pre: victim points to the node to be removed //Post: the victim node is removed from the list { }

CPSC 252 Linked Lists III Page 17 Implementing the remove() function void remove( Node* victim ) //Pre: victim points to the node to be removed //Post: the victim node is removed from the list { victim->next->prev = victim->prev; victim->prev->next = victim->next; }

CPSC 252 Linked Lists III Page 18 If we have two link fields in our nodes, we could also maintain two lists through one set of data. Suppose we want to sort students by last name and student number. We can maintain a list of students sorted by both criteria by using our two link fields and two head pointers: nameList Smith 5647 numList Jones 3623 Wong 1287