COSC 1030 Section 8 List.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
Linear Lists – Linked List Representation
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
CHAPTER 7 Queues.
List Representation - Chain Fall 2010 CSE, POSTECH.
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Reference: Vinu V Das, Principles of Data Structures using C and C++
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Chapter 4 Data Structures ADT Examples  Stack  Queue  Trees.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Introduction to Data Structures and Algorithms
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
Chapter 5 Linked Lists II
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
CS Data Structures I Chapter 7 Queue I. 2 Topics Introduction Queue Application Implementation Linked List Array ADT List.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
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.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
COSO 1030 Section 3 Linked Data Representation. What is about Reference and Object in JAVA List and Node The Difference Between List and Array Linear.
Linked Lists Single Linked Lists Double Linked Lists.
1 Lecture 15: Big O Notation (Wednesday) Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science Test See Web for Details Don’t be deleted!
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
Linked Data Structures
Data Structures: Linked Lists
Section 2.6 Linked List StringLog ADT Implementation
The List ADT.
Computing with C# and the .NET Framework
COP 3538 Data Structures with OOP
LinkedList Class.
8-1.
8-1.
MyList<T> It’s a generic type, <T> is a type parameter
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Chapter 18: Linked Lists.
Chapter 13 Collections.
Circular List removedNode General Case Single Node Case lastNode aNode
Data Structures ADT List
Data Structures ADT List
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Building Java Programs
Recitation 5 CS0445 Data Structures
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Building Java Programs
Queues CSC212.
Data Structures ADT List
Linked Lists.
Building Java Programs
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Modularity & Data Abstraction
ITI Introduction to Computing II Lab-12
Building Java Programs
Presentation transcript:

COSC 1030 Section 8 List

Topics List ADT Non Linear Lists Generic List Circular List Double Linked List Generic List

List ADT interface ListSpec { // constructor – create an empty list boolean isEmpty(); void insert(Item anItem); Item search(Object aKey); boolean delete(Object aKey); int length(); // or size() or count() Item getItem(int ith); }

Circular List removedNode General Case Single Node Case lastNode aNode Insert empty case general case Remove single node case lastNode Empty Case lastNode

void insertAtLast(CListNode newNode) { if(lastNode == null) { newNode.link = newNode; } else { newNode.link = lastNode.link; lastNode.link = newNode; } lastNode = newNode; // update lastNode

CListNode removeLastNode() { CListNode tempNode = null; if(lastNode!= null) { CListNode previousNode = findPreviousNode(); tempNode = lastNode; if (previousNode != lastNode) { // generic case previousNode.link = lastNode.link; lastNode = lastNode.link; } else { // single node case lastNode = null; } // end if tempNode.link = null; // break link to the list } // end if return tempNode; }

Helper method private CListNode findPreviousNode() { assert(lastNode != null); CListNode previousNode = lastNode; while(previousNode.link != lastNode) { previousNode = previousNode.link; } assert(previousNode.link == lastNode); return previousNode;

search return null; public CListNode search(int key) { CListNode temp = lastNode; if(lastNode == null) return null; do { if(temp.key == key) return temp; temp = temp.link; } while(temp.next != lastNode); return null; }

Double Linked List General Case firstNode Insert at first empty case general case Search generic case Remove single node case Single Node Case firstNode Empty Case firstNode

Double Linked List - Insert General Case firstNode Insert at first empty case firstNode = newNode; General case newNode.next = firstNode; firstNode.previous = newNode; Empty Case firstNode

Double Linked List - remove firstNode theNode general case theNode.next.previous = theNode.previous; theNode.previous.next = theNode.next; last node case theNode.previous.next = null; first node case theNode.next.previous = null; firstNode = theNode.next; single node case – firstNode = null;

Generic List A list contains lists as its nodes Lisp notation of an expression (1, 2, plus)  1 + 2 ((1, x, plus), (x, x, plus), multiple)  (1 + x) * (x + x) List List multiple firstNode 1 x plus x x plus

class ListNode { Object item; ListNode link; String toString() { // It will call GenList’s toString // if the actual type of the item is GenList return item.toString(); }

class GenLisl { private ListNode firstNode; public void insert(Object newItem) { ListNode aNode = new ListNode(newItem); aNode.link = firstNode; firstNode = aNode; } public String toString() { StringBuffer strBuff = new StringBuffer(); strBuff.append(‘(‘); ListNode current = firstNode; while(current != null) { strBuff.append(current.toString()); current = current.next; if(current != null) strBuff .append(‘,‘); strBuff .append(‘)‘); return strBuff .toString();

class GenList { private ListNode firstNode; …… public int count() { int n = 0; ListNode current = firstNode; while(current != null) { // recursive call if the item is a list if (current.item instanceof ListNode) { n += ((ListNode) current.item).count(); } else { n ++; } current = current.next; return n; What is the complexity class of print() and count()?