CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
COMP171 Fall 2005 Lists.
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
CS 367 – Introduction to Data Structures
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
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.
Data Structures Using C++
CSE Lecture 12 – Linked Lists …
Data Structure Lecture-5
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.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
CS 106 Introduction to Computer Science I 12 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 02 / 06 / 2009 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 09 / 15 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 02 / 09 / 2009 Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.
Data Structures Using Java1 Chapter 4 Linked Lists.
1 Working with Pointers An exercise in destroying your computer.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List by Chapter 5 Linked List by
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Data Structures Using C++1 Chapter 5 Linked Lists.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
CSCI387 Data Structure Fall Doubly Linked List Sep. 3, 2012 Sung Hee Park Computer Science Virginia State University.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
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.
Data Structure & Algorithms
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
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.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Lists CS 3358.
Linked List Variations
LINKED LISTS CSCD Linked Lists.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Doubly linked lists Idea: same as singly linked list, but each node also points to the previous: Can optionally also have a pointer to the tail, so we.
CS148 Introduction to Programming II
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)

Reminder & Agenda Discussion Slides Linked List Doubly Linked List 1 st Midterm next Thursday Project 2 due next Tuesday 9:00 p.m.

Linked Lists Component Node value can be any kind of data variables/structures next points to either the next Node or null Example value*next typedef int ItemType; struct Node { ItemType value; Node *next; };

Linked Lists Single linked lists Are we done? value*nextvalue*nextvalue*next null NO!!

Linked Lists You have to know where it begins We call it head pointer Node *head; value*nextvalue*nextvalue*next null

Linked Lists Check list A description of a node with a next pointer A head pointer points to the first node The list must be loop-free Unless it is a circularly linked list

Linked Lists Operations Search Insert Remove Sanity Check!!

Linked Lists (Search) Simply traverse the linked list 1*next12*next LinkedList::search(int expect) { Node *currNode = head; if (currNode == null) return null; while (currNode->next != null) { if (currNode->value == expect) return currNode; currNode = currNode->next; } return null; } null

Linked Lists (Insert) 1.New a node 12*next null Node *newNode = new Node; newNode->value = 12; newNode->next = null;

Linked Lists (Insert) 1.New a node 2.Fine the tail 1*next4 Node *currNode = head; while (currNode->next != null) currNode = currNode->next; null

Linked Lists (Insert) 1.New a node 2.Fine the tail 3.Link the new node and the tail 12*next null 1*next4 currNode->next = newNode;

Linked Lists (Insert) LinkedList::insert(int v) { Node *newNode = new Node; newNode->value = v; newNode->next = null; Node *currNode = head; if (currNode == null) { // Empty list head = newNode; return ; } while (currNode->next != null) currNode = currNode->next; currNode->next = newNode; } Are we done?NO!! What if head is null ?

Linked Lists (Advance Insert) Now we want to build a SORTED linked list This function still works? No, we may insert a new node in any place! 12*next null 1*next4 LinkedList::insert(int v) { Node *newNode = new Node; newNode->value = v; newNode->next = null; Node *currNode = head; if (currNode == null) { // Empty list head = newNode; return ; } while (currNode->next != null) currNode = currNode->next; currNode->next = newNode; }

Linked Lists (Advance Insert) 1.Find the right position 12*next null 1*next Node *currNode = head; Node *nextNode = null; while (currNode->next != null) { nextNode = currNode->next; if (currNode->value value > v) break; currNode = currNode->next; }

Linked Lists (Advance Insert) 2. New a node and link pointers Node *currNode = head; Node *nextNode = null; while (currNode->next != null) { nextNode = currNode->next; if (currNode->value value > v) break; currNode = currNode->next; } 12*next null 1*next4 Node *newNode = new Node; newNode->value = v; newNode->next = currNode->next = currNode->next; newNode;

Linked Lists (Advance Insert) Node *newNode = new Node; newNode->value = v; Node *currNode = head; Node *nextNode = null; if (currNode == null) { // Empty list head = newNode; return ; } if (currNode->value > v) { // Update head newNode->next = head; head = newNode; return ; } while (currNode->next != null) { nextNode = currNode->next; if (currNode->value value > v) break; currNode = currNode->next; } newNode->next = currNode->next; currNode->next = newNode; return ; Are we done?NO!! What if head is null ? What if we want to insert the smallest value (head)? What if we want to insert the largest value (tail)?

Linked Lists (Remove) Similar to the advance insert 1.Find the right position 2.Fix pointers 3.Delete the node 12*next null 1*next4 Node *prevNode = // Find the previous node Node *currNode = // Find the node prevNode->next = currNode->next; delete currNode; Previous nodeCurrent node

Linked Lists (Remove) LinkedList::remove(int v) { Node *prevNode = null; Node *currNode = head; if (currNode == null) // Empty list return ; while (currNode->next != null) { if (currNode->value == v) break; prevNode = currNode; currNode = currNode->next; } if (currNode->next == null) { if (currNode->value != v) // Not found return ; else // Delete tail prevNode->next = null; } else if (prevNode == null) // Delete head head = currNode->next; else // Delete middle prevNode->next = currNode->next; delete currNode; return ; } Are we done?NO!! What if head is null ? What if we want to delete the tail? What if we want to delete the head? What if we cannot find the node to be deleted?

Discussion: Linked Lists and Arrays Linked Lists Pros Memory efficiency Easy to remove nodes Easy to insert nodes in the middle Array Pros Quickly search Easy to be implemented

Variations Sorted Linked List Changed insertion method Doubly Linked List Each node has prev and next pointers A tail pointer is kept to point to the last node Circularly Linked List The tail node points to the first node Essentially there is no first node

Doubly Linked Lists Create links in reversed order Discussion: Why you want to use doubly linked list? Search:  Insert: Maybe Remove:

Doubly Linked Lists (Search) Do you really need to do anything else especially for doubly linked list? NO!

Doubly Linked Lists (Insert) 1.Find the position Always tail (unsorted) or specific (sorted) 2.New a node and link pointers Note: Order! newNode->next = 2.newNode->prev = 3. currNode->next = 4. currNode->next->prev = One possible order (not only): 1  2  4  3 currNode->next; currNode; newNode;

Doubly Linked Lists (Insert) Discussion: Other cases? Insert to empty Insert to head Insert to tail

Doubly Linked Lists (Remove) LinkedList::remove(int v) { Node *prevNode = null; Node *currNode = head; if (currNode == null) // Empty list return ; while (currNode->next != null) { if (currNode->value == v) break; prevNode = currNode; currNode = currNode->next; } if (currNode->next == null) { if (currNode->value != v) // Not found return ; else // Delete tail prevNode->next = null; } else if (prevNode == null) // Delete head head = currNode->next; else // Delete middle prevNode->next = currNode->next; delete currNode; return ; } Can you modify this function? currNode->prev->next = null; currNode->prev->next = currNode->next;

1 st Midterm Constructor/Deconstructor Initialization ordering Copy constructor Operator overloading Array  Linked list Implement a linked list Any kind of linked lists Make sure you understand EVERY components! Stack and queue Make sure you pay attention to all lectures on next week!