We help IT Professionals succeed at work.
Get Started
Troubleshooting Question

A code in C++ or C programming language  for linked list implementation using queue

lianne143
lianne143 asked
on
49 Views
Last Modified: 2020-10-06
Hi,
This is a code for implementation of linked list using queue.I have coded this, however i am getting an error could you debug it for me.
Thanks in advance


#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node* next;
};
struct node* front;
struct node* rear;
void enqueue(int value){
    struct node* new;
    new=(struct node *)malloc(sizeof(struct node));
    if(rear==NULL){
        rear=new;
        rear->data=value;
        rear->next=NULL;
        front=rear;
    }
    else{
        rear->next=new;
        new->data=value;
        new->next=NULL;
        rear=new;
    }
}
void dequeue(){
    struct node* temp;
    temp=front;
    if(rear==NULL&&front==NULL){
        printf("Queue is empty, Deletion is not possible !!\n");
    }
    else if(front->next==NULL){
        printf("The deleted element is: %d\n",front->data);
        free(front);
        front=NULL;
        rear=NULL;
    }
    else{
        temp=temp->next;
        printf("The deleted element is: %d\n",front->data);
        free(front);
        front=temp;
    }
}
void display(){
    struct node* temp;
    temp=front;
    if(front==NULL&&rear==NULL){
        printf("Queue is empty\n");
    }
    else{
        printf("Elements in the queue are: ");
        while(temp!=NULL){
            printf("->%d ",temp->data);
            temp=temp->next;
        }
        printf("\n");
    }
}
int main(){
    printf("\tName: K.Sanjay\n");
    printf("\tReg.no: URK19CS1212\n");
    printf("\n");
    system("color 70");
    int v,choice;
    do{
        printf("Enter the choice:\n1.Enqueue\n2.Dequeue\n3.Dispaly\n4.Exit\n");
        scanf("%d",&choice);
        switch(choice){
            case 1:
            printf("Enter the value: ");
            scanf("%d",&v);
            enqueue(v);
            break;
            case 2:
            dequeue();
            break;
            case 3:
            display();
            break;
            case 4:
            printf("Exit Program\n");
            break;
            default:
            printf("Invalid choice");
            break;
        }
    }
    while(choice!=4);
    return 0;
}
 

Comment
Watch Question
Software Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE