SlideShare a Scribd company logo
Merit-Quality-Excellence
Sukkur IBA University
Data Structures (Fall 2023)
LAB No: 02
Instructor: Marina Gul
Objective of Lab No. 2:
After performing lab2, students will be able to:
o More on Array
o Implementation of Linked list
Practice 01
We have already covered an array. Perform following task on an array,
a) Input size of an array
b) Define and create array with the input size on random value
c) Display the defined array
d) Implement insert, delete and search operation (Binary)
import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Take input the size of an array
Scanner sc = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter the size of an Array");
int size= sc.nextInt(); // Read user input
//create array with the input size on random value
Random rd = new Random(); // creating Random object
rd.setSeed(System.currentTimeMillis()); // use current time as a seed
// fill the data array with pseudo-random numbers from 0 to 99,
inclusive
int[] arr = new int[size];
for (int i = 0; i < arr.length; i++)
arr[i] = rd.nextInt(100); // storing random integers in an array
//Display the defined array
System.out.println(Arrays.toString(arr));
//delete an element from array
arr = MyArray.removeTheElement(arr, 2);
System.out.println(Arrays.toString(arr));
}
}
/** Program showing some array uses. */
class MyArray{
// Function to remove the element
public static int[] removeTheElement(int[] arr, int index)
{
// If the array is empty or the index is not in array range
// return the original array
if (arr == null || index < 0
|| index >= arr.length) {
return arr;
}
int i=0;
//start form the index till end of array
for (i = index; i< arr.length-1; i++)
arr[i]=arr[i+1];
// for last index
arr[i]=0;
// return the resultant array
return arr;
}
// Returns index of x if it is present in arr[].
public static int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// If we reach here, then element was
// not present
return -1;
}
}
Practice 02
We have already covered Linked List. Some of the basic operation of linked list are following
a) Create linked list
b) Implementation of Insert, show, isEmpty and delete operation
class Node{
int data;
Node next;
//Constructor
Node(int data)
{
this.data=data;
this.next=null;
}
}
class LinkedList{
Node head;
void addLast(int data)
{
Node node=new Node(data);
node.data=data;
node.next=null;
if(head==null)
head=node;
else{
Node n=head;
while(n.next!=null)
n=n.next;
n.next=node;
}
}
void show() {
Node = head;
while (node.next != null){
System.out.print(node.data + " ");
node = node.next;
}
System.out.print(node.data + " ");
}
void addStart(int data)
{
Node =new Node(data);
node.data=data;
//node.next=null;
node.next=head;
head=node;
}
void addMiddle(int index,int data)
{
Node =new Node(data);
node.data=data;
node.next=null;
Node n=head;
for(int i=1;i<index-1;i++)
{
n=n.next;
}
node.next=n.next;
n.next=node;
}
void deleteStart()
{
head=head.next;
}
void deleteLast()
{
Node n=head;
while(n.next.next!=null)
n=n.next;
n.next=null;
}
void deleteAt(int index)
{
if (index==1)
deleteStart();
}
else{
Node n=head;
Node temp=null;
for(int i=1;i<index-1;i++)
{
n=n.next;
}
temp=n.next;
n.next=temp.next;
}
boolean isEmpty(){
return head == null;
}
}
public class Main {
public static void main(String[] args) {
LinkedList ll=new LinkedList();
ll.addLast(5);
ll.addLast(19);
/*
ll.addLast(19);
ll.show();
System.out.println("");
ll.addStart(20);
ll.show();
System.out.println("");
ll.addMiddle(2,200);
ll.show();
System.out.println("");
ll.deleteStart();
ll.show();
*/
}
}
Exercise
Arrays
1. Create a file named NLArray.java and design following functions or performing NLP.
- String [] wordTokenize (String fileName)  Read any text file and return list of words
from that file. (Ignore . , : and all these types operators)
- String[] extractEmail (String fileName)  Read any text file and return all emails from
and file
Note: Read about Natural Language Processing (NLP), Word Tokenizing, Stop Words,
Information Extraction/Retrieval for Knowledge.
2. Design following methods in above same class NLArray.java for Image Cropping.
- void extractBoundaries (int arr[][])  This function should extract boundaries and
print from arr (Boundaries include 1st
row, 1st
col, last row, last col).
- void cropCenterPart (int arr[][])  This function should extract center part and print
from arr, center part includes everything except Boundaries (Boundaries include 1st
row,
1st
col, last row, last col).
3. Design following methods in above same class NLArray.java in order to determining N
consecutive same values
boolean NConRep (int arr[][])  This function would return True if N consecutive values are
same otherwise false. Check examples:
2 1 3 5
22 22 22 22
12 41 88 53
57 8 74 4
N is 4 so matrix is 4x4 and in 4 consecutive values are same so it should return True.
Linked List
4. Implement a function to search for a specific element in a linked list.
5. Write a function to find the length of a linked list.
6. In this task you will write a program that implements a variant of a linked list. This variant has
a dummy node pointed to by the head link as shown in the following figure:
Linked list with a dummy first node:
This trick will allow your code to be a little simpler, not requiring a special case for add or remove
operations. Your constructor method will be:
public LinkedList(){
head = new Node(null);
size = 0;
}
You need to write a class called LinkedList that implements the following List interface:
// a list interface
public interface List {
public boolean isEmpty();
// returns true if the list is empty, false otherwise
public int size();
// returns the number of items in the list
public void add(Object item);
// adds an item to the list
// item is added at the end of the list
public void add(int index, Object item);
// adds an item to the list at the given index
// item is added at the given index;
// the indices start from 1.
public void remove(int index);
// removes the item from the list that has the given index
public void remove(Object item);
// removes an item from the list
// removes the first item in the list whose equal method matches
// that of the given item
public List duplicate();
// creates a duplicate of the list
// returns a copy of the linked list
public List duplicateReversed();
// creates a duplicate of the list with the nodes in reverse order
// returns a copy of the linked list with the nodes in reverse order
}
In addition to the interface, your LinkedList class needs to implement a toString() method that
prints the list in the format
[ size: the_size_of_the_list - item1, item2, .... ]
Note: Your Node class should be an inner class within the LinkedList class. Make sure your
class implements the interface as specified, i.e. your class should begin with public class
LinkedList implements List.

