Skip to content

Instantly share code, notes, and snippets.

@priyadarshitathagat
Created September 23, 2016 20:04
Show Gist options
  • Save priyadarshitathagat/afd94ff1a61350b127c9cca363e89b2c to your computer and use it in GitHub Desktop.
Save priyadarshitathagat/afd94ff1a61350b127c9cca363e89b2c to your computer and use it in GitHub Desktop.
Program to implement QUEUE (insert element at rear and remove elements form front) concept using linked list
#include <stdio.h>
#include<stdlib.h>
struct node
{
int val;
struct node *link;
};
struct node* getnode( )
{
return ( struct node *)malloc( sizeof( struct node ));
};
struct node* inr( struct node *); struct node* def( struct node *);
void display( struct node *);
int main()
{
struct node *F;
int ch;
F = NULL;
while( 1 )
{
printf("1. To insert at Rear\n");
printf("2. To delete from Front\n");
printf("3. Display \n");
printf("4. Exit\n");
scanf("%d", &ch );
if( ch == 1 )
F = inr (F);
else if( ch == 2 )
F = def (F);
else if( ch == 3 )
display(F);
else
break;
}
}
struct node* inr( struct node *FIRST)
{
struct node *T;
if( FIRST == NULL )
{
FIRST = getnode();
T = FIRST;
}
else
{
T = FIRST;
while( T->link != NULL ) T = T->link;
T->link = getnode( );
T = T->link;
}
printf("Enter the element to push\n");
scanf("%d", &T->val);
T->link = NULL;
return FIRST;
}
struct node* def( struct node *FIRST)
{ struct node* T; T=FIRST;
if(FIRST==NULL)
{
printf("NO Elements\n"); return(FIRST);
}
printf("The deleted value is: %d\n",T->val);
T=T->link;
FIRST=T;
return FIRST;
}
void display( struct node *T)
{ if(T==NULL)
{
printf("Empty \n"); return;
}
while( T!= NULL )
{
printf("%d ",T->val);
T = T->link;
}
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment