Skip to content

Instantly share code, notes, and snippets.

@defrindr
Last active March 14, 2021 13:11
Show Gist options
  • Save defrindr/f0e4fc60324103eaaa0786c54b8f09b2 to your computer and use it in GitHub Desktop.
Save defrindr/f0e4fc60324103eaaa0786c54b8f09b2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#define MAX 100
typedef struct {
int Item[MAX];
int Front;
int Rear;
int Max;
int Is_Max;
int Count;
}
Queue;
int Empty(Queue * Q) {
if (Q -> Rear == -1) {
return 1;
}
return 0;
}
void Push(Queue * Q, int value) {
if (Q -> Count == Q -> Max) printf("Maximum");
else {
if (Empty(Q) == 1) {
Q -> Front = 0;
Q -> Rear = 0;
Q -> Item[Q -> Rear] = value;
Q -> Count = 1;
} else {
// if(Q -> Rear == Q -> Max){
// Q -> Front = 0;
// }else{
Q -> Rear = (Q -> Rear + 1) % Q -> Max;
// }
Q -> Item[Q -> Rear] = value;
Q -> Count++;
}
}
}
void PushFront(Queue * Q, int value) {
if (Q -> Count == Q -> Max) printf("Maximum");
else {
if (Empty(Q) == 1) {
Q -> Front = 0;
Q -> Rear = 0;
Q -> Item[Q -> Rear] = value;
Q -> Count = 1;
} else {
Q -> Item[Q -> Front] = value;
Q -> Front--;
Q -> Count++;
}
}
}
int Delete(Queue * Q) {
int deleted_item;
deleted_item = Q -> Item[Q -> Front];
Q -> Item[Q -> Front] = -10000;
if (Q -> Front == Q -> Rear)
printf("Empty");
else {
Q -> Front = (Q -> Front + 1) % Q -> Max;
Q -> Count--;
}
return deleted_item;
}
void Print(Queue * Q) {
int i, val;
for (i = 0; i < Q -> Max; i++) {
val = Q -> Item[i];
if (val > -10000 && val < 10000) {
printf("%d ", val);
} else printf(" ");
}
printf("\n");
}
void Initialize(Queue * Q, int value) {
Q -> Count = 0;
Q -> Rear = -1;
Q -> Front = -1;
Q -> Max = value;
}
int main() {
Queue q;
int del;
Initialize( & q, 5);
Push( & q, 5);
Push( & q, 6);
Push( & q, 7);
Push( & q, 8);
Print( & q);
Delete( & q);
Print( & q);
return 0;
}
#include <stdio.h>
#define MAX 100
typedef struct {
int Item[MAX];
int Front;
int Rear;
int Max;
int Is_Max;
int Count;
}
Queue;
int Empty(Queue * Q) {
if (Q -> Rear == -1) {
return 1;
}
return 0;
}
void Push(Queue * Q, int value) {
if (Q -> Count == Q -> Max) printf("Maximum");
else {
if (Empty(Q) == 1) {
Q -> Front = 0;
Q -> Rear = 0;
Q -> Item[Q -> Rear] = value;
Q -> Count = 1;
} else {
// if(Q -> Rear == Q -> Max){
// Q -> Front = 0;
// }else{
Q -> Rear = (Q -> Rear + 1) % Q -> Max;
// }
Q -> Item[Q -> Rear] = value;
Q -> Count++;
}
}
}
void PushFront(Queue * Q, int value) {
if (Q -> Count == Q -> Max) printf("Maximum");
else {
if (Empty(Q) == 1) {
Q -> Front = 0;
Q -> Rear = 0;
Q -> Item[Q -> Rear] = value;
Q -> Count = 1;
} else {
Q -> Item[Q -> Front] = value;
Q -> Front--;
Q -> Count++;
}
}
}
int Delete(Queue * Q) {
int deleted_item;
deleted_item = Q -> Item[Q -> Front];
Q -> Item[Q -> Front] = -10000;
if (Q -> Front == Q -> Rear)
printf("Empty");
else {
Q -> Front = (Q -> Front + 1) % Q -> Max;
Q -> Count--;
}
return deleted_item;
}
void Print(Queue * Q) {
int i, val;
for (i = 0; i < Q -> Max; i++) {
val = Q -> Item[i];
if (val > -10000 && val < 10000) {
printf("%d ", val);
} else printf(" ");
}
printf("\n");
}
void Initialize(Queue * Q, int value) {
Q -> Count = 0;
Q -> Rear = -1;
Q -> Front = -1;
Q -> Max = value;
}
int main() {
Queue q;
int del;
Initialize( & q, 5);
Push( & q, 5);
Push( & q, 6);
Print( & q);
Push( & q, 7);
Print( & q);
del = Delete( & q);
Print( & q);
Push( & q, 8);
Print( & q);
Push( & q, 9);
Print( & q);
del = Delete( & q);
Print( & q);
Push( & q, 10);
Print( & q);
Push( & q, 11);
Print( & q);
return 0;
}
@defrindr
Copy link
Author

defrindr commented Mar 8, 2021

output 👍
5 6
5 6 7
6 7
6 7 8
6 7 8 9
7 8 9
10 7 8 9
10 11 7 8 9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment