DSA Notes: Stack Algorithm For PUSH Operation
DSA Notes: Stack Algorithm For PUSH Operation
• Primitive data structures: These are the basic data structures and are
directly operated upon by the machine instructions, which is in a
primitive level.
– Example: integers, floating point numbers, characters, string
constants, pointers
• Non-primitive data structures: It is a more sophisticated data structure
emphasizing on structuring of a group of homogeneous (same type) or
heterogeneous (different type) data items.
– Example: Array, list, files, linked list, trees and graphs
Stack
Algorithm for PUSH operation
• Step 1 − Checks if the stack is full.
• Step 2 − If the stack is full, produces an error and exit.
• Step 3 − If the stack is not full, increments top to point next empty
space.
• Step 4 − Adds data element to the stack location, where top is
pointing.
• Step 5 − Returns success.
Algorithm for POP operation
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at
which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
void push(int value){
• if(top == SIZE-1)
• cout<<"Stack is Full!!! Insertion is not possible!!!";
• else{
• stack[++top] = value;
• cout<<“Insertion success!!!";
• }
• }
void pop(){
• if(top == -1)
• cout<<“Stack is Empty!!! Deletion is not possible!!!";
• else{
• cout<<“Deleted :“<< stack[top--];
• }
• }
void display()
• {
• if(top == -1)
• cout<<“Stack is Empty!!!";
• else
• {
• cout<<“Stack elements are:\n";
• for(int i=top; i>=0; i--)
• cout<<stack[i];
• }
• }
Queue
Algorithm for ENQUEUE operation
• Check if the queue is full or not.
• If the queue is full, then print overflow error and exit the
program.
• If the queue is not full, then increment the rear or tail and add
the element.
Algorithm for DEQUEUE operation
• Check if the queue is empty or not.
• If the queue is empty, then print underflow error and exit the
program.
• If the queue is not empty, then print the element at the front or
head and increment the front or head.
void enQueue(int value)
• {
• if(rear == SIZE-1)
• cout<<“Queue is Full!!!
• Insertion is not possible!!!";
• else
• {
• if(front==-1)
• front=0;
• queue[++rear] = value;
• cout<<“Insertion success!!!";
• }
• }
void deQueue()
• {
• if(front == -1||front>rear)
• cout<<“Queue is Empty!!!
• Deletion is not possible!!!";
• else
• {
• cout<<“Deleted:“<<queue[front++];
• }
• cout<<“Deletion Success!!!”;
• }
void display()
• {
• if(rear == -1)
• cout<<"Queue is Empty!!!";
• else
• {
• cout<<“Queue elements are:“<<“\n”;
• for(int i=front; i<=rear; i++)
• cout<<queue[i];
• }
• }
Pointer And Array
#include<iostream.h>
void main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i=0; i<5; i++)
{
cout<<*p; // or cout<<*(p+i);
p++;
}
}
New Delete Operator
#include <iostream.h
void main ()
{
int n;
cout<<“Enter the number of elements:”;
cin>>n;
int *q = new int[n];
if (!p)
cout << "allocation of memory failed\n";
else
{
for (int i = 0; i < n; i++)
cin>>(q+i);
cout << "Value store in block of memory: ";
for (int i = 0; i < n; i++)
cout << *(q+i) << " ";
}
delete[] q; // freed the allocated memory
}
Linked Lists
Advantages:
• No wastage of memory
• Prior knowledge on depth of a tree is not needed.
• Insertion or deletion can be done without moving other nodes.
Disadvantages:
• Direct access to a node is not possible.
• Needs additional space in each node for representing left and right sub
trees.
Binary Tree
Structure of Node in Binary Tree ADT
struct node{
int data;
struct node *left;
struct node *right; };
Traversal
InOrder Traversal
void inorder (struct node *temp)
{ if(temp!=NULL)
{ inorder(temp->left);
cout<<temp->data;
inorder(temp->right);
}}
PreOrder Traversal
void preorder (struct node *temp)
{ if(temp!=NULL)
{ cout<<temp->data;
preorder(temp->left);
preorder(temp->right);
}}
PostOrder Traversal
void postorder (struct node *temp)
{ if(temp!=NULL)
{ postorder(temp->left);
postorder(temp->right);
cout<<temp->data;
}}
Binary Search Tree
Binary Search Tree (BST) is a binary tree which has the following properties:
• The left sub tree of a node contains only nodes with keys less than the
root node's key.
• The right sub tree of a node contains only nodes with keys greater than
the root node's key.
• Both the left and right sub trees must also be binary search trees.
• There must be no duplicate nodes.
BST Insertion
void insert(struct node *root, struct node *temp)
{
if (temp->data < root->data)
{
if(root -> lchild == NULL)
root->lchild = temp;
else
insert(root->lchild, temp);
}
else if(temp->data > root->data)
{
if(root->rchild == NULL)
root->rchild = temp;
else
insert(root->rchild, temp);
}
else
printf("\nDuplicate entry..\n"); }
BST Searching
void search(struct node *root, int key)
{ struct node *par;
par = root, temp = root;
if(key == root->data)
printf("%d is present as the root node",key);
else
{
while((key!=temp->data)&&(temp!=NULL))
{
par = temp;
if(key < temp->data)
{ temp = temp->lchild;
if(key == temp->data)
printf("%d is present as left child of %d",key,par->data);
}
else if(key > temp->data)
{ temp = temp->rchild;
if(key == temp->data)
printf("%d is present as right child of %d",key,par->data);
}
}
if(temp==NULL) printf("%d is not present",key);
}
}
BST Deletion
void delete(struct node *root,int key) {
struct node *par=NULL,*succ=NULL;
par = root, temp = root;
while((key!=temp->data)&&(temp!=NULL)) {
par=temp;
if(key < temp->data) temp=temp->lchild;
else if(key > temp->data) temp=temp->rchild; }