Queue using Linked List in C

/*code for implementing queue operations using linked list*/
#include<stdio.h>
#include<stdlib.h>
struct node

{
int data;

struct node* link;

};

struct node *front=NULL;

struct node *rear=NULL;



void insert(int);

void display();

void del();



void insert(int d)

{

struct node *newnode=(struct node *)malloc(sizeof(struct node));

newnode->data=d;

newnode->link=NULL;



if(front==NULL)

{

front=rear=newnode;

}

else

{

rear->link=newnode;

rear=newnode;

}

}

void display()

{

if(front==NULL){printf("\nQueue is empty.\n");}

else

{

struct node *ptr;

ptr=front;

while(ptr)

{

printf("%d ",ptr->data);

ptr=ptr->link;

}

}

printf("\n");

}

void del()

{

struct node* temp;

temp=front;

if(front==NULL){printf("\nQueue is Empty.\n");}

else

{

struct node* temp;

temp=front;

front=front->link;

//rear->link=front;

free(temp);

}

}



int main()

{

insert(10);

insert(20);

insert(100);

insert(40);

insert(300);

display();

del();

display();

return 0;

}

/*

10 20 100 40 300

20 100 40 300

*/

No comments:

Post a Comment