C Program For Que Using Linked List

/*Q using Linked List*/

#include<stdio.h>
#include<conio.h>
struct node
{
struct node *front;
int n;
struct node *rear;
};
struct node *curr,*temp;
struct node* create(struct node*);
void display(struct node*);
void insert(struct node*);
struct node* Delete(struct node*);
void isempty(struct node*);
void main(void)
{
struct node *s;
int ch;
clrscr();
s=NULL;
do
{
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert\n");
printf("4.Delete\n");
printf("5.Isempty\n");
printf("6.Exit\n");
printf("Enter the choice");
scanf("%d",&ch);
 switch(ch)
 {
 case 1:s=create(s);
  break;
 case 2:display(s);
  break;
 case 3:insert(s);
  break;
 case 4:s=Delete(s);
  break;
 case 5:isempty(s);
  break;
 }

}while(ch!=6);
}

struct node * create(struct node *x)
{
++++ if (x==NULL)
 {
 x=(struct node *)malloc(sizeof(struct node));
 printf("Enter the number");
 scanf("%d",&x->n);
 x->front=NULL;
 x->rear=NULL;
 return x;
 }
 else
 {
 printf("The Q already Created\n");
 return x;
 }
}

void display(struct node *x)
{
curr=x;
 while(curr!=NULL)
 {
 printf("%d->",curr->n);
 curr=curr->rear;
 }
}

void insert(struct node *x)
{
curr=x;
 while(curr->rear!=NULL)
 {
 curr=curr->rear;
 }

temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the number");
scanf("%d",&temp->n);
temp->rear=NULL;
curr->rear=temp;
temp->front=curr;
}

struct node* Delete(struct node *x)
{
curr=x;
 if (curr==NULL)
 {
 printf("No node to be delted\n");
 return x;
 }
 else if (curr->rear==NULL)
 {
 printf("u r deleting final element\n");
 free(curr);
 x=NULL;
 return x;
 }
 else
 {
 x=x->rear;
 x->front=NULL;
 free(curr);
 return x;
 }
}


void isempty(struct node *x)
{
 if (x==NULL)
 {
 printf("Q is empty\n");
 }
 else
 {
 printf("Q is not empty\n");
 }
}
Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment