JAVA & DATA
STRUCTURE
(5 marks)
1)FEATURES OF JAVA:
Object-Oriented: Everything is an object, allowing easy extension based on
the object model.
Platform Independence: "Write Once, Run Anywhere" (WORA) capability.
Code compiles into bytecode runnable on any platform with a Java Virtual
Machine (JVM).
Simple: Designed for easy learning and mastery, especially if you understand
OOP concepts.
Multithreaded: Supports writing programs that perform multiple tasks
simultaneously, ideal for interactive applications.
Robust: Emphasizes compile-time and runtime error checking to minimize
error-prone situations.
Secure: Enables the development of virus-free and tamper-free systems with
public-key encryption-based authentication.
High Performance: Utilizes Just-In-Time compilers to achieve high
performance.
Distributed: Specifically designed for the distributed environment of the
internet.
Architecture-Neutral: Generates architecture-neutral object files, making
compiled code executable on various processors with Java runtime system.
Portability: Java's architecture-neutral and implementation-independent
aspects make it portable across different platforms.
Interpreted: Java bytecode is translated on-the-fly to native machine
instructions without being stored.
Dynamic: Supports dynamic loading of classes and dynamic compilation,
offering flexibility during runtime.
2)Data types in java
3)Switch statement with example
In Java, the `switch` statement is used to execute different blocks of code
based on the value of an expression.
It is often used as an alternative to a series of `if-else` statements when there
are multiple cases to consider.
Syntax:
switch (expression) {
case 1:
// code block
break;
case 2:
// code block
break;
default:
// code block
}
Example:
public class Switch {
public static void main(String[] args) {
int choice = 2;
switch (choice) {
case 1:
[Link]("You chose option 1");
break;
case 2:
[Link]("You chose option 2");
break;
case 3:
[Link]("You chose option 3");
break;
default:
[Link]("Invalid choice");
}
}
}
Output:
You chose option 2
4)Deletion of node at front and rear in singly linked list
(i) Delete from front:
If head is null, then print the list is empty and quit, else initialize the pointer t
To point to head node.
Print data of node pointed by t.
Move head to point to next return the node pointed by t to memory.
(ii) Delete from rear:
If head is null, then print the list is empty and quite else initialize the pointer
cur and prev to point to head node, traverse the list so that cur points to last
node in list and prev point to previous node of cur.
If there is only one node in the list then assigns the null value to head else
assign a null value to link part of the node pointed by prev.
Return the node pointed by cur to memory.
5)Insertion of node at front and rear in doubly linked list.
(i) Insert at the front:
Create a new node read and assign the data part of the node.
Initialize the left pointer of new node to null and right to pointer to point
head node.
If head is not null then adjust the left pointer of head to point to this new
node.
(ii) Insert at rear:
Create a new node read and assign the data part of the node.
Initialize the pointer left and right of new node to null.
If head is null make head to point to this new node else traverse to the last
node in the list.
Assign in the left pointer of the new node to point to the last node.
Adjust the right pointer of last node to point to new node.
(10 Marks)
1)Insert operation in singly linked list
Operations for singly clinked list
1. Search for a node in the list
2. we can detect and retrieve a specific node either from front, the end or
anywhere in the list
3. Add a node to the list.
4. we can add a node at the front at the end or anywhere in the list.
a) Insert at front
Create a new node, read and assign the data part of the node.
Initialize the link part of new node to point to head node.
Move head to point to this new node.
b) Insert at rear
Create a new node read and assign the data part of the node.
Initialize the link part of new node to NULL.
If the head is NULL make head to point to the node. Else traverse to the list
node in the list adjusted the link part of last node to point the new.
c) Insert by position
Read the position initialize the pointer cur & prev. To point to the head node.
To traverse the list so that cur Reacher the appropriate position and prev
points to previous node of cur.
If position is beyond last node in the then print insertion is unsuccessful and
quit.
Create a new node, read and assign the data part of the node.
If cur is Position in the first, the first node then Initialize the link part of the
new node to either cur or prev.
Move head to point to this new node else adjust the link part of previous
node by prev to the new node.
Assign the part of new node to point to current node pointed by cur.
2) Operators in java
Java Operators:
Operators are used to perform operations on variables and values.
Java divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
1)Arithmetic operators are used to perform common mathematical operations.
2)Assignment operators are used to assign values to variables.
3)Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make
decisions.
4) Logical Operators
You can also test for true or false values with logical operators.
Logical operators are used to determine the logic between variables or values: