Skip to content

Instantly share code, notes, and snippets.

@B45i
Last active March 31, 2016 16:49
Show Gist options
  • Save B45i/8e2d301b37e615095065 to your computer and use it in GitHub Desktop.
Save B45i/8e2d301b37e615095065 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct node{
int i;
struct node *link;
}*top=NULL,*front=NULL,*rear=NULL,*ptr,*n,*tptr;
void stack();
void queue();
void menu();
int main(){
menu();
return 0;
}
void menu(){
int c;
printf("\n MAIN MENU\n");
printf("PRESS 1 FOR STACK\nPRESS 2 FOR QUEUE\nPRESS 3 TO EXIT\nYOUR CHOICE: ");
scanf("%d",&c);
switch(c){
case 1:
stack();
break;
case 2:
queue();
break;
case 3:
return ;
break;
default:
printf("INVALID INPUT\n");
menu();
}
}
void stack(){
int c;
printf("\n STACK MENU\n");
printf("PRESS 1 FOR PUSH\nPRESS 2 FOR POP\nPRESS 3 TO DISPLAY\nPRESS 4 TO EXIT TO MIAN MENU\nYOUR CHOICE: ");
scanf("%d",&c);
switch(c){
case 1:
if(top==NULL){
top= (struct node*)malloc(sizeof(struct node));
printf("\nEnter Item : ");
scanf("%d",&top->i);
top->link = NULL;
}
else{
n= (struct node*)malloc(sizeof(struct node));
printf("\nEnter Item : ");
scanf("%d",&n->i);
n->link = top;
top = n;
}
stack();
break;
case 2:
ptr = top;
if(top == NULL){
printf("\nERROR! STACK IS EMPTY \n");
}
else{
ptr = top;
if(top == NULL){
printf("\nERROR LINK IS EMPTY !\n");
}
else{
top = top->link;
free(ptr);
}
printf("POP OPERATION SUCCESS\n");
}
stack();
break;
case 3:
ptr = top;
if(top==NULL){
printf("\nERROR! STACK IS EMPTY \n");;
}
else{
while(ptr!=NULL){
printf("%d ",ptr->i);
ptr = ptr->link;
}
}
stack();
break;
case 4:
menu();
break;
}
}
void queue(){
int c,itm;
printf("\n QUEUE MENU\n");
printf("PRESS 1 FOR ENQUE\nPRESS 2 FOR DEQUEUE\nPRESS 3 TO DISPLAY\nPRESS 4 TO EXIT TO MIAN MENU\nYOUR CHOICE: ");
scanf("%d",&c);
switch(c){
case 1:
n= (struct node*)malloc(sizeof(struct node));
printf("\nEnter Item : ");
scanf("%d",&n->i);
n->link==NULL;
if(front==NULL){
front=rear=n;
}
else{
rear->link=n;
rear=n;
rear->link=NULL;
}
queue();
break;
case 2:
ptr = front;
if(front == NULL){
printf("\nERROR! QUEUE IS EMPTY \n");
}
else{
if(front == rear){
free(front);
front = rear = NULL;
}
else{
front = front->link;
free(ptr);
}
printf("DEQUEUE OPERATION SUCCESS\n");
}
queue();
break;
case 3:
ptr = front;
if(front==NULL){
printf("\nERROR! QUEUE IS EMPTY \n");
}
else{
while(ptr!=NULL){
printf("%d ",ptr->i);
ptr = ptr->link;
}
}
queue();
break;
case 4:
menu();
break ;
default:
printf("INVALID INPUT\n");
queue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment