Java Programming 2: LinkedList Implementation and Methods, Exercises of Java Programming

The uml diagram and code snippets for implementing methods in a linkedlist class in java, including 'find', 'printlast', and 'removeall'. It also includes a code segment for calculating the sum of all elements in an intlinkedlist and finding the node at an odd position.

Typology: Exercises

2020/2021

Uploaded on 04/20/2021

shamma-alshamikh
shamma-alshamikh 🇺🇸

5

(1)

6 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
King Saud University
College of Computer and Information Sciences
Computer Science Department
CSC#113:#Java#Programming#2#
Tutorial##10#
Spring#2019##
#"Data$Structures$$$1"
1"
Given the Following UML:
LinkedList
-head: Node
-tail:Node
+LinkedList()
+isEmpty():Boolean
+size():int
+insertAtFront(int): void
+insertAtBack(int):void
+removeFromFront():Integer
+removefromBack():Integer
1- Add a method called find” to the class “LinkedList” that receives an integer and
returns an index of the first node containing that integer -1 if not found.
2- Add a method called printLast” to the class “LinkedList” that print the data in the last
node.
3- In another class, implement a method called “find” that receives a reference to a
LinkedList object and an integer, and returns an index of the first node containing that
integer -1 if not found. This method should not change the elements order
4- Consider the following method removeAll that receives a LinkedList object and an
integer. The method should remove all nodes containing that integer from the received
list.
public void removeAll (LinkedList myList, int x)
{
if (myList.isEmpty())
return;
int i=0;
while (i<myList.size())
{
int temp= myList."removeFromFront();
if (temp!=x)
myList."insertAtBack(temp)
i++;
}}
Node
- data:Integer
- next: Node
+ Node (in i: Integer )
+ setData(in i: Integer )
+getData(): Integer
+setNext(in n:Node)
+getNext() : Node
pf2

Partial preview of the text

Download Java Programming 2: LinkedList Implementation and Methods and more Exercises Java Programming in PDF only on Docsity!

King Saud University College of Computer and Information Sciences Computer Science Department CSC 113: Java Programming 2 Tutorial # 10 Spring 2019

Data Structures 1 1

Given the Following UML: LinkedList

  • head: Node
  • tail:Node +LinkedList() +isEmpty():Boolean +size():int +insertAtFront(int): void +insertAtBack(int):void +removeFromFront():Integer +removefromBack():Integer 1 - Add a method called “ find ” to the class “ LinkedList ” that receives an integer and returns an index of the first node containing that integer - 1 if not found. 2 - Add a method called “ printLast ” to the class “ LinkedList ” that print the data in the last node. 3 - In another class, implement a method called “ find ” that receives a reference to a LinkedList object and an integer, and returns an index of the first node containing that integer - 1 if not found. This method should not change the elements order 4 - Consider the following method removeAll that receives a LinkedList object and an integer. The method should remove all nodes containing that integer from the received list. public void removeAll (LinkedList myList, int x) { if (myList.isEmpty()) return; int i=0; while (i King Saud University College of Computer and Information Sciences Computer Science Department CSC 113: Java Programming 2 Tutorial # 10 Spring 2019

Data Structures 1 2

Q 2 : Given the Following UML: IntLinkedList

  • head: IntNode
  • tail : IntNode +LinkedList() +isEmpty():Boolean +size():int +insertAtFront(in i:int): void +insertAtBack(in i:int):void +removeFromFront():int +removefromBack():int +print():void +sum():int 1. State if the following code segments run successfully or not IntNode
  • data:int
  • next: IntNode
  • Node (in i: int) Sum of all elements int sum=0; IntNode c=head; while (c !=null) sum+=c.data; return sum; Print last System.out.print (tail.next.data); Find previous node of the last node IntNode c=head; while (c.next.next !=null) c=c.next ; Print node at odd position IntNode c=head; while (c! =null){ System.out.print (c.data+” ”); if(c.next!=null) c=c.next.next; else break;}