Applications of Linked Lists

Similar documents
abstract classes & types

Sparse Matrices. sparse many elements are zero dense few elements are zero

Ashish Gupta, Data JUET, Guna

Data Structure. Recitation III

How to declare an array in C?

Objectives Describe how to make a copy of a reference type ICloneable Clone Object.MemberwiseClone Discuss special cases

Spare Matrix Formats, and The Standard Template Library

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

List ADT. Announcements. The List interface. Implementing the List ADT

OBJECT ORIENTED DATA STRUCTURE & ALGORITHMS

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Chapter 6: Using Arrays

Maths for Signals and Systems Linear Algebra in Engineering. Some problems by Gilbert Strang

Week 3 Linked Lists & Sorting Algorithms

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

: Advanced Programming Final Exam Summer 2008 June 27, 2008

Computer Science 136. Midterm Examination

Data Structures (INE2011)

CS S-20 Linked Lists IV 1

Dynamic Data Structures

DATA STRUCTURES: OVERVIEW AND ITS APPLICATIONS

What is recursion. WAP to find sum of n natural numbers using recursion (5)

Recitation #2 Abstract Data Types, Collection Classes, and Linked List

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

DYNAMIC ARRAYS; FUNCTIONS & POINTERS; SHALLOW VS DEEP COPY

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Object-Oriented Principles and Practice / C++

MODULE 3: LINKED LIST

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

Computer Science Spring 2005 Final Examination, May 12, 2005

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Introduction to Computer Science II CS S-20 Linked Lists IV

CS2 Practical 1 CS2A 22/09/2004

CS32 Discussion Week 3

Introduction to Object-Oriented Programming

Introduction to Programming in C Department of Computer Science and Engineering

Introduction to Programming in C Department of Computer Science and Engineering

+ Abstract Data Types

Computers in Engineering. Linear Algebra Michael A. Hawker

Computers in Engineering COMP 208. Representing Vectors. Vector Operations 11/29/2007. Scaling. Adding and Subtracting

Sparse Matrix Formats

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

Data Structure (CS301)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

DYNAMIC MEMORY ALLOCATION AND DEALLOCATION

Comp Sci 1MD3 Mid-Term II 2004 Dr. Jacques Carette

Programming II (CS300)

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

EECE.2160: ECE Application Programming

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

Matrix Multiplication. (Dynamic Programming)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Data Structures & Algorithms

Data Structure. Chapter 4 List (Part II) Department of Communication Engineering National Central University Jhongli, Taiwan.

CS 314 Final Fall 2012

Introduction to Computer Science II CS S-20 Linked Lists III

x = 12 x = 12 1x = 16

Programming II (CS300)

Algorithms & Data Structures

ECS 178 Course Notes REFINEMENT

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

S.E. (Computer) (First Semester) EXAMINATION, 2011 DATA STRUCTURES AND ALGORITHM (2008 PATTERN) Time : Three Hours Maximum Marks : 100

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Lesson 7B Array methods. By John B. Owen All rights reserved 2011, revised 2014

How to swap values of two variables without tmp? However, this naive algorithm is biased. 1

Object-Oriented Principles and Practice / C++

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

Fall, 2015 Prof. Jungkeun Park

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp


Linked lists. Yet another Abstract Data Type Provides another method for providing space-efficient storage of data

Largest Online Community of VU Students

Today s Goals 2. Get familiar with the idea of references (things that point to objects) So far, the only real data structure we ve seen is arrays

CS 307 Final Fall 2009

Computer Science 136. Midterm Examination

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Interlude: Solving systems of Equations

2. Use elementary row operations to rewrite the augmented matrix in a simpler form (i.e., one whose solutions are easy to find).

JAVA.UTIL.LINKEDLIST CLASS

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Linked List Implementation of Queues

Column and row space of a matrix

The time and space are the two measure for efficiency of an algorithm.

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

Write a program that prompts a user to enter the number of students and then, their names and grades. The program will then outputs the average.

C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis

This midterm has about 4 questions more than the actual midterm of 200 pts will have

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

Performing Matrix Operations on the TI-83/84

Previous Year Nagarro Questions

CP2 Revision. theme: dynamic datatypes & data structures

Written Test 2. CSE Section M, Winter p. 1 of 8. Family Name: Given Name(s): Student Number:

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Transcription:

Applications of Linked Lists Linked List concept can be used to deal with many practical problems. Problem 1: Suppose you need to program an application that has a pre-defined number of categories, but the exact items in each category is unknown. Solution: Pre-defined number of categories implies that we can use a simple static structure like array to represent the categories. Since we do not know the number of items in each category, we can represent items in each category using a linked list. So what we need is an array of linked lists More Examples You can also think of representing a web index using an array of linked lists, where array contains the keywords and linked lists contains the web URL s where that keyword occurs.

Sparse Matrices Most problems can be modeled by a system of linear equations Thousands of equations (constraints) Thousands of variables (unknowns) It is not practical to allocate n by n space to store a linear system How much space is necessary for a matrix of size 10000x10000 where each entry is a double? Sparse matrix representation Designing the Node for Sparse Matrix public class Node { public Comparable data; public int row, col; public Node nextrow; public Node nextcol; Node(int row, int col, Comparable data) { this.data = data; this.row = row; this.col = col; nextrow = null; nextcol = null; } public String tostring(){return data.tostring();} }

Reversing a Linked List Other Advanced Operations on Linked Lists Cloning a Linked List Let us begin by evaluating the following code LinkedList list = new LinkedList(); list.prepend(new Node("first")); list.append(new Node("last")); LinkedList list2 = list; The reference list2 is an alias, it points to the same object as list1. This means that once we change list, these changes occur in list2 as well. In this case, we consider list2 as a shallow copy of list1. In other words, list1 and list2 shares the same nodes in one linked list. In many cases it will be quite useful to have a deep copy of an object. To create a deep copy, we need to duplicate all nodes in the list. For this purpose, the Object class provides the method clone(), which we will override in the LinkedList class: public Object clone() throws java.lang.clonenotsupportedexception { LinkedList twin = new LinkedList(); if(head == null) return twin; Node tmp = head; for(int i = 0; i < size; i++) { twin.append(tmp.data); tmp = tmp.getnext(); } } return twin;

Sorting a Linked List Sorting a List is one of the common operations that are performed. Sorting a linked list is not that trivial since linked list nodes cannot be randomly accessed. One useful way to sort a linked list is to remove a node from the old list and insert into a new list in order. o However for large lists, this is not very practical as insertion sort requires O(n 2 ) operations. An alternative method is to define a toarray method for the linked list class that allows the linked list to be converted to an array first and then apply a more efficient algorithm like quick sort to sort. o Once sorted the array can be converted into a linked list again. Questions Choosing the proper data structure depends on the application. Specify what data structure you would choose in each of the following cases. You can choose from a static array, singly linked list, circular LL, doubly LL, array of LL s, multilinked list etc A sorted file is given and a list in reverse order needs to be built in O(n) An application requires a structure where new nodes can easily added to the front and back of a given node in O(1) An application requires a data structure that can be randomly accessed A set of entries needs to be sorted by a category first. Each category will receive an unknown number of entries An application requires frequent insertions, generally in the same region A list needs to be maintained in multiple sorted orders, but space for each entry can be allocated only once.