Last active
October 1, 2016 13:17
-
-
Save priyadarshitathagat/c5a4a5f46b37a0b5166c669485a2f995 to your computer and use it in GitHub Desktop.
Program to implement DEQUEUE (to insert elements from rear/front and also delete elements from rear/front) concept using linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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* inf( struct node *); struct node* def( struct node *); | |
struct node* der( struct node *); struct node* inr( struct node *); | |
void display( struct node *); | |
int main() | |
{ | |
struct node *F; | |
int ch; | |
F = NULL; | |
while( 1 ) | |
{ | |
printf("1. To insert at front\n"); | |
printf("2. To delete from front\n"); | |
printf("3. To insert at rear\n"); | |
printf("4. To delete from rear\n"); | |
printf("5. Display \n"); | |
printf("6. Exit\n"); | |
scanf("%d", &ch ); | |
if( ch == 1 ) | |
F = inf (F); | |
else if( ch == 2 ) | |
F = def (F); | |
else if( ch == 3 ) | |
F = inr (F); | |
else if( ch == 4 ) | |
F = der (F); | |
else if( ch == 5 ) | |
display(F); | |
else | |
break; | |
} | |
} | |
struct node* inf( struct node *FIRST) | |
{ | |
struct node *T; struct node *P; | |
if( FIRST == NULL ) | |
{ | |
FIRST = getnode(); | |
T = FIRST; | |
T->link=NULL; | |
} | |
else | |
{ | |
P=getnode(); | |
P->link=FIRST; | |
FIRST=P; | |
T=FIRST; | |
} | |
printf("Enter the element to push\n"); | |
scanf("%d", &T->val); | |
return FIRST; | |
} | |
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; | |
} | |
struct node* der( struct node *FIRST) | |
{ struct node* T; T=FIRST; | |
if(FIRST==NULL) | |
{ | |
printf("NO Elements\n"); return(FIRST); | |
} | |
struct node* P=T; | |
while(T->link!=NULL) | |
{ P=T; | |
T=T->link; | |
} | |
printf("The deleted value is: %d\n",T->val); | |
if(P==T) | |
FIRST=NULL; | |
else | |
P->link=NULL; | |
return FIRST; | |
} | |
void display( struct node *T) | |
{ if(T==NULL) | |
{ | |
printf("Empty \n"); return; | |
} | |
printf("The Elements present are: \n"); | |
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