Sunday, 16 August 2020

Simple Stack program using Array

#include<stdio.h>

#include<stdlib.h>

#define MAX_SIZE 5

int main() {

    int item, choice, i;

    int arr_stack[MAX_SIZE];

    int top = 0;

    int exit = 1;

    printf("\n Simple Stack Example - Array");

    do {

        printf("\n\n Stack Main Menu");


        printf("\n 1.Push \n 2.Pop \n 3.Display \n 4.Exit");

        printf("\n Enter Your Choice : ");

        scanf("%d", &choice);

        switch (choice) {

            case 1:

                if (top == MAX_SIZE)

                    printf("\n ***Stack is Full!");

                else {

                    printf("\n Enter The Value to be pushed : ");

                    scanf("%d", &item);

                    printf("\n ***Position : %d , Pushed Value  : %d ", top, item);

                    arr_stack[top++] = item;

                }

                break;

            case 2:

                if (top == 0)

                    printf("\n ***Stack is Empty!");

                else {

                    --top;

                    printf("\n ***Position : %d , Popped Value  : %d ", top, arr_stack[top]);

                }

                break;

            case 3:

                printf("\n ***Stack Size : %d ", top);

                for (i = (top - 1); i >= 0; i--)

                    printf("\n ***Position : %d , Value  : %d ", i, arr_stack[i]);

                break;

            default:

                exit = 0;

                break;

        }

    } while (exit);

    return 0;

}

Output ::

💬

Friday, 14 August 2020

Stack Program


Simple stack Program Using Array



Write a Program to find Smallest and Largest Elements in the Binary Search Tree

Write a Program to check whether a tree is a Binary Search Tree

 

This C Program Checks whether a Tree is a Binary Search Tree.


#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
}
;
static struct node *prev = NULL;
//Function to check whether the tree is BST or not
int is_bst(struct node* root) {
if (root) {
//moves towards the leftmost child of the tree and checks for the BST
if (!is_bst(root->left)) 
return 0;
if (prev != NULL && root->data <= prev->data)
            return 0;
prev = root;
return is_bst(root->right);
//moves the corresponding right child of the tree and checks for the BST
}
return 1;
}
struct node* newNode(int data) {
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
int main() {
struct node *root = newNode(40);
root->left        = newNode(20);
root->right       = newNode(60);
root->left->left  = newNode(10);
root->left->right = newNode(30);
root->right->right = newNode(80);
root->right->right->right = newNode(90);
if (is_bst(root))
        printf("TREE 1 Is BST \n"); else
        printf("TREE 1 Not a BST");
return 0;
}

Output :
TREE 1 Is BST

Data Structure in C



C Introduction

What is C?

C is one of the most widely used programming language of all times.
It is a general purpose, block-structured, procedural, case-sensitive and high level programming language.

Who is C Developer? 

C is developed by Dennis Richie and his partner Ken Thompson at the Bell Telephone Laboratories in span of 1969 to 1973.

Why name is C?

It was named C because it evolved from earlier languages Basic Combined Programming Language(BCPL) and B.

Why to learn C?

  • You should learn first basic programming skills to learn object oriented concept. Which will can be got through C very easily.
  • You have to use basic elements of C always no matter in which platform you are.
  • Graphical programming like 3D appplications, Mobile games and device drivers are exclusively made in C.

My First C Program

  1. #include<stdio.h>
  2. int main()
  3. {
  4. printf("Hello World");
  5. return 0;
  6. }

Output

  1.  Hello World


.

Algorithm in C