Skip to content

Commit

Permalink
assignment 7 completed, major changes expected to occur in future
Browse files Browse the repository at this point in the history
  • Loading branch information
MartyMiniac committed Mar 18, 2020
1 parent f19939d commit 2294953
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 0 deletions.
75 changes: 75 additions & 0 deletions Semester 2 (DSA)/assignment 7/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
public class LinkedList
{
Node head;
LinkedList()
{
head=null;
}
//Insertion
public void add(int data)
{
Node t=head;
if(head==null)
{
head = new Node(data);
}
else
{
while(t.next!=null)
{
t=t.next;
}
t.next= new Node(data);
}
}
//Deletion
public void del()
{
if(head==null)
System.out.println("Stack Underflow");
else
head=head.next;
}
public void DelEnd()
{
int len=this.count();
len=len-1;
Node n=head;
int c=0;
while(n.next!=null)
{
c++;
if(c==len)
break;
n=n.next;
}
n.next=null;
}
public int count()
{
int c=0;
Node n=head;
while(n.next!=null)
{
c++;
n=n.next;
}
c++;
return c;
}
public void display()
{
Node n=head;
if(n==null)
System.out.println("Queue is Empty");
else
{
while(n.next!=null)
{
System.out.println(n);
n=n.next;
}
System.out.println(n);
}
}
}
23 changes: 23 additions & 0 deletions Semester 2 (DSA)/assignment 7/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class Node
{
protected Node next;
protected int data;

Node()
{
next=null;
}
Node(int data)
{
this.data=data;
next=null;
}
int getdata()
{
return data;
}
public String toString()
{
return data+"";
}
}
37 changes: 37 additions & 0 deletions Semester 2 (DSA)/assignment 7/part1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.*;
public class part1
{
public static void main(String args[])
{
queue q = new queue(5);
Scanner in = new Scanner(System.in);
while(true)
{
System.out.println("***MENU***");
System.out.println("0: Exit");
System.out.println("1: Insert");
System.out.println("2: Delete");
System.out.println("3: Display");
System.out.println("Enter your choice");
int choice=in.nextInt();
switch(choice)
{
case 0:
System.exit(0);
case 1:
System.out.print("Enter the value to insert :");
int i=in.nextInt();
q.insert(i);
break;
case 2:
q.delete();
break;
case 3:
q.display();
break;
default:
System.out.println("Invalid choice");
}
}
}
}
37 changes: 37 additions & 0 deletions Semester 2 (DSA)/assignment 7/part2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.*;
public class part2
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
LinkedList q = new LinkedList();
while(true)
{
System.out.println("***MENU***");
System.out.println("0: Exit");
System.out.println("1: Insert");
System.out.println("2: Delete");
System.out.println("3: Display");
System.out.println("Enter your choice");
int choice=in.nextInt();
switch(choice)
{
case 0:
System.exit(0);
case 1:
System.out.print("Enter the value to insert :");
int i=in.nextInt();
q.add(i);
break;
case 2:
q.del();
break;
case 3:
q.display();
break;
default:
System.out.println("Invalid choice");
}
}
}
}
44 changes: 44 additions & 0 deletions Semester 2 (DSA)/assignment 7/queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class queue
{
int Q[];
int rear, front;

queue(int n)
{
Q = new int[n];
rear=0;
front=0;
}
public void insert(int i)
{
if(front==Q.length)
System.out.println("Stack Overflow");
else
{
Q[front]=i;
front++;
}
}
public void delete()
{
if(front==rear)
System.out.println("Stack Underflow");
else
rear++;
}
public void display()
{
for(int i=rear; i<front; i++)
{
System.out.println(Q[i]);
}
}
public boolean is_full()
{
return front==Q.length-1;
}
public boolean is_empty()
{
return front==rear;
}
}

0 comments on commit 2294953

Please sign in to comment.