Queue Using Single Pointer Linked ListCode CrazeMar 25, 20231 min readUpdated: Mar 28, 2023#include <stdio.h>#include<stdlib.h>#include<math.h>#define size 5int top=-1;void enque(void);void deque(void);void peek(void);void isEmpty(void);void isFull(void);void display(void);struct queue{ int data; struct node *next;};struct queue *head=0;void main() { int c,e; do{ switch(c) { case 1: enque(); break; case 2: deque(); break; case 3: peek(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1.Enque\t2.Deque\t3.Peek\t4.isEmpty\t5.isFull\t6.Display\t7.Exit"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); }void enque(){ struct queue *newnode,*temp; if(top<size-1) { newnode=(struct queue *)malloc(sizeof(struct queue)); printf("\nEnter data : "); scanf("%d",&newnode->data); newnode->next=0; top++; if(head==0) { head=temp=newnode; } else { temp=head; while(temp->next!=0) { temp=temp->next; } temp->next=newnode; } } else { printf("\nQueue is full"); }}void display(){ struct queue *temp; temp=head; while(temp!=0) { printf("--%d",temp->data); temp=temp->next; }}void deque(){ struct queue *temp,*prevnode; printf("\n\n----Deletion From End----"); temp=head; top--; head=head->next; free(temp); printf("\nTop : %d",top);}void peek(){ struct queue *temp; temp=head; while(temp->next!=0) { temp=temp->next; } printf("\nTop : %d",top); printf("\nPeek : %d",temp->data);}void isEmpty(){ if(top==-1) { printf("\nQueue is empty"); } else { printf("\nQueue is not empty"); }}void isFull(){ if(top==size-1) { printf("\nQueue is full"); } else { printf("\nQueue is not full"); }}
#include <stdio.h>#include<stdlib.h>#include<math.h>#define size 5int top=-1;void enque(void);void deque(void);void peek(void);void isEmpty(void);void isFull(void);void display(void);struct queue{ int data; struct node *next;};struct queue *head=0;void main() { int c,e; do{ switch(c) { case 1: enque(); break; case 2: deque(); break; case 3: peek(); break; case 4: isEmpty(); break; case 5: isFull(); break; case 6: display(); break; case 7: printf("\n----Exit from program successfully----"); printf("\n--------------Thank you--------------"); exit(0); } printf("\n1.Enque\t2.Deque\t3.Peek\t4.isEmpty\t5.isFull\t6.Display\t7.Exit"); printf("\nEnter your choice : "); scanf("%d",&c); }while(c); }void enque(){ struct queue *newnode,*temp; if(top<size-1) { newnode=(struct queue *)malloc(sizeof(struct queue)); printf("\nEnter data : "); scanf("%d",&newnode->data); newnode->next=0; top++; if(head==0) { head=temp=newnode; } else { temp=head; while(temp->next!=0) { temp=temp->next; } temp->next=newnode; } } else { printf("\nQueue is full"); }}void display(){ struct queue *temp; temp=head; while(temp!=0) { printf("--%d",temp->data); temp=temp->next; }}void deque(){ struct queue *temp,*prevnode; printf("\n\n----Deletion From End----"); temp=head; top--; head=head->next; free(temp); printf("\nTop : %d",top);}void peek(){ struct queue *temp; temp=head; while(temp->next!=0) { temp=temp->next; } printf("\nTop : %d",top); printf("\nPeek : %d",temp->data);}void isEmpty(){ if(top==-1) { printf("\nQueue is empty"); } else { printf("\nQueue is not empty"); }}void isFull(){ if(top==size-1) { printf("\nQueue is full"); } else { printf("\nQueue is not full"); }}
Stack Using QueueA stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue...
Comentários