Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Online C compiler to run C program online
- #include <stdio.h>
- #include <stdlib.h>
- struct Node {
- int data;
- struct Node *next;
- } *start = NULL;
- int maxsize = 5;
- int length = 0;
- void Queue(int d) {
- struct Node *temp;
- temp = (struct Node *)malloc(sizeof(struct Node));
- temp->data = d;
- temp->next = NULL;
- if (start == NULL) {
- start = temp;
- printf("Queue Created successfully");
- length = 1;
- }
- }
- int size() {
- return length;
- }
- int isEmpty() {
- return size() == 0;
- }
- void displayQueue(){
- struct Node *ptr;
- ptr = start;
- while (ptr != NULL) {
- printf("%d ->", ptr->data);
- ptr = ptr->next;
- }
- }
- void enqueue(int d) {
- struct Node *temp;
- temp = (struct Node *)malloc(sizeof(struct Node));
- temp->data = d;
- temp->next = start;
- start = temp;
- length += 1;
- }
- void dequeue() {
- struct Node *temp, *ptr;
- if (isEmpty()) {
- printf("Queue underflow");
- }
- else {
- ptr = start;
- while (ptr->next->next != NULL) {
- ptr = ptr->next;
- }
- temp = ptr->next;
- ptr->next = NULL;
- printf("Element deleted %d", temp->data);
- free(temp);
- length -= 1;
- }
- }
- int main(){
- int choice, data=0;
- while (choice!=6) {
- printf("\n1. Create a Queue");
- printf("\n2. Display Queue Elements");
- printf("\n3. Enqueue an element");
- printf("\n4. Dequeue an element");
- printf("\n5. Get Queue Size");
- printf("\n6. Exit");
- printf("\n *** Enter a choice : ");
- scanf("%d", &choice);
- switch(choice) {
- case 1:
- printf("Enter a start element : ");
- scanf("%d", &data);
- Queue(data);
- break;
- case 2:
- displayQueue();
- break;
- case 3:
- if (start == NULL) {
- printf("Create a Queue First");
- }
- else if (size() >= maxsize){
- printf("Queue overflow");
- }
- else{
- printf("Insert element : ");
- scanf("%d", &data);
- enqueue(data);
- }
- break;
- case 4:
- dequeue();
- break;
- case 5:
- printf("Queue Size is : %d",size());
- break;
- case 6: break;
- default: break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement