কম্পিউটার টিউটোরিয়াল

C ভাষায় লিংকড লিস্ট ব্যবহার করে কিউ (Queue) ব্যাখ্যা


লিংকড লিস্ট ব্যবহার করে কিউ ওভারফ্লো এবং কিউ অন্ডারফ্লো এড়ানো যায়।

C প্রোগ্রামিং ভাষায় লিংকড লিস্টের সাহায্যে কিউ-এর অধীনে gerçekleştirিত অপারেশনগুলো হলো:

  • ইনসার্ট (Insert)
  • ডিলিট (Delete)

ইনসার্শন (Insertion)

সিনট্যাক্স নিম্নরূপ:

সিনট্যাক্স

&item : Newnode = (node*) mallac (sizeof (node)); newnode ->data = item; newnode ->link = NULL; if ((front = = NULL) || (rear = = NULL)){    front= newnode;    rear = newnode; }else{    Rear->link = newnode;    rear = newnode; }

ডিলিশন (Deletion)

সিনট্যাক্স নিম্নরূপ:

সিনট্যাক্স

if ((front= = NULL)) printf("Deletion is not possible, Queue is empty"); else{    temp = front;    front = front ->link;    free (temp); }

ডিসপ্লে (Display)

সিনট্যাক্স নিম্নরূপ:

সিনট্যাক্স

while (front! = NULL){    printf("%d", front ->data);    front = front->link; }

প্রোগ্রাম

লিংকড লিস্ট ব্যবহার করে কিউ-এর C প্রোগ্রাম:

#include <stdio.h> #include <stdlib.h> struct node{    int info;    struct node *ptr; }*front,*rear,*temp,*front1; int frontelement(); void enq(int data); void deq(); void display(); void create(); int count = 0; void main(){    int no, ch, e;    printf("\n 1 - Enqueue");    printf("\n 2 - Dequeue");    printf("\n 3 - Display");    printf("\n 4 - Exit");    printf("\n 5-front");    create();    while (1){       printf("\n Enter choice : ");       scanf("%d", &ch);       switch (ch){          case 1:             printf("Enter data : ");          scanf("%d", &no);          enq(no);          break;          case 2:             deq();          break;          case 3:             display();          break;          case 4:             exit(0);          break;          case 5:             e = frontelement();          if (e != 0)             printf("Front element : %d", e);          else             printf("\n No front element in Queue");          break;          default:          printf("Wrong choice, Try again ");          break;       }    } } void enq(int data){    if (rear == NULL){       rear = (struct node *)malloc(1*sizeof(struct node));       rear->ptr = NULL;       rear->info = data;       front = rear;    }else{       temp=(struct node *)malloc(1*sizeof(struct node));       rear->ptr = temp;       temp->info = data;       temp->ptr = NULL;       rear = temp;    }    count++; } void display(){    front1 = front;    if ((front1 == NULL) && (rear == NULL)){       printf("Queue is empty");       return;    }    while (front1 != rear){       printf("%d ", front1->info);       front1 = front1->ptr;    }    if (front1 == rear)       printf("%d", front1->info);    }    void deq(){       front1 = front;       if (front1 == NULL){          printf("\n Error");          return;       }       else       if (front1->ptr != NULL){          front1 = front1->ptr;          printf("\n Dequeued value : %d", front->info);          free(front);          front = front1;       }else{          printf("\n Dequeued value : %d", front->info);          free(front);          front = NULL;       rear = NULL;    }    count--; } int frontelement(){    if ((front != NULL) && (rear != NULL))       return(front->info);    else       return 0; }

আউটপুট

উপরোক্ত প্রোগ্রামটি এক্সিকিউট করলে নিচের ফলাফলটি দেখায়:

1 - Enque 2 - Deque 3 – Display 4 - Exit 5 - Front element Enter choice: 1 Enter data: 14 Enter choice: 1 Enter data: 85 Enter choice: 1 Enter data: 38 Enter choice: 5 Front element: 14 Enter choice: 3 14 85 38 Enter choice: 2 Dequed value: 14 Enter choice: 3 Enter choice: 4

  1. C ভাষায় স্যুইচ স্টেটমেন্ট ব্যাখ্যা

  2. স্ট্রাকচার কনসেপ্ট ব্যবহার করে সি ভাষায় বিট ফিল্ড ব্যাখ্যা

  3. C-তে লিংকড লিস্ট ব্যবহার করে প্রায়োরিটি কিউ (Priority Queue using Linked List)

  4. ডাবলি লিংকড লিস্ট দিয়ে প্রায়োরিটি কিউ বাস্তবায়ন C++-তে