0% found this document useful (0 votes)
19 views11 pages

Dania

The document contains code for implementing a doubly linked list data structure with various methods. It includes methods to add elements to the front, back or a specified index of the list, remove elements from the front, back or a specified index, check if an element is contained, get an element by index, find the index of an element, and print the list. The doubly linked list implementation uses Node objects with next and previous pointers to link elements together in both directions.

Uploaded by

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

Dania

The document contains code for implementing a doubly linked list data structure with various methods. It includes methods to add elements to the front, back or a specified index of the list, remove elements from the front, back or a specified index, check if an element is contained, get an element by index, find the index of an element, and print the list. The doubly linked list implementation uses Node objects with next and previous pointers to link elements together in both directions.

Uploaded by

daniaabuali12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Q1:

public void addByIndex(int index,T value)


{
if(index<0||index>size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else if(index==0)addFirst(value);
else if(index==size)addLast(value);
else{
Node n=new Node(value);
Node curr=head;
for(int i=1;i<index;i++){
curr=curr.next;
}
n.next=curr.next;
curr.next=n;
size++;
}
}
public void addFirst(T value)
{
Node n=new Node(value);
if(head==null)
head=tail=n;
else
{
n.next=head;
head=n;
}
size++;
}

public void addLast(T value)


{
Node n=new Node(value);
if(head==null)
head=tail=n;
else
{
tail.next=n;
tail=n;
}
size++;
}
-----------------------------------------------------------------------------------
-----------------------------------------
Q2:
public T removeByIndex(int index)
{
if(index<0||index>=size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else if(head==null)return null;
else if(index==0)return removeFirst();
else{
Node curr=head;
for(int i=1;i<index;i++)
{
curr=curr.next;
}
T x=(T)curr.next.value;
curr.next=curr.next.next;
size--;
return x;
}
}
public T removeFirst()
{
if(head==null)return null;
else{
T x=head.value;
head=head.next;
size--;
if(head==null)tail=null;
return x;
}
}
-----------------------------------------------------------------------------------
---------------------------------------
Q3:
private static MyLinkedList reversedList(MyLinkedList list1) {
MyLinkedList j=new MyLinkedList();
Node t=list1.head,ans=j.head;
if(list1.head==null||list1.head.next==null)return list1;
else{
while(t.next!=null){
Node curr=list1.head,prev=list1.head;
while(curr.next!=null)
{
prev=curr;
curr=curr.next;
}
if(ans==null)
{
ans=curr;
j.head=ans;
}
else {ans.next=curr;
ans=ans.next;
}
prev.next=null;
}
ans.next=t;
return j;
}
}
-----------------------------------------------------------------------------------
----------------------------------------
Q4:
public boolean contains(T data)
{
if(head==null)return false;
else {
Node curr=head;
while(curr!=null)
{
if(curr.value.equals(data))return true;
curr=curr.next;
}
return false;
}
}

public T get(int index)


{
if(index<0||index>=size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else{
Node curr=head;
for(int i=0;i<index;i++)
curr=curr.next;
return (T)curr.value;
}
}

public int indexOf(T data)


{
boolean x=contains(data);
if(x)
{
Node curr=head;
int res=0;
for(int i=0;i<size;i++){
if(curr.value.equals(data)){
res=i;break;
}
curr=curr.next;
}
return res;
}
else return -1;
}

public int lastIndexOf(T data)


{
boolean x=contains(data);
if(x)
{
Node curr=head;
int res=0;
for(int i=0;i<size;i++){
if(curr.value.equals(data)){
res=i;
}
curr=curr.next;
}
return res;
}
else return -1;
}

public T set(int index,T data)


{
if(index<0||index>=size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else{
Node curr=head;
for(int i=0;i<index;i++)
{
curr=curr.next;
}
T x=(T)curr.value;
curr.value=data;
return x;
}
}
-----------------------------------------------------------------------------------
---------------------------------------
Q6:
public static MyLinkedList merge(MyLinkedList l1,MyLinkedList l2)
{
MyLinkedList j=new MyLinkedList();
Node ans=null,curr1=l1.head,curr2=l2.head;
if(l1.head==null&&l2.head==null)return null;
else if(l1.head==null&&l2.head!=null)return l2;
else if(l1.head!=null &&l2.head==null)return l1;
else{
while(curr1!=null)
{

if(curr2!=null&&curr2.value<=curr1.value)
{
if(ans==null){
ans=curr2;
j.head=ans;
}
else{
ans.next=curr2;
ans=ans.next;
}
curr2=curr2.next;
}
else{
if(ans==null)
{
ans=curr1;
j.head=ans;
}
else{
ans.next=curr1;
ans=ans.next;
}
curr1=curr1.next;
}
}
ans.next=curr2;
return j;
}
}
-----------------------------------------------------------------------------------
---------------------------------------
Q7:
public void addByIndex(int index,T value)
{
if(index<0||index>size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else if(index==0){
addFirst(value);
}
else if(index==size)addLast(value);
else{
Node n=new Node(value);
Node curr=head;
for(int i=1;i<index;i++)
{
curr=curr.next;
}
n.next=curr.next;
curr.next.previous=n;
curr.next=n;
n.previous=curr;
size++;
}
}
public void addFirst(T value)
{
Node n=new Node(value);
if(head==null){
head=tail=n;
head.next=null;
head.previous=null;
}
else{
n.next=head;
head.previous=n;
n.previous=null;
head=n;
}
size++;
}
public void addLast(T value)
{
Node n=new Node(value);
if(head==null){
head=tail=n;
head.previous=null;
head.next=null;
}
else {
tail.next=n;
n.previous=tail;
tail=n;
}
size++;
}
public T removeByIndex(int index)
{
if(index<0||index>=size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else if(head==null)return null;
else if(index==0){
return removeFirst();
}
else if(index==(size-1))return removeLast();
else{
Node curr=head;
for(int i=1;i<index;i++)
curr=curr.next;
T x=(T)curr.next.value;
Node temp=curr.next;
curr.next=curr.next.next;
curr.next.previous=curr;
temp.next=null;
temp.previous=null;
size--;
return x;
}
}
public T removeFirst()
{
if(head==null)return null;
else if(head.next==null)
{
T x=(T)head.value;
head=tail=null;
size--;
return x;
}
else{
T x=(T)head.value;
head=head.next;
head.previous.next=null;
head.previous=null;
size--;
return x;
}
}
public T removeLast()
{
if(head==null)return null;
else if(head.next==null)
{
T x=(T)head.value;
head=tail=null;
size--;
return x;
}
else{
Node curr=head;
while(curr.next.next!=null)
{
curr=curr.next;
}
T x=(T)curr.next.value;
tail=curr;
curr.next.previous=null;
tail.next=null;
tail.previous=curr.previous;
size--;
return x;
}
}
public void print()
{
Node curr=head;
while(curr!=null)
{
System.out.print(curr.value+" ");
curr=curr.next;
}
System.out.println("");
}
public boolean contains(T data)
{
if(head==null)return false;
else {
Node curr=head;
while(curr!=null)
{
if(curr.value.equals(data))return true;
curr=curr.next;
}
return false;
}
}
public T get(int index)
{
if(index<0||index>=size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else{
Node curr=head;
for(int i=0;i<index;i++)
curr=curr.next;
return (T)curr.value;
}
}
public int indexOf(T data)
{
boolean x=contains(data);
if(x)
{
Node curr=head;
int res=0;
for(int i=0;i<size;i++){
if(curr.value.equals(data)){
res=i;break;
}
curr=curr.next;
}
return res;
}
else return -1;
}
public int lastIndexOf(T data)
{
boolean x=contains(data);
if(x)
{
Node curr=head;
int res=0;
for(int i=0;i<size;i++){
if(curr.value.equals(data)){
res=i;
}
curr=curr.next;
}
return res;
}
else return -1;
}
public T set(int index,T data)
{
if(index<0||index>=size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else{
Node curr=head;
for(int i=0;i<index;i++)
{
curr=curr.next;
}
T x=(T)curr.value;
curr.value=data;
return x;
}
}
-----------------------------------------------------------------------------------
---------------------------------------
Q8:
public void addByIndex(int index,T value)
{
if(index<0||index>size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else if(index==0)addFirst(value);
else if(index==size)addLast(value);
else{
Node n=new Node(value);
Node curr=head;
for(int i=1;i<index;i++){
curr=curr.next;
}
n.next=curr.next;
curr.next=n;
size++;
}
}
public void addFirst(T value)
{
Node n=new Node(value);
if(head==null){
head=tail=n;
tail.next=head;
}
else{
n.next=head;
head=n;
tail.next=head;
}
size++;
}
public void addLast(T value)
{
Node n=new Node(value);
if(head==null){
head=tail=n;
tail.next=head;
}
else{
tail.next=n;
tail=n;
tail.next=head;
}
size++;
}
public void print()
{
if(head==null)System.out.println("empty");
else{
Node curr=head;
System.out.print(curr.value+" ");
curr=curr.next;
while(curr!=head)
{
System.out.print(curr.value+" ");
curr=curr.next;
}
System.out.println("");
}
}
public T removeByIndex(int index)
{
if(index<0||index>=size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else if(head==null)return null;
else if(index==0)return removeFirst();
else if(index==(size-1))return removeLast();
else{
Node curr=head;
for(int i=1;i<index;i++)
{
curr=curr.next;
}
T x=(T)curr.next.value;
curr.next=curr.next.next;
size--;
return x;
}
}
public T removeFirst()
{
if(head==null)return null;
else if(head.next==head){
T x=(T)head.value;
head=tail=null;
size--;
return x;
}
else{
T x=(T)head.value;
head=head.next;
tail.next=head;
size--;
return x;
}
}
public T removeLast()
{
if(head==null)return null;
else if(head.next==head){
T x=(T)head.value;
head=tail=null;
size--;
return x;
}
else{
Node curr=head;
while(curr.next!=tail)
{
curr=curr.next;
}
T x=(T)tail.value;
tail=curr;
curr.next=head;
size--;
return x;
}
}
public boolean contains(T data)
{
if(head==null)return false;
else {
Node curr=head;
if(curr.value.equals(data))return true;
curr=curr.next;
while(curr!=head){
if(curr.value.equals(data))return true;
curr=curr.next;
}
return false;
}
}
public T get (int index)
{
if(index<0||index>=size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else{
Node curr=head;
for(int i=0;i<index;i++)
curr=curr.next;
return (T)curr.value;
}
}
public int indexOf(T data)
{
boolean x=contains(data);
if(x)
{
Node curr=head;
int res=0;
for(int i=0;i<size;i++){
if(curr.value.equals(data)){
res=i;break;
}
curr=curr.next;
}
return res;
}
else return -1;
}
public int lastIndexOf(T data)
{
boolean x=contains(data);
if(x)
{
Node curr=head;
int res=0;
for(int i=0;i<size;i++){
if(curr.value.equals(data)){
res=i;
}
curr=curr.next;
}
return res;
}
else return -1;
}
public T set(int index,T data)
{
if(index<0||index>=size)throw new ArrayIndexOutOfBoundsException("invalid
index");
else{
Node curr=head;
for(int i=0;i<index;i++)
{
curr=curr.next;
}
T x=(T)curr.value;
curr.value=data;
return x;
}
}

You might also like