Site icon

9.Data Structure

Data structure

Data structure

What is Data Structure?

ডেটা স্ট্রাকচার( Data Structure) হল একটি স্টোরেজ যা ডেটা সংরক্ষণ এবং সংগঠিত করতে ব্যবহৃত হয়। এটি একটি কম্পিউটারে ডেটা সাজানোর একটি উপায় যাতে এটি অ্যাক্সেস করা যায় এবং দক্ষতার সাথে আপডেট করা যায়।

Type of Data structure

মূলত, ডেটা স্ট্রাকচার দুটি বিভাগে বিভক্ত: 1) Linear data structure  2) Non-linear data structure

 Linear data structure: উপাদানগুলিকে একের পর এক ক্রমানুসারে সাজানো হয়। যেহেতু উপাদানগুলি নির্দিষ্ট ক্রমে সাজানো হয়েছে, সেগুলি বাস্তবায়ন করা সহজ।

জনপ্রিয়  Data structure গুলি হলঃ-

  1. Array Data struvture
  2. Stack Data structure
  3. Array Data Structure
  4. Queue Data Structure
  5. Link List Data structure

Non-linear data structure:

Non-linear data structure বলতে এমন ডেটা স্ট্রাকচারগুলিকে(Data structure) বোঝায় যারা লিনিয়ার বা সরলরেখীয় নয়। এই ধরনের ডেটা স্ট্রাকচারে উপাদানগুলি সরলভাবে সাজানো থাকে না।

উদাহরণ:

সাধারণত, নন-লিনিয়ার ডেটা স্ট্রাকচারগুলি (Non-linear data structure লিনিয়ার ডেটা স্ট্রাকচার চেয়ে জটিল হলেও কিছু ক্ষেত্রে সেগুলি বেশি দক্ষতার সাথে কাজ করে।

What is Algorithm ?

একটি অ্যালগরিদম একটি নির্দিষ্ট সমস্যা সমাধানের জন্য সুনির্দিষ্ট নির্দেশাবলীর একটি সেট। এটি ইনপুট(গুলি) এর একটি সেট নেয় এবং পছন্দসই আউটপুট তৈরি করে। উদাহরণ স্বরূপ,

দুটি সংখ্যা গুন করার জন্য একটি অ্যালগরিদম:

  1. দুটি ইনপুট নিতে হবে [Take two number for inputs] যেমন – a,b
  2. দুটি নাম্বারের গুন করতে হবে [Multiply two number]- a*b
  3. কোন চলরাশির মাধ্যমে ফলাফল দেখাও[Show result in other variable]।

How to write algorithm?

Example: write an algorithm to check whether a number is positive or negative.

//algorithm
                  Step 1: Start      
                  Step 2: Input any Number
                  Step 3: Read n
                  Step 4: if(n==0)
                                    print "Number is 0"
                             else 
                                 if(n>0)
                                    print "Number is positive"
                                else
                                   print "Number is negative" 
                  step 5: stop
                                    

Data Structure: Stack

STACK operation with algorithm

স্ট্যাক(Stack) হল একটি লিনিয়ার ডাটা স্ট্রাকচার যা LIFO (Last In First Out) প্রিন্সিপল অনুসরণ করে। স্ট্যাকে প্রধানত নিম্নলিখিত অপারেশনগুলি করা হয়:

PUSH – স্ট্যাকের শীর্ষে একটি উপাদান যোগ করে।

POP – স্ট্যাক থেকে শীর্ষস্থ উপাদানটি সরিয়ে নেয়।

PEEK – স্ট্যাকের শীর্ষস্থ উপাদানটি রিটার্ন করে তবে সরিয়ে নেয় না।

স্ট্যাক(Stack) সম্পর্কে কিছু গুরুত্বপূর্ণ বিষয়:

সংক্ষেপে, স্ট্যাক LIFO অর্ডার অনুসরণ করে, শুধুমাত্র এক দিকে অপারেশনের সুবিধা দেয় এবং ফাংশন, পার্সার, ব্যাকট্র্যাকিং অ্যালগরিদম ইত্যাদি ইমপ্লিমেন্টের জন্য ব্যবহৃত হয়। এর সহজ গঠনটি পুশ(PUSH)/পপ(POP) অপারেশনকে খুব দ্রুত করে তুলে। এই দুই অপারেশন দুই ভাবে ইমপ্লিমেন্ট করা সম্ভব 1) Array 2) Link List

স্ট্যাক(STACK) এর পুশ(PUSH) এবং পপ(POP) অপারেশনটির algorithm:

 PUSH/INSERT()

                 1.if top=MAX then

                             print "Stack is full";

   		             exit;

                2. otherwise

                            top:=top+1;

                             stack(top)=item;

                3.  end of if

                4. exit

   POP/DELETE()

               1.  if top=0 then

   			     print "Stack is empty";

   			     exit;

   	       2. otherwise

   			     item=stack(top);

                             top:=top-1;

   	       3.  end of if

   	       4. exit


 isFull()

                 1. if top=MAX then

   			status=true;

   		 2. otherwise

   		       status=false;

   		 3.  end of if

   		 4. exit


 isEmpty()

                 1.   if top=0 then

                                  status=true;

   		 2.   otherwise

   		                status=false;

   		 3.    end of if

   		 4.    exit

1. Array : স্ট্যাক(STACK) এর পুশ(PUSH) এবং পপ(POP) অপারেশন এর Source code টি উল্লেখ করা হল –
                                               //Stack Implementation Using Array[Static Memory allocation]
#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

int main()

{

    top=-1;

    printf("\n Enter the size of STACK[MAX=100]:");

    scanf("%d",&n);

    printf("\n\t STACK OPERATIONS USING ARRAY");

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

    printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");

    do

    {

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

        scanf("%d",&choice);

        switch(choice)

        {

            case 1:

            {

                push();

                break;

            }

            case 2:

            {

                pop();

                break;

            }

            case 3:

            {

                display();

                break;

            }

            case 4:

            {

                printf("\n\t EXIT POINT ");

                break;

            }

            default:

            {

                printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");

            }

                

        }

    }

    while(choice!=4);

    return 0;

}

void push()

{

    if(top>=n-1)

    {

        printf("\n\tSTACK is over flow");

        

    }

    else

    {

        printf(" Enter a value to be pushed:");

        scanf("%d",&x);

        top++;

        stack[top]=x;

    }

}

void pop()

{

    if(top<=-1)

    {

        printf("\n\t Stack is under flow");

    }

    else

    {

        printf("\n\t The popped elements is %d",stack[top]);

        top--;

    }

}

void display()

{

    if(top>=0)

    {

        printf("\n The elements in STACK \n");

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

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

        printf("\n Press Next Choice");

    }

    else

    {

        printf("\n The STACK is empty");

    }

 }
Link list : স্ট্যাক(STACK) এর পুশ(PUSH) এবং পপ(POP) অপারেশন এর Source code টি উল্লেখ করা হল –
                                                          //Stack Implementation Using Link List 

#include <stdio.h>

#include <stdlib.h>

struct Node

{

    int data;

    struct Node *next;

} *top = NULL;


void push(int);

void pop();

void display();


void main()

{

    int ch, data;

    printf("\n:: Stack using Linked List ::\n");

    while (1)
    {
        printf("\n****** MENU ******\n");

        printf("1. Insert\n2. Pop\n3. Display\n4. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &ch);

        switch (ch)

        {

        case 1:

            printf("Enter the value to be insert: ");

            scanf("%d", &data);

            push(data);

            break;

        case 2:

            pop();

            break;

        case 3:

            display();

            break;

        case 4:

            exit(0);

        default:

            printf("\nWrong selection!!! Please try again!!!\n");

        }

    }

}

void push(int value)

{

    struct Node *newNode;

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

    newNode->data = value;

    if (top == NULL)

        newNode->next = NULL;

    else

        newNode->next = top;

    top = newNode;

    printf("\nInsertion is Success!!!\n");

}

void pop()

{

    if (top == NULL)

        printf("\nStack is Empty!!!\n");

    else

    {

        struct Node *temp = top;

        printf("\nDeleted element: %d", temp->data);

        top = temp->next;

        free(temp);

    }

}

void display()

{

    if (top == NULL)

        printf("\nStack is Empty!!!\n");

    else

    {
        struct Node *temp = top;

        while (temp->next != NULL)

        {

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

            temp = temp->next;
        }

        printf("%d--->NULL", temp->data);

    }

}

Data Structure : Queue

Queue Operation with algorithm

ডাটা স্ট্রাকচারে কিউয়ের(Queue) মূল অপারেশনগুলি নিম্নরূপ:

1. ENQUEUE কিউয়ের(queue) শেষে একটি উপাদান যোগ করে। এটি রিয়ার পয়েন্টার (REAR pointer) ব্যবহার করে করা হয়।

2. DEQUEUEকিউয়ের(queue) সামনে থেকে একটি উপাদান সরায়। যে উপাদানটি সরানো হয় তাকে ফ্রন্ট পয়েন্টার(FRONT pointer) দ্বারা পয়েন্ট করা থাকে।

3. isEmpty – কিউ(queue) খালি(empty) কিনা তা পরীক্ষা করে। যদি কিউয়ের সাইজ 0 হয় তাহলে ট্রু(TRUE) রিটার্ন করে।

4. is Full – কিউ পূর্ণ কিনা তা পরীক্ষা করে। যদি কিউয়ের সাইজ ম্যাক্স সাইজের সমান হয় তাহলে ট্রু(TRUE) রিটার্ন করে।

কিউয়ের(queue) কিছু গুরুত্বপূর্ণ বৈশিষ্ট্য:

সংক্ষেপে, কিউ হল একটি লিনিয়ার ডাটা স্ট্রাকচার যেটি এক দিক থেকে যোগ করার এবং অন্য দিক থেকে সিকুয়েন্সিয়াল FIFO ম্যানারে সরানোর সুবিধা দেয়। এই দুই অপারেশন দুই ভাবে ইমপ্লিমেন্ট করা সম্ভব 1) Array 2) Link List

Queue এর enqueue এবং dequeue অপারেশনটির algorithm:

                                                                   // Algorithm:

 enqueue/insert element to the queue

                    procedure enqueue(data)      

                                  if  queue is full

                                      return overflow

                               endif

                                      rear ← rear + 1

                                   queue[rear] ← data

                                   return true

                   end procedure


 dequeue/delete element from the queue

                             procedure dequeue

                                              if queue is empty

                                                  return underflow

                                           end if

                                                   data = queue[front]

                                                   front ← front + 1

                                                   return true

                              end procedure
Array: enqueue এবং dequeue অপারেশন এর Source code টি উল্লেখ করা হল –
                                                        //Queue Implementation Using Array

#include <stdio.h>

#include <stdlib.h>

#define MAX 50

void enqueue();

void dequeue();

void display();

int queue_array[MAX];

int rear = -1;

int front = -1;

int main()

{

    int choice;

    while (1)

    {

        printf("\n1.Enqueue/Insert element to queue \n");

        printf("\n2.Dequeue/Delete element from queue \n");

        printf("\n3.Display all elements of queue \n");

        printf("\n4.Quit \n");

        printf("\nEnter your choice : ");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            enqueue();

            break;

        case 2:

            dequeue();

            break;

        case 3:

            display();

            break;

        case 4:

            exit(1);

        default:

            printf("Wrong choice \n");

        }

    }

}

void enqueue()

{

    int data;

    if (rear == MAX - 1)

        printf("Queue Overflow \n");

    else

    {

        if (front == -1)

            front = 0;

        printf("Inset the element in queue : ");

        scanf("%d", &data);

        rear = rear + 1;

        queue_array[rear] = data;

    }

}

void dequeue()

{

    if (front == -1 || front > rear)

    {

        printf("Queue Underflow \n");

        return;

    }

    else

    {

        printf("Element dequeue from queue is : %d\n", queue_array[front]);

        front = front + 1;

    }

}

void display()

{

    int i;

    if (front == -1)

        printf("Queue is empty \n");

    else

    {

        printf("Queue is : \n");

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

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

        printf("\n");

    }

}
Linked list: enqueue এবং dequeue অপারেশন এর Source code টি উল্লেখ করা হল –
                                                                     //Queue implementation in C using Link List

#include<stdio.h>

#include<stdlib.h>

struct Queue         //structure is declared

{ 

    struct Queue *next; //self referensing structure

    int item;            //initailizing item in the structure

};

    struct Queue *front;  //a pointer front is declared in structure

    struct Queue *rear;   //a pointer rear is declared in structure

    void addq(int);      //addq function is declared

    int delq();          //delq function id declared

void main()

{

    int ch, item, val; //variables are declared

    while(1)

    {

        printf("\nEnter 1 to add element in the Queue");

        printf("\nEnter 2 to delete element from the Queue");

        printf("\nEnter 3 to exit from program");

        printf("\nEnter your choice::");

        scanf("%d",&ch);

        switch(ch)

        {

            case 1: printf("\nEnter the item in the list::");

                scanf("%d", &item);

                addq(item);

                break;

            case 2: val=delq() ;

                printf("\nThe deleted element is %d=",val);

                break ;

            case 3: exit(1);

            default: printf("\nWrong Input") ;

         }

     }

        

}

void addq(int item)

{

 struct Queue *temp; //a pointer temp is declared within the structure

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

 if(temp==NULL)

    {

      printf("\nQueue Overflowed");

      return;

    }

 temp->item=item;//the value is entered in the List

 temp->next=NULL;

 if(rear==NULL)

  {

    front=temp;

    rear=temp;

  }

 else

  {

   rear->next=temp;

   rear=rear->next;     //rear is incremented

  }

}

int delq()

{

 struct Queue *temp;

 int item=0;

 if(front==NULL)       //checking unerflowed condition

  {

   printf("\nQueue Underflowed");

   return 0;

  }

  item=front->item;

  temp=front;

   if(front==NULL)   //checking whether front is null or not, if true

    { 

     front=NULL;    //front is assign with NULL

     rear=NULL;

    } 

  else

  {

    front=front->next;

    free(temp);

  }

  return item;

}

Data Structure: link list

Single link list Operation with algorithm

লিঙ্কড লিস্ট(linked list) একটি লিনিয়ার ডাটা স্ট্রাকচার(lenear data structure) যেখানে প্রতিটি উপাদানকে নোড(Node) নামে আলাদা অবজেক্ট হিসেবে ধরা হয়। লিঙ্কড লিস্টগুলি নোডগুলি দিয়ে গঠিত যেখানে প্রতিটি নোডে ডাটার ফিল্ড(Data field) এবং লিস্টে পরবর্তী নোডের লিঙ্ক (Node pointer) থাকে।

লিঙ্কড লিস্টের কিছু গুরুত্বপূর্ণ বৈশিষ্ট্য নিম্নরূপ:

অ্যারের তুলনায় লিঙ্কড লিস্টের সুবিধাগুলো:

অসুবিধাগুলো:

মোটামুটি, অজানা সাইজের কালেকশন সংরক্ষণ এবং দ্রুত ইনসার্ট ও ডিলিটের প্রয়োজনে লিঙ্কড লিস্ট(linked list) একটি গুরুত্বপূর্ণ লিনিয়ার ডাটা স্ট্রাকচার(linear data structure)।

Linked list অপারেশন গুলির algorithm:

                                                                       //Algorithm:

InsertNodeAtBegining()

    If (newNode == NULL) then
        print ('Unable to allocate memory')
    End if
    Else then
        read item
        newNode.item ← item
        newNode.next ← head
        head ← newNode
    End else

InsertNodeAtEnding()

     If (newNode == NULL) then

              print ('Unable to allocate memory')

     End if

     Else then

         read item

         newNode.item ← item

         newNode.next ← NULL

             temp ← End

         While (temp.next != NULL) do

              temp ← temp.next

          End while

               temp.next ← newNode

           End else


InsertNodeAtMiddle()

      If (newNode == NULL) then

            write ('Unable to allocate memory.')

      End if

      Else then

         read item

         newNode.item ← item

         temp ← head

          For i ← 2 to n-1

             temp ← temp.next

               If (temp == NULL) then

                   break

               End if

           End for

           If (temp != NULL) then

                newNode.next ← temp.next

                temp.next ← newNode

           End if

           End else


deleteFirstNode()

           If (head==NULL)

               print “List Empty”

           end if

           else

              temp = head

              head = head.next

              delete temp

            End


deleteLastNode() 

        If (head == NULL) then

             print ('List is already empty')

        End if

        Else then

            temp ← head

            previousNode ← head

            While (temp.next != NULL) do

                 previousNode ← temp

                 temp ← temp.next

            End while

                If (temp == head) then

                   head ← NULL

                End if

                Else then

                    previousNode.next ← NULL

                End else

                     free (temp)

               End


deleteMiddleNode()

         If (head == NULL) then

               print('List empty')

         End if

         Else then

               temp ← head

               previousNode ← head

               For i←2 to n do

                    previousNode ← temp

                    temp ← temp.next

                      If (temp == NULL) then

                                break

                      End if

                End for

                    If (temp != NULL and temp == head) then

                              head ← head.next

                    End if

                    Else

                           previousNode.next ← temp.next

                           temp.next ← NULL

                           free (temp)

         	     End else

                  End
লিঙ্কড লিস্ট (Linked list) অপারেশন এর source code টি উল্লেখ করা হল –
                                                          // Single Link List using C program



#include <stdio.h>

#include <stdlib.h>

/* Structure of a node */

struct node

{

    int item;          

    struct node *next; 

} * head;

void createList(int n);

void displayList();

void insertNodeAtBeginning(int item);

void insertNodeAtEnd(int item);

void insertNodeAtMiddle(int item, int position);

void deleteFirstNode();

void deleteLastNode();

void deleteMiddleNode(int position);

int main()

{

    int n, item, position;

    int choice;

    for (;;)

    {

        printf("\n===========::Single Link List Menu::=================\n\n ");

        printf("\n1. Press 1 for Creating Lists\n");

        printf("\n2. Press 2 for display Lists\n");

        printf("\n3. Press 3 for insert item into fisrt Position of Lists\n");

        printf("\n4. Press 4 for insert item into last Position of Lists\n");

        printf("\n5. Press 5 for insert item into specified middle Position of Lists\n");

        printf("\n6. Press 6 for delete item from fisrt Position of Lists\n");

        printf("\n7. Press 7 for delete item from  last Position of Lists\n");

        printf("\n8. Press 8 for delete item from specified middle Position of Lists\n");

        printf("\n===========::Single Link List Menu::=================\n ");

        printf("\n\tEnter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            printf("Enter the total number of nodes: ");

            scanf("%d", &n);

            createList(n);

            break;

        case 2:

            printf("\nitem in the list \n");

            displayList();

            break;

        case 3:

            printf("\nEnter item to insert at beginning of the list: ");

            scanf("%d", &item);

            insertNodeAtBeginning(item);

            break;

        case 4:

            printf("\nEnter item to insert at end of the list: ");

            scanf("%d", &item);

            insertNodeAtEnd(item);

            break;

        case 5:

            printf("nEnter item to insert at middle of the list: ");

            scanf("%d", &item);

            printf("Enter the position to insert new node: ");

            scanf("%d", &position);

            insertNodeAtMiddle(item, position);

            break;

        case 6:

            deleteFirstNode();

            break;

        case 7:

            deleteLastNode();

            break;

        case 8:

            printf("\nEnter the node position you want to delete: ");

            scanf("%d", &position);

            deleteMiddleNode(position);

            break;

        case 9:

            exit(1);

            break;

        default:

            printf("Wrong selction");

        }

    }

    return 0;

}

void createList(int n)

{

    struct node *newNode, *temp;

    int item, i;

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

    if (head == NULL)

    {

        printf("Unable to allocate memory.");

        exit(0);

    }

    printf("Enter the item of node 1: ");

    scanf("%d", &item);

    head->item = item; 

    head->next = NULL; 

   

    temp = head;

    for (i = 2; i <= n; i++)

    {

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

        if (newNode == NULL)

        {

            printf("Unable to allocate memory.");

            break;

        }

        printf("Enter the item of node %d: ", i);

        scanf("%d", &item);

        newNode->item = item; 

        newNode->next = NULL; 

        temp->next = newNode; 

        temp = temp->next;   

    }

}

void displayList()

{

    struct node *temp;

    if (head == NULL)

    {

        printf("List is empty.");

    }

    else

    {

        temp = head;

        while (temp != NULL)

        {

            printf("item = %d\n", temp->item);

            temp = temp->next;

        }

    }

}

void insertNodeAtBeginning(int item)

{

    struct node *newNode;

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

    if (newNode == NULL)

    {

        printf("Unable to allocate memory.");

    }

    else

    {

        newNode->item = item;

        newNode->next = head;

        head = newNode;

        printf("item INSERTED SUCCESSFULLY\n");

    }

}

void insertNodeAtEnd(int item)

{

    struct node *newNode, *temp;

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

    if (newNode == NULL)

    {

        printf("Unable to allocate memory.");

    }

    else

    {

        newNode->item = item;

        newNode->next = NULL;

        temp = head;

        while (temp != NULL && temp->next != NULL)

            temp = temp->next;

        temp->next = newNode;

        printf("item INSERTED SUCCESSFULLY\n");

    }

}

void insertNodeAtMiddle(int item, int position)

{

    int i;

    struct node *newNode, *temp;

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

    if (newNode == NULL)

    {

        printf("Unable to allocate memory.");

    }

    else

    {

        newNode->item = item;

        newNode->next = NULL;

        temp = head;

        for (i = 2; i <= position - 1; i++)

        {

            temp = temp->next;

            if (temp == NULL)

                break;

        }

        if (temp != NULL)

        {

            newNode->next = temp->next;

            temp->next = newNode;

            printf("item INSERTED SUCCESSFULLY\n");

        }

        else

        {

            printf("UNABLE TO INSERT item AT THE GIVEN POSITION\n");

        }

    }

}

void deleteFirstNode()

{

    struct node *toDelete;

    if (head == NULL)

    {

        printf("List is already empty.");

    }

    else

    {

        toDelete = head;

        head = head->next;

        printf("\nData deleted = %d\n", toDelete->item);

        free(toDelete);

        printf("Successfully Delete First Node\n");

    }

}

void deleteLastNode()

{

    struct node *toDelete, *secondLastNode;

    if (head == NULL)

    {

        printf("List is already empty.");

    }

    else

    {

        toDelete = head;

        secondLastNode = head;

        while (toDelete->next != NULL)

        {

            secondLastNode = toDelete;

            toDelete = toDelete->next;

        }

        if (toDelete == head)

        {

            head = NULL;

        }

        else

        {

           secondLastNode->next = NULL;

        }

        free(toDelete);

        printf("Successfully Deleted Last Node\n");

    }

}

void deleteMiddleNode(int position)

{

    int i;

    struct node *toDelete, *prevNode;

    if (head == NULL)

    {

        printf("List is already empty.");

    }

    else

    {

        toDelete = head;

        prevNode = head;

        for (i = 2; i <= position; i++)

        {

            prevNode = toDelete;

            toDelete = toDelete->next;

            if (toDelete == NULL)

                break;

        }

        if (toDelete != NULL)

        {

            if (toDelete == head)

                head = head->next;

            prevNode->next = toDelete->next;

            toDelete->next = NULL;

            free(toDelete);

            printf("Successfully\n");

        }

        else

        {

            printf("Invalid position unable to delete.");

        }

    }

}
Exit mobile version