0% found this document useful (0 votes)
5 views6 pages

Array Codes

The document provides a comprehensive guide on array and linked list operations in Java. It includes examples of declaring, initializing, printing, reversing, merging, sorting arrays, and finding minimum and maximum values, as well as handling duplicates. Additionally, it covers linked list implementation with methods for inserting nodes at the beginning and at a specific position.

Uploaded by

Noorulain Memon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Array Codes

The document provides a comprehensive guide on array and linked list operations in Java. It includes examples of declaring, initializing, printing, reversing, merging, sorting arrays, and finding minimum and maximum values, as well as handling duplicates. Additionally, it covers linked list implementation with methods for inserting nodes at the beginning and at a specific position.

Uploaded by

Noorulain Memon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Programming of array

Declare, initialize and print array in java


Int arr[]=new int[3]
Int arr[0]=100;
Int arr[1]=101;
Int arr[2]=102;
[Link](arr[0]);
[Link](arr[1]);
[Link](arr[2]);
String array and traversing array
String [] arr={ “Noor” ,”sania”,”afaque”};
1:For(int i=0;i<[Link]; i++){
S.o.p(arr[i]);
2: For(String arr: names){
s.o.p(arr);
Reversing the string array
String [] names= {“Him”, “me”, “my”}
For (int I = [Link]-1; i>=0; i--)
S.o.p(names[i]);
Reverse the array without forming any other empty array:
[1,2,3,4,5,6,7] <-> [7,6,5,4,3,2,1]
Code: void reverseArray(int Arr[], int len)
{ for(int i=0, i<len/2, ++i){ std::swap(Arr[i], Arr(len-i-1)}

Merge two arrays into one


Int [] a = {1,2,3}
Int [] b = {4,5,6}
Int [] c = new int [[Link]+[Link]];
For(int i=0; i<[Link]; i++){
C[i]=a[i];}
For (int i=0; i<[Link]; i++){
C[I + [Link]] = b[i];}
s.o.p([Link](c));
Sort the array in ascending order {4,2,3,1} to [1,2,3,4}
Int [] a= {4,2,3,1}
For(int I =0; i<[Link]; i++){
For (int j=i+!; j<[Link];j++){
If(a[i] > a[j]){ int temp= a[i];
A[i]=a[j];
A[j]=temp;}}}s.o.p([Link](A));
Print the duplicate elements in the array
Int [] a = {4,2,4,5,6,5,7,9,0,1};
For (int i=0; i<[Link];I ++){
For (int j=i+1; j<[Link]; j++){
If(A[i] == a[j]){
S.o.p(a[i]);
Find Min and Max in the array
Int [] arr ={4,3,2,0,1,99};
Int min=arr[0], max=arr[0];
For(int i=0; i<[Link];i++){
If(arr[i]<min){
Min=arr[i];}
If(arr[i]>max){
Max=arr[i];}}
S.o.p(Min+” “ + max)

LINKEDLIST CODES
// Node classpublic class Node {
int data;
Node next;
public Node(int data) { [Link] = data; [Link] = null; }}
// LinkedList classpublic class LinkedList {
Node head;
public void insert(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode; }
else {
Node current = head;
while ([Link] != null) {
current = [Link]; }
[Link] = newNode; }}
public void display() {
Node current = head;
if (head == null) {
[Link]("List is empty");
return; }
while (current != null) {
[Link]([Link] + " ");
current = [Link]; } [Link](); }}

Inserting new Node at beginning :


Algorithm:
 Make the first node of Linked List linked to the new node
 Remove the head from the original first node of Linked List
 Make the new node as the Head of the Linked List.

class Node {
int data;
Node next;
// Constructor to initialize the node
Node(data) {
[Link] = data;
next = null; }}
class GfG {
// Function to insert a new node at
// the beginning of the list
static Node insertAtFront(Node head, int x) {
Node newNode = new Node(x);
[Link] = head;
head = newNode;} }

Inserting node at a given position


 Traverse the linked list to find the given node.
 If the given node is not found, print "Node not found".
 Else if the given node is found, create a new node,
say new_node initialized with the given data.
 Make the next pointer of new_node as next of given node.
 Update the next pointer of given node point to the new_node.
public static Node insertAfter(Node head, int key, int newData) {
// Initialize curr Pointer to head
Node curr = head;
// Iterate over Linked List to find the key
while (curr != null) {
if ([Link] == key)
break;
curr = [Link];
} Node newNode = new Node(newData);
// Set the next pointer of new node to the next
// pointer of given node
[Link] = [Link];
// Change the next pointer of given node to the new node
[Link] = newNode;
// Return the head of the modified linked list
return head;
}

You might also like