More Related Content

Similar to Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf

File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
archiesgallery
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
rozakashif85
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
Nivegeetha
 
Java Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docxJava Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docx
michael1810
 
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdfUsing Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
fms12345
 
Write a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdfWrite a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdf
hardjasonoco14599
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
aromalcom
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
Ratnala Charan kumar
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
freddysarabia1
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
ajoy21
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
soniya555961
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
Jagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
lavparmar007
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
Bharati Vidyapeeth COE, Navi Mumbai
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
amazing2001
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
jagriti srivastava
 

Similar to Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf (20)

File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Java Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docxJava Programming- 1) Write a recursive method that finds and returns t.docx
Java Programming- 1) Write a recursive method that finds and returns t.docx
 
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdfUsing Array Approach, Linked List approach, and Delete Byte Approach.pdf
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
 
Write a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdfWrite a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdf
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
In java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdfIn java , I want you to implement a Data Structure known as a Doubly.pdf
In java , I want you to implement a Data Structure known as a Doubly.pdf
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 

Recently uploaded

Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Denish Jangid
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
ricssacare
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
Special education needs
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Denish Jangid
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 

Recently uploaded (20)

Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 

Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf

  • 1. Merit-Quality-Excellence Sukkur IBA University Data Structures (Fall 2023) LAB No: 02 Instructor: Marina Gul Objective of Lab No. 2: After performing lab2, students will be able to: o More on Array o Implementation of Linked list Practice 01 We have already covered an array. Perform following task on an array, a) Input size of an array b) Define and create array with the input size on random value c) Display the defined array d) Implement insert, delete and search operation (Binary) import java.util.Random; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { // Take input the size of an array Scanner sc = new Scanner(System.in); // Create a Scanner object System.out.println("Enter the size of an Array"); int size= sc.nextInt(); // Read user input //create array with the input size on random value Random rd = new Random(); // creating Random object rd.setSeed(System.currentTimeMillis()); // use current time as a seed // fill the data array with pseudo-random numbers from 0 to 99, inclusive int[] arr = new int[size]; for (int i = 0; i < arr.length; i++) arr[i] = rd.nextInt(100); // storing random integers in an array //Display the defined array System.out.println(Arrays.toString(arr)); //delete an element from array arr = MyArray.removeTheElement(arr, 2); System.out.println(Arrays.toString(arr));
  • 2. } } /** Program showing some array uses. */ class MyArray{ // Function to remove the element public static int[] removeTheElement(int[] arr, int index) { // If the array is empty or the index is not in array range // return the original array if (arr == null || index < 0 || index >= arr.length) { return arr; } int i=0; //start form the index till end of array for (i = index; i< arr.length-1; i++) arr[i]=arr[i+1]; // for last index arr[i]=0; // return the resultant array return arr; } // Returns index of x if it is present in arr[]. public static int binarySearch(int arr[], int x) { int l = 0, r = arr.length - 1; while (l <= r) { int m = l + (r - l) / 2; // Check if x is present at mid if (arr[m] == x) return m; // If x greater, ignore left half if (arr[m] < x) l = m + 1; // If x is smaller, ignore right half else r = m - 1; } // If we reach here, then element was // not present return -1; } }
  • 3. Practice 02 We have already covered Linked List. Some of the basic operation of linked list are following a) Create linked list b) Implementation of Insert, show, isEmpty and delete operation class Node{ int data; Node next; //Constructor Node(int data) { this.data=data; this.next=null; } } class LinkedList{ Node head; void addLast(int data) { Node node=new Node(data); node.data=data; node.next=null; if(head==null) head=node; else{ Node n=head; while(n.next!=null) n=n.next; n.next=node; } } void show() { Node = head; while (node.next != null){ System.out.print(node.data + " "); node = node.next; } System.out.print(node.data + " "); } void addStart(int data) { Node =new Node(data); node.data=data; //node.next=null; node.next=head; head=node; } void addMiddle(int index,int data)
  • 4. { Node =new Node(data); node.data=data; node.next=null; Node n=head; for(int i=1;i<index-1;i++) { n=n.next; } node.next=n.next; n.next=node; } void deleteStart() { head=head.next; } void deleteLast() { Node n=head; while(n.next.next!=null) n=n.next; n.next=null; } void deleteAt(int index) { if (index==1) deleteStart(); } else{ Node n=head; Node temp=null; for(int i=1;i<index-1;i++) { n=n.next; } temp=n.next; n.next=temp.next; } boolean isEmpty(){ return head == null; } } public class Main { public static void main(String[] args) { LinkedList ll=new LinkedList(); ll.addLast(5); ll.addLast(19); /* ll.addLast(19); ll.show(); System.out.println(""); ll.addStart(20); ll.show();
  • 6. Exercise Arrays 1. Create a file named NLArray.java and design following functions or performing NLP. - String [] wordTokenize (String fileName)  Read any text file and return list of words from that file. (Ignore . , : and all these types operators) - String[] extractEmail (String fileName)  Read any text file and return all emails from and file Note: Read about Natural Language Processing (NLP), Word Tokenizing, Stop Words, Information Extraction/Retrieval for Knowledge. 2. Design following methods in above same class NLArray.java for Image Cropping. - void extractBoundaries (int arr[][])  This function should extract boundaries and print from arr (Boundaries include 1st row, 1st col, last row, last col). - void cropCenterPart (int arr[][])  This function should extract center part and print from arr, center part includes everything except Boundaries (Boundaries include 1st row, 1st col, last row, last col). 3. Design following methods in above same class NLArray.java in order to determining N consecutive same values boolean NConRep (int arr[][])  This function would return True if N consecutive values are same otherwise false. Check examples: 2 1 3 5 22 22 22 22 12 41 88 53 57 8 74 4 N is 4 so matrix is 4x4 and in 4 consecutive values are same so it should return True. Linked List 4. Implement a function to search for a specific element in a linked list. 5. Write a function to find the length of a linked list. 6. In this task you will write a program that implements a variant of a linked list. This variant has a dummy node pointed to by the head link as shown in the following figure:
  • 7. Linked list with a dummy first node: This trick will allow your code to be a little simpler, not requiring a special case for add or remove operations. Your constructor method will be: public LinkedList(){ head = new Node(null); size = 0; } You need to write a class called LinkedList that implements the following List interface: // a list interface public interface List { public boolean isEmpty(); // returns true if the list is empty, false otherwise public int size(); // returns the number of items in the list public void add(Object item); // adds an item to the list // item is added at the end of the list public void add(int index, Object item); // adds an item to the list at the given index // item is added at the given index; // the indices start from 1. public void remove(int index); // removes the item from the list that has the given index public void remove(Object item); // removes an item from the list // removes the first item in the list whose equal method matches // that of the given item public List duplicate(); // creates a duplicate of the list // returns a copy of the linked list public List duplicateReversed();
  • 8. // creates a duplicate of the list with the nodes in reverse order // returns a copy of the linked list with the nodes in reverse order } In addition to the interface, your LinkedList class needs to implement a toString() method that prints the list in the format [ size: the_size_of_the_list - item1, item2, .... ] Note: Your Node class should be an inner class within the LinkedList class. Make sure your class implements the interface as specified, i.e. your class should begin with public class LinkedList implements List.