Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void enqueue(int);
- void dequeue(void);
- void display_queue(void);
- void queue_size(void);
- void rear_and_front(void);
- struct node{
- int data;
- struct node *next;
- }*start=NULL;
- struct node *front, *rear;
- int main(){
- int choice, ele;
- while(1){
- printf("Enter 1 to Enqueue\n");
- printf("Enter 2 to Dequeue\n");
- printf("Enter 3 to display Queue\n");
- printf("Enter 4 to show Queue size\n");
- printf("Enter 5 to display Front and Rear elements\n");
- scanf("%d",&choice);
- switch(choice){
- case 1: printf("Enter the element\n");
- scanf("%d",&ele);
- enqueue(ele);
- break;
- case 2: dequeue();
- break;
- case 3: display_queue();
- break;
- case 4: queue_size();
- break;
- case 5: rear_and_front();
- break;
- default: printf("Invalid choice\n");
- }
- }
- return 0;
- }
- void enqueue(int ele){
- struct node *temp, *p;
- temp = (struct node*)malloc(sizeof(struct node));
- if(start == NULL){
- temp -> data = ele;
- temp -> next = NULL;
- start = temp;
- rear = front = temp;
- printf("Added %d to Queue!!\n",start->data);
- }
- else{
- p = start;
- while(p->next != NULL){
- p = p->next;
- }
- temp = (struct node*)malloc(sizeof(struct node));
- temp -> data = ele;
- temp -> next = NULL;
- p -> next = temp;
- rear = temp;
- printf("Added %d to Queue!!\n",temp->data);
- }
- }
- void dequeue(){
- struct node *front;
- if (start == NULL){
- printf("No element to remove\n");
- }
- else{
- start = start -> next;
- front = start;
- printf("Item successfully removed!!\n");
- }
- }
- void display_queue(){
- struct node *p;
- p = start;
- if (p == NULL){
- printf("List is empty\n");
- }
- else{
- while(p != NULL){
- printf("%d\t",p->data);
- p = p->next;
- }
- printf("\n");
- }
- }
- void queue_size(){
- int count = 1;
- if (start == NULL){
- printf("Queue is empty!!\n");
- }
- else{
- while(start -> next != NULL){
- count++;
- start = start->next;
- }
- printf("Size of queue : %d\n", count);
- }
- }
- void rear_and_front(){
- if(start == NULL){
- printf("Queue is empty!\n");
- }
- else{
- printf("FRONT -> %d\t",front -> data);
- printf("REAR -> %d\n",rear -> data );
- }
- }
Add Comment
Please, Sign In to add comment