Blog Details

img
Machine Learning

Data Structure Material

Administration / 18 Apr, 2024

 

Array

 

Stack implementations in Data Structure:

 

#include<stdio.h>

#define MAX 5

int stack[MAX];

int top=-1;

push(int element)

{

          if(top==MAX-1)

          {

                   printf("\n @@@@@@@@ Stack is Full or Overflow @@@@@@@@ \n");

                   return 0;

          }

          top=top+1;

          stack[top]=element;        

}

pop()

{

          if(top==-1)

          {

                   printf("\n  @@@@@@@@ Stack is Empty or Underflow @@@@@@@@ \n");

                   return 0;

          }

          printf("Delete an Element from the Stack :%d",stack[top]);

          top=top-1;

          return 0;

}

display()

{

          printf("\n Display the Stack  :");

          if(top==-1)

          {

                   printf("\n @@@@@@@@ Stack is Empty or Underflow @@@@@@@@ \n");

                   return 0;

          }

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

          {

                   printf(" %d",stack[i]);

          }

}

exit()

{

          return 0;

}

 main()

{

          int choice,element;

          while(1)

          {

                   printf("\n****** Performing a Stack Operation****** \n");

                   printf("\n***************************************** \n");

                   printf("\n Performing a Stack Operation 1: PUSH  \n");

                   printf("\n Performing a Stack Operation 2: POP  \n");

                   printf("\n Performing a Stack Operation 3: DISPLAY \n");

                   printf("\n Performing a Stack Operation 4: EXIT \n");

                   printf("\n***************************************** \n\n");

         

                   printf("Enter Your Choice from [1 to 4 ] :");

                   scanf("%d",&choice);

                  

                   switch(choice)

                   {

                             case 1:

                                      printf("\n Enter the Element to Insert Into Stack : ");

                                      scanf("%d",&element);

                                      push(element);

                                      break;

                             case 2:

                                      pop();

                                      break;

                             case 3:

                                      display();

                                      break;

                             case 4:

                                      exit();

                                      break;

                             default :

                                                printf("You have Enter a Wrong Choice please Select from only {1 to 4} :");

                                                break;         

                   }

          }

         

}

 

Queue implementation in Data Structure:

 

#include<stdio.h>

#define MAX 5

#include<stdlib.h>

int queue[MAX];

int front=0;

int rear=0;

insert(int item)

{

          if(rear==MAX)

          {

                   printf("\n \n!!!!--------Queue is Overflow --------!!!! \n");

                   return 0;

          }

          queue[rear]=item;

          rear=rear+1;

         

}

Delete()

{

          if(front==rear)

          {

                   printf("\n \n!!!!--------Queue is Underflow --------!!!! \n");

                   return 0;

          }

          printf("\n \n Deleted item from the Queue :%d \n ",queue[front]);

          for(int i=0;i<rear;i++)

          {

                   queue[i]=queue[i+1];

          }

          rear=rear-1;

}

display()

{

          if(front==rear)

          {

                   printf("\n \n!!!!--------Queue is Underflow --------!!!! \n");

                   return 0;

          }

          printf("\n @ @ @ @ @ @ <==== Display the Element from Queue @ @ @ @ @ @ ====> \n");

          for(int i=front;i<rear;i++)

          {

                   printf(" %d",queue[i]);

          }

         

}

 

 

main()

{

          int choice,item;

          while(1)

          {

                   printf("\n \n!!!!--------Performing a Queue Operations------!!!! \n");

                   printf("----------------------------------------------------------\n");

                   printf("Insertion operation in a Queue : 1.insert()\n");

                   printf("Deletion  operation in a Queue : 2.delete() \n");

                   printf("Display   operation in a Queue : 3.Display() \n");

                   printf("Exit      operation in a Queue : 4.Exit() \n");

                   printf("----------------------------------------------------------\n \n ");

                  

                   printf("\n Enter a choice from [1 ,2 ,3, 4] :");

                   scanf("%d",&choice);

                  

                   switch(choice)

                   {

                             case 1:

                                      printf("Insert a item into the Queue : ");

                                      scanf("%d",&item);

                                      insert(item);

                                      break;

                             case 2:

                                      Delete();

                                      break;

                             case 3:

                                      display();

                                      break;

                             case 4:

                                      exit(0);

                                      break;

                             default :

                                      printf("/n You have Enter a Wrong Input please Select input from [1,2,3,4] /n");

                                      break;

                                     

                            

                   }

          }

}

 

 

 

 

 

 

Linked-List implementation in Data Structure:

 

 

#include<stdio.h>

#include<stdlib.h>

struct node

{

          int data;

          struct node *link;

};

struct node *start=NULL;

ssl_enter_at_end(int item)

{

          struct node *temp,*ptr;

          temp=(struct node*)malloc(sizeof(struct node));

          if(temp==NULL)

          {

                   printf("Insufficient Memory !!!");

                   exit(0);

          }

          temp->data=item;

          temp->link=NULL;

          if(start == NULL)

          {

                   start = temp;

          }

          else

          {

                   ptr=start;

                   while(ptr->link!=NULL)

                   {

                             ptr=ptr->link;

                   }

          }

          ptr->link=temp; 

}

ssl_enter_at_begin(int item)

{

          struct node *temp;

          temp=(struct node*)malloc(sizeof(struct node));

          if(temp==NULL)

          {

                   printf("Insufficient Memory !!!");

                   exit(0);

          }

          temp->data=item;

          temp->link=start;

          start=temp;

         

          return 1;

}

ssl_enter_at_any(int item,int pos)

{

          struct node *temp,*ptr;

          temp=(struct node*)malloc(sizeof(struct node));

          if(temp==NULL)

          {

                   printf("Insufficient Memory !!!");

                   exit(0);

          }

          temp->data=item;

          ptr=start;

          for(int i=1;i<=pos-2;i++)

          {

                    ptr=ptr->link;

          }

          temp->link=ptr->link ;

          ptr->link=temp;

}

sll_deletion_at_begin()

{

          struct node *ptr;

          if(start==NULL)

          {

                   printf("No node is for Deletion :");

                   return 0;

          }

          ptr=start;

          start=start->link;

}

sll_deletion_at_end()

{

          struct node *ptr,*prevptr;

          if(start==NULL)

          {

                   printf("No node is for Deletion :");

                   return 0;

          }

          ptr=start;

          //for deletion of the single node

          if(ptr->link == NULL)

          {

                   start=NULL;

          }

          //for deletion of the many node

          else

          {

                   while(ptr->link!=NULL)

                   {

                             prevptr=ptr;

                             ptr=ptr->link;

                   }

                   prevptr->link=NULL;

          }

}

sll_deletion_at_any(int pos)

{

          struct node *ptr,*prevptr;

          if(start==NULL)

          {

                   printf("No node is for Deletion :");

                   return 0;

          }

          ptr=start;

          for(int i=1;i<=pos-1;i++)

          {

                   prevptr=ptr;

                   ptr=ptr->link;

          }

          prevptr->link=ptr->link;

}

display()

{

          struct node *ptr;

          if(start==NULL)

          {

                   printf("Linked List not a Created ");

                   return 0;

          }

          ptr=start;

         

          while(ptr!=NULL)

          {

             printf("%d ",ptr->data);

             ptr=ptr->link;     

          }

          printf("NULL");

}

 

main()

{

          int ch,item,pos;

          while(1)

          {

                   printf("\n \n---- Linked List----- \n");

                   printf("\n 1.Enter the linked list at End \n");

                   printf("\n 2.Enter the linked list at Begin \n");

                   printf("\n 3.Enter the linked list at any \n");

                   printf("\n 4.Deletion a node from begin \n");

                   printf("\n 5.Deletion a node from end \n");

                   printf("\n 6.Deletion a node from any \n");

                   printf("\n 7.Display the Linked list \n");

                   printf("\n 8. Exit Linked list application \n");

                  

                   printf("\n \n Enter the choice into list = ");

                   scanf("%d",&ch);

                  

                   switch(ch)

                   {

                             case 1:

                                      printf("Enter the item into the List :");

                                      scanf("%d",&item);

                                      ssl_enter_at_end(item);

                                      break;

                             case 2:

                                      printf("Enter the item into the List :");

                                      scanf("%d",&item);

                                      ssl_enter_at_begin(item);

                                      break;

                             case 3:

                                      printf("Enter the item into the List :");

                                      scanf("%d",&item);

                                      printf("Enter the possition of List :");

                                      scanf("%d",&pos);

                                      ssl_enter_at_any(item,pos);

                                      break;

                             case 4:

                                      sll_deletion_at_begin();

                                      break;

                             case 5:

                                      sll_deletion_at_end();

                                      break;

                             case 6:

                                      printf("Enter the possition of List :");

                                      scanf("%d",&pos);

                                      sll_deletion_at_any(pos);

                                      break;

                             case 7:

                                      display();

                                      break;

                             case 8:

                                      exit(0);

                                      break;

                             default :

                                      printf("You have enter a wrong choice :");

                            

                   }

          }

}

 

TREE :-BINARY TREE

 

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node *left;

    struct Node *right;

};

 

struct Node* createNode(int data) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = data;

    newNode->left = NULL;

    newNode->right = NULL;

    return newNode;

}

 

struct Node* insert(struct Node* root, int data) {

    if (root == NULL) {

        root = createNode(data);

    } else if (data <= root->data) {

        root->left = insert(root->left, data);

    } else {

        root->right = insert(root->right, data);

    }

    return root;

}

 

void inorderTraversal(struct Node* root) {

    if (root != NULL) {

        inorderTraversal(root->left);

        printf("%d ", root->data);

        inorderTraversal(root->right);

    }

}

 

int main() {

    struct Node* root = NULL;

 

    root = insert(root, 10);

    root = insert(root, 5);

    root = insert(root, 15);

    root = insert(root, 3);

    root = insert(root, 7);

 

    printf("In-order traversal: ");

    inorderTraversal(root);

    printf("\n");

 

    return 0;

}

 

0 comments