Java Assignment
Java Assignment
Fall 2018
Individual Assessment
Submitted by
Name:
ID:
Ans.1) Difference Between Stack and Queue
Definition
Stack
A Stack is a non-primitive linear data structure. It is an ordered list where the new item is
added and existing element is deleted from only one end, called as the top of the stack
(TOS). As all the deletion and insertion in a stack is done from the top of the stack, the last
element added will be the first to be removed from the stack. That is the reason why stack
is called Last-in-First-out (LIFO) type of list.
Queue
A queue is a linear data structure comes in the category of the non-primitive type. It is a
collection of similar type of elements. The addition of new elements takes place at one end
called rear end. Similarly, deletion of the existing elements takes place at the other end
called the Front-end, and it is logically a First in first out (FIFO) type of list.
2
When an element is deleted from the top of the stack it is known as POP. The stack is
decreased by one, after every pop operation. If there is no element left on the stack and the
pop is performed, then this will result in STACK UNDERFLOW condition which means your
stack is Empty. POP OPERATION – functions in C:
Considering stack is declared as
int stack [5], top = -1;
void pop()
{
int item;
if (top >= 4)
{
item = stack [top];
top = top - 1;
printf ("Number deleted is = %d", item) ;
}
else
{
printf (" Stack is empty");
}
}
Queue
To insert an element in a queue. Enqueuing function in C:
Queue is declared as
int queue [5], Front = -1 and rear = -1;
void add ()
{
int item;
if ( rear < 4)
{
printf ("Enter the number") ;
scan ("%d", & item) ;
if (front == -1)
{
front =0 ;
rear =0 ;
}
else
{
rear = rear + 1;
}
queue [rear] = item ;
}
else
{
3
printf ("Queue is full") ;
}
}
To delete an element from the queue. Dequeue function in C:
Queue is declared as
4
Operations
Stack
Stack uses ‘top’ pointer which is always pointed to the most recent added element in the
stack.
5
Queue
Unlike stack data structure, queue requires two pointers so-called FRONT and REAR.
6
Applications
Stack
Parsing in a compiler.
Java virtual machine.
Undo in a word processor.
Back button in a Web browser.
PostScript language for printers.
Implementing function calls in a compiler.
Queue
Data Buffers
Asynchronous data transfer (file IO, pipes, sockets).
Allotting requests on a shared resource (printer, processor).
Traffic analysis.
Determine the number of cashiers to have at a supermarket.
7
Ans.2): Java function (method) max2List
import java.util.Scanner;
/* Class Node */
class Node
{
protected int data;
protected Node next;
/* Constructor */
public Node()
{
next = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
next = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
next = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
8
/* Function to get link to next node */
public Node getLink()
{
return next;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
}
/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size ;
/* Constructor */
public linkedList()
{
start = null;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
9
public int getSize()
{
return size;
}
/* Function to insert an element at begining */
}
else
{
Node last = start;
while (last.next != null)
last = last.next;
}
public linkedList max( linkedList one, linkedList two)
10
{
linkedList three=new linkedList();
Node p=one.start;
Node q= two.start;
while(p!=null)
{
if(p.data>q.data)
three.insertAtEnd(p.data);
else
three.insertAtEnd(q.data);
p=p.next;
q=q.next;
}
return three;
}
/* Function to insert an element at position */
11
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ "->");
ptr = start.getLink();
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ "\n");
}
}
/* Class SinglyLinkedList */
public class sll
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list1 = new linkedList();
linkedList list2 = new linkedList();
linkedList list3 = new linkedList();
linkedList list4 = new linkedList();
System.out.println("Singly Linked List Test\n");
char ch;
/* Perform list operations */
12
do
{
System.out.println("\nSingly Linked List Operations\n");
System.out.println("1. insert in First Linked List");
System.out.println("2. insert in Second Linked List");
System.out.println("3. Max");
13
}
/* Display List */
//list3.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
14
Works Cited
Chaudhari, A., 2017. Difference Between Stack and Queue in Data Structure. [Online]
Available at: https://www.csestack.org/difference-between-stack-and-queue/
[Accessed 26 November 2018].
15