0% found this document useful (0 votes)
9 views

Assignment #01 DSA

Uploaded by

Soban Janjua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Assignment #01 DSA

Uploaded by

Soban Janjua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structure and Algorithm

Assignment No #01
Submitted By:
 Muhammad Soban Rasheed
Roll Number:
 FA20-BCS-020
Submitted To:
 Sir Javid Ali
Section:
 BCS ‘4A’
Program No #01
Inserting At Start:
public class Assignment_01 {
node head;
class node{
String data;
node previous;
node next;
public node(String data){
this.data=data;
this.previous=null;
this.next=null;
}
}
public void addstart(String data){
node newnode=new node(data);
if (head==null){
head=newnode;
return;
}
newnode.next=head;
head=newnode;
}

public void display(){


node curl=head;
while (curl!=null){
System.out.print(curl.data +"<");
curl=curl.next;
}
}

public static void main(String[] args) {


Assignment_01 sc=new Assignment_01();
sc.addstart("Rasheed");
sc.addstart("Soban");
sc.addstart("Muhammad");
sc.display();

}
}

Ouput:
Program No #02
Inserting at last:
public class insertat_last {
node head;
node previous;

class node {
String data;
node previous;
node next;

public node(String data) {


this.data = data;
this.previous = null;
this.next = null;
}
}

public void insertlast(String data) {


node newnode = new node(data);
if (head == null) {
head = newnode;
previous = newnode;
return;
}
head=head.previous;
head.previous=newnode;
newnode=previous;
}
public void display(){
node curl=head;
while (curl!=null){
System.out.print(curl.data +"<");
curl=curl.next;
}
}

public class insertat_last {


public static void main(String[] args) {
insertat_last sc=new insertat_last();

sc.insertlast("Fa20");
sc.display();
System.out.println("");
System.out.println("After inserting");
sc.insertlast("11");
sc.display();

}
}

Output:

Program No #03
Deleting at last
public class deletelast {
node head;
class node {
String data;
node next;
node previous;

public node(String data) {


this.data=data;
this.next = null;
this.previous = null;
}
}
public void addstart(String data){
node newnode =new node(data);
if (head==null){
head=newnode;
return;
}
newnode.next=head;
head=newnode;
}
public void dletelst(){
if (head==null){
System.out.println("List is Empty");
return;
}

head.previous=head.previous.previous;
}

public void display(){


node curl=head;
while (curl!=null){
System.out.print(curl.data +"<");
curl=curl.next;
}
}

public static void main(String[] args) {


deletelast sd=new deletelast();
sd.addfirst("Rasheed");
sd.addfirst("Soban");
sd.addfirst("Muhammad");
sd.display();
System.out.println("");
System.out.println("After deleting");
sd.deletelast("");
sd.display();

}
}

Output:

Program No #04
Reverse an Array:
import java.util.Scanner;

public class Reverse_array {


public static void main(String[] args) {

int arr[] = {2, 4, 5, 6, 7, 9};

for (int i = arr.length - 1; i >= 0; i--) {

System.out.println("" + arr[i]);

}
}
}

Output:

THE END….

You might also like