Demo For Comsats

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

PRESENTED BY: MARY AKSA

MS COMPUTER SCIENCE

DATA STRUCTURES
TABLE OF CONTENT

1. Data Structure
2. Types of Data
3. Classification of Data Structure
4. Stack
5. Queue
WHY WE NEED TO ORGANIZE OR
STRUCTURE?
• Data: Collection of raw facts
• Data Structure is a representation of logical relationship existing between individual
elements of data.
• Data structure is a specialized format for organzing and storing data in memory that
considers not only the elements stored but also their relationship to each other.
• Affects the structural and functional aspects of a program.
• Program = Algorithm + Data Structure
CATEGORIES OF DATA STRUCTURE

• Primitive and Non-primitive Data structure

• Primitive Data Structure: directly operated upon the machine level instructions. E.g
Integers, float, character, pointers.

• Non-primitive Data Structure: emphasize on structuring of a group of homogeneous or


heterogeneous data items.
TWO WAYS TO IMPLEMENT STACKS:

• Arrays: Static
• Pointers: Dynamic (DFS)
OPERATIONS:

• Stack(): it creates a new stack that is empty. It needs no parameters and returns an empty stack.
• push(obj): Add object obj at the top of the stack.
• Obj Pop():it removes the top item from the stack.
• Obj peek(): it removes the top item from the stack but doesn’t remove.
• isEmpty(): it test whether the stack is empty.
• isFull(): it test whether the stack is empty.
• Size(): it returns the number of items on the stack.
STACK IMPLEMENTATION

• class Stack {

static final int MAX = 1000;

int top;

int a[] = new int[MAX];

boolean isEmpty()

return (top < 0);

• Stack()

top = -1;

}
INSERTION

boolean push(int x)
{
if (top >= (MAX - 1)) {
System.out.println("Stack Overflow");
return false;
}
else {
a[++top] = x;
System.out.println(x + " pushed into stack");
return true;
}
}
DELETION

int pop()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top--];
return x;
}
}
PEEK VALUE: RETURN

int peek()
{
if (top < 0) {
System.out.println("Stack Underflow");
return 0;
}
else {
int x = a[top];
return x;
}
}
}
DRIVER CODE

Main {
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack")
}
}
APPLICATIONS OF STACK:

• Infix to Postfix /Prefix conversion


• Redo-undo features at many places like editors, photoshop.
• Forward and backward feature in web browsers
• Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram
problem.
• Other applications can be Backtracking, Knight tour problem, rat in a maze, N queen
• In Graph Algorithms like Topological Sorting and Strongly Connected Components
TWO WAYS OF IMPLEMENTATION

• Arrays: Static
• Pointers: Dynamic
TYPES OF QUEUE

• Simple Queue
• Circular Queue
• Priority Queue
• Double ended Queue
OPERATIONS

• Queue(): creates new queue that is empty


• Enqueue(item): it adds new item to the rear
• Dequeue(): it removes an item from front
• isEmpty(): it test whether the queue is empty.
• Size(): it returns the number of items in the queue
MEMORY REPRESENTATION OF A QUEUE USING
ARRAY
• Queue is represented in a linear array
• Rear and front are the pointers
APPLICATIONS OF QUEUE

• Printer server routine


• Railway reservation system
• Scheduling algorithms

You might also like