Data Structure
Data Structure
By Ramana Sir.
Q1 :- ArrayList.
public class Node
{
Object ele;
Node next;
public Node(Object ele,Node next)
{
this.ele = ele;
this.next = next;
}
public Node(Object ele)
{
this.ele = ele;
}
}
public class ArrayList
{
Object [] a = new Object[5];
int count = 0;
public void add(Object ele)
{
if(count==a.length)increase();
a[count] = ele;
count++;
}
public void increase()
{
Object [] a1 = new Object[a.length + 5];
for(int i = 0;i<size();i++)
{
a1[i] = a[i];
}
a = a1;
}
public int size()
{
return count;
}
public Object get(int index)
{
if(index<0||index>size())
throw new IndexOutOfBoundsException();
return a[index];
}
public int indexOf(Object ele)
{
for(int i = 0;i<size();i++)if(a[i]==ele)return i;
return -1;
}
public void set(Object ele,int index)
{
if(index<0||index>size())
throw new IndexOutOfBoundsException();
a[index] = ele;
}
public void addPos(Object ele,int index)
{
if(index<0||index>size())
throw new IndexOutOfBoundsException();
if(index==count)increase();
for(int i = size();i>index;i--)a[i] = a[i-1];
a[index] = ele;
count++;
}
public Object remove(int index)
{
if(index<0||index>size())
throw new IndexOutOfBoundsException();
Object ele = a[index];
for(int i = index+1;i<size();i++)a[i-1] = a[i];
count--;
return ele;
}
}
Q2 :- LinkedList.
public class Node
{
Object ele;
Node next;
public Node(Object ele,Node next)
{
this.ele = ele;
this.next = next;
}
public Node(Object ele)
{
this.ele = ele;
}
}
public class LinkedList
{
Node head;
Node tail;
int count = 0;
public void insertFirst(Object ele)
{
Node node = new Node(ele);
node.next = head;
head = node;
count++;
if(tail==null)tail = head;
}
public int size()
{
return count;
}
public void display()
{
Node temp = head;
while(temp!=null)
{
System.out.print(temp.ele + " ");
temp = temp.next;
}
}
public void add(Object ele)
{
if(count==0)
{
insertFirst(ele);
return;
}
Node temp = head;
while(temp.next!=null)temp = temp.next;
temp.next = new Node(ele,null);
count++;
}
public void inserLast(Object ele)
{
Node temp = head;
while(temp.next!=null)temp = temp.next;
Node node = new Node(ele);
temp.next = node;
tail = node;
tail.next = null;
count++;
}
public void insertInBetween(Object ele,int index)
{
Node temp = head;
for(int i = 1;i<index;i++)temp = temp.next;
Node node = new Node(ele,temp.next);
temp.next = node;
count++;
}
public Object getEle(int index)
{
if(index<0||index>size())
throw new IndexOutOfBoundsException();
Node temp = head;
for(int i = 0;i<index;i++)temp = temp.next;
return temp.ele;
}
public Object removeFirst()
{
Object ele = head.ele;
head = head.next;
if(head==null)tail = null;
count--;
return ele;
}
public Object removeInBetween(int index)
{
if(index<0||index>size())
throw new IndexOutOfBoundsException();
if(index==0)return removeFirst();
Node temp = head;
for(int i = 0;i<index;i++)temp = temp.next;
Object ele = temp.next.ele;
temp.next = temp.next.next;
count--;
return ele;
}
public Node getAdd(int index)
{
Node temp = head;
for(int i = 0;i<index;i++)temp = temp.next;
return temp;
}
public Object removeLast()
{
if(count==1)return removeFirst();
Object ele = tail.ele;
Node secondLast = getAdd(size()-2);
tail = secondLast;
tail.next = null;
count--;
return ele;
}
}
public class LinkedListDriver
{
public static void main(String[] args)
{
LinkedList a = new LinkedList();
a.add(10);
a.add(20);
a.add(30);
a.inserLast(40);
a.display();
a.insertInBetween(25, 2);
System.out.println();
a.removeInBetween(3);
a.display();
}
}
Q3 :- Double LinkedList.
public class Node {
Node pre;
Object ele;
Node next;
Node(Object ele) {
this.ele = ele;
}
}
============================================================
public class DoubleLinkedList {
Node head;
int count = 0;
Node last;
count++;
}
}
public Object remove(int index) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException();
if (index == 0) {
return removeFirst();
}
if (index == size()) {
return removeLast();
}
Node temp = head;
for (int i = 1; i < index; i++) {
temp = temp.next;
}
Object ele = temp.next.ele;
temp.next = temp.next.next;
temp.next.pre = temp;
count--;
return ele;
}
}
============================================================
public class UserProg {
public static void main(String[] args) {
DoubleLinkedList d = new DoubleLinkedList();
d.add(10);
d.add(20);
d.add(30);
d.add(40);
d.add(50);
d.insertFirst(5);
d.insertLast(60);
d.insert(25, 3);
d.addAfter(30, 35);
System.out.println(d.removeFirst());
System.out.println(d.removeLast());
System.out.println(d.remove(5));
System.out.println(d.get(2));
d.display();
d.reverse();
System.out.println("===================");
System.out.println(d.size());
Q4 :- Circular LinkedList.
public class Node {
Object ele;
Node next;
Node(Object ele) {
this.ele = ele;
}
}
---------------------------------------------------------
public class CircularLinkedList {
Node head;
int count = 0;
Node tail;
}
-----------------------------------------------------------------------
public class UserProg {
public static void main(String[] args) {
CircularLinkedList c = new CircularLinkedList();
c.insert(10);
c.insert(20);
c.insert(30);
c.insert(40);
c.insert(50);
System.out.println(c.size());
c.display();
System.out.println("-----------");
c.delete(50);
c.display();
System.out.println(c.size());
}
Q5 :- Queue.
public class Node {
Object ele;
Node next;
Node(Object ele) {
this.ele = ele;
}
}
--------------------------
public class Queue {
Node head;
int count = 0;
Node tail;
while(!q.isEmpty())System.out.println(q.poll());
System.out.println(q.isEmpty());
System.out.println(q.size());
}
Q6 Stack :-
public class Node {
Object ele;
Node next;
Node(Object ele) {
this.ele = ele;
}
------------------------------------
public class Stack {
Node first;
int count = 0;
}
--------------------------------------------------
public class UserProg {
public static void main(String[] args) {
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
System.out.println(s.isEmpty());
System.out.println(s.size());
System.out.println(s.peek());
System.out.println("-----------------");
while (!s.isEmpty())
System.out.println(s.pop());
System.out.println(s.size());
System.out.println(s.isEmpty());