You are on page 1of 15

Data Structure

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.

Principles used to insert/ remove an object


Stack
when a new element is added to the top of the stack is known as PUSH. Pushing an element
in the stack invokes adding of the element, as the new element will be inserted at the top.
After each push operation, the top is increased by one. If the array is full, and no new
element can be added, it is called STACK-FULL condition or STACK OVERFLOW. PUSH
OPERATION – function in C:
Considering stack is declared as
int stack [5], top = -1;
void push()
{
int item;
if (top < 4)
{
printf ("Enter the number") ;
scan ("%d", & item) ;
top = top + 1;
stack [top] = item;
}
else
{
printf (" Stack is full");
}
}

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

int queue [5], Front = -1 and rear = -1;


void delete ()
{
int item;
if ( front ! = -1)
{
item = queue [ front ] ;
if (front == rear)
{
front =-1 ;
rear =-1 ;
}
else
{
front = front + 1;
printf ("Number deleted is = %d", item) ;
}
}
else
{
printf ("Queue is empty") ;
}
}

4
Operations
Stack
Stack uses ‘top’ pointer which is always pointed to the most recent added element in the
stack.

PUSH Operation on Stack:


The operation of adding an element to the stack is PUSH operation.
Steps:
 Find the top pointer of the stack.
 Add object to the top of the stack
 Increment the top pointer and point it to the newly added object.
POP Operation on Stack:
The operation of removing top elements from the stack is PUSH operation.
Steps:
 Check if the stack is not empty.
 Access the topmost object using the top pointer. (Save the object in a local variable
to use in the application.)
 Remove the object from the top.
 Point the ‘top’ pointer to next object in the stack.

5
Queue
Unlike stack data structure, queue requires two pointers so-called FRONT and REAR.

Enqueue operation on Queue:


The operation of adding an element in the queue is Enqueue operation.
Steps:
 Find the REAR pointer of the queue.
 Increment the REAR pointer.
 Add new object at the REAR pointer.
Dequeue operation on Queue:
The operation of removing an element from the queue is Dequeue operation.
Steps:
 Ensure if the queue is not empty.
 Get access to the object pointed by the FRONT pointer.
 Remove the object. (Store in a local variable to use in the application.)
 Increment the FRONT pointer to point it to next front object.

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 */

/* Function to insert an element at end */


public void insertAtEnd(int val)
{
Node nptr = new Node(val,null);
size++ ;
if(start == null)
{
start = nptr;

}
else
{
Node last = start;
while (last.next != null)
last = last.next;

/* Change the next of last node */


last.next = nptr;
}
return;

}
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 */

/* Function to delete an element at position */

/* Function to display elements */


public void display()
{
System.out.print("\nSingly Linked List = ");
if (size == 0)
{
System.out.print("empty\n");
return;
}
if (start.getLink() == null)

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");

int choice = scan.nextInt();


switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list1.insertAtEnd( scan.nextInt() );
list1.display();
break;
case 2 :
System.out.println("Enter integer element to insert");
list2.insertAtEnd( scan.nextInt() );
list2.display();
break;
case 3 :
//System.out.println("Enter integer element to insert");
list3=list4.max(list1,list2);
list3.display();

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].

Kumar, L., 2016. Differences Between Stack and Queue. [Online]


Available at: https://techwelkin.com/differences-between-stack-and-queue
[Accessed 26 November 2018].

Tech Differences, 2017. Difference Between Stack and Queue. [Online]


Available at: https://techdifferences.com/difference-between-stack-and-queue.html
[Accessed 26 November 2018].

15

You might also like