Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Complexity Analysis of SetSize Method and Implementation of LinkedList, Exams of Data Structures and Algorithms

The time complexity analysis of the setsize method in java's arraylinearlistext class and the implementation of linkedlist's reverse method. Code snippets and explanations of the worst and best-case complexities for each method.

Typology: Exams

Pre 2010

Uploaded on 03/11/2009

koofers-user-4qb-1
koofers-user-4qb-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Complexity Analysis of SetSize Method and Implementation of LinkedList and more Exams Data Structures and Algorithms in PDF only on Docsity! Solution 1: The worst case complexity of the SetSize(int size) method will be O(n) and best case complexity can be O(1). The code is as follows: /** Extension of array implementation of Linear List*/ package dataStructures; import java.util.*; import utilities.*; public class ArrayLinearListExt extends ArrayLinearList { public ArrayLinearListExt(int capacity) { super(capacity); } public void SetSize(int size) { if(size>element.length) { throw new IllegalArgumentException("The size should be smaller than the capacity of the List"); } if(this.size<size) { for(int i=this.size+1;i<=size;i++) { element[i]=null; } this.size=size; } else if(this.size>size) { for(int i=this.size-1;i>=size;i--) { element[i]=null; } this.size=size; } } public static void main(String [] args) { // test default constructor ArrayLinearListExt x = new ArrayLinearListExt(10); // test size System.out.println("Initial size is " + x.size()); // test isEmpty if (x.isEmpty()) System.out.println("The list is empty"); else System.out.println("The list is not empty"); // test put x.add(0, new Integer(2)); x.add(1, new Integer(6)); x.add(0, new Integer(1)); x.add(2, new Integer(4)); System.out.println("List size is " + x.size()); //test SetSize x.SetSize(5); System.out.println("List size is " + x.size()); x.SetSize(3); System.out.println("List size is " + x.size()); x.SetSize(11); System.out.println("List size is " + x.size()); } } Solution 2: Complexity of void Add( int element) is O(1) Complexity of void Delete()is O(1) Only two temporary reference variable must be required to implement void Reverse(Node start)in O(n) time. The code is as follows: package applications; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class LinkedList { private Node start; public LinkedList() { start=null; } public void Create(int nodeNum) { System.out.println("\nEnter the data to be added: "); for(int i=0;i<nodeNum;i++)
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved