लिंक्ड लिस्ट का उपयोग करके क्यू ओवरफ्लो और क्यू अंडरफ्लो से बचा जा सकता है।
C प्रोग्रामिंग भाषा में लिंक्ड लिस्ट की सहायता से क्यू के तहत किए जाने वाले ऑपरेशन निम्नलिखित हैं:
- इन्सर्ट
- डिलीट
इन्सर्शन
सिंटैक्स निम्नलिखित है:
&item : Newnode = (node*) malloc (sizeof (node));
newnode->data = item;
newnode->link = NULL;
if ((front == NULL) || (rear == NULL)) {
front = newnode;
rear = newnode;
} else {
Rear->link = newnode;
rear = newnode;
}
डिलीशन
सिंटैक्स निम्नलिखित है:
if ((front == NULL))
printf("डिलीशन संभव नहीं, क्यू खाली है");
else {
temp = front;
front = front->link;
free(temp);
}
डिस्प्ले
सिंटैक्स निम्नलिखित है:
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 विकल्प दर्ज करें: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("डेटा दर्ज करें: ");
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("फ्रंट एलिमेंट: %d", e);
else
printf("\n क्यू में कोई फ्रंट एलिमेंट नहीं");
break;
default:
printf("गलत विकल्प, फिर से कोशिश करें ");
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("क्यू खाली है");
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 त्रुटि");
return;
} else if (front1->ptr != NULL) {
front1 = front1->ptr;
printf("\n डिक्यूड मान: %d", front->info);
free(front);
front = front1;
} else {
printf("\n डिक्यूड मान: %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 विकल्प दर्ज करें: 1 डेटा दर्ज करें: 14 विकल्प दर्ज करें: 1 डेटा दर्ज करें: 85 विकल्प दर्ज करें: 1 डेटा दर्ज करें: 38 विकल्प दर्ज करें: 5 फ्रंट एलिमेंट: 14 विकल्प दर्ज करें: 3 14 85 38 विकल्प दर्ज करें: 2 डिक्यूड मान: 14 विकल्प दर्ज करें: 3 विकल्प दर्ज करें: 4