/*
* Copy right by Shiv Yaragatti
* you are free to use, modify and redistribute code on own risk
* Interactive C Program to implement Queue using linked list
*/
/* header needs have all required header files like stdio.h etc */
#include "header.h"
struct queue {
int data;
struct queue *next;
};
typedef struct queue *Q;
struct queue {
int data;
struct queue *next;
};
typedef struct queue *Q;
/* Queue head */
struct qhead_struct {
struct queue *head;
struct queue *tail;
};
typedef struct qhead_struct *H;
struct qhead_struct {
struct queue *head;
struct queue *tail;
};
typedef struct qhead_struct *H;
/* Insert elements into queue */
void insert(H *hq)
{
int e;
Q temp;
temp= malloc(sizeof(struct queue));
if(!temp) {
printf("Can't allocate memory\n");
return;
}
printf("Enter element\n");
scanf("%d", &e);
temp->data =e;
temp->next = NULL;
void insert(H *hq)
{
int e;
Q temp;
temp= malloc(sizeof(struct queue));
if(!temp) {
printf("Can't allocate memory\n");
return;
}
printf("Enter element\n");
scanf("%d", &e);
temp->data =e;
temp->next = NULL;
/* add element queue at tail */
if((*hq)->head == NULL) {
(*hq)->head =(*hq)->tail =temp;
} else {
(*hq)->tail->next = temp;
(*hq)->tail = temp;
}
}
if((*hq)->head == NULL) {
(*hq)->head =(*hq)->tail =temp;
} else {
(*hq)->tail->next = temp;
(*hq)->tail = temp;
}
}
/* delete elements from queue */
void del(H *hq){
Q temp;
temp= (*hq)->head;
if((*hq)->;head == NULL){
printf("Q is empty ..\n");
return;
}
printf("Deleted element is %d\n", temp->data);
Q temp;
temp= (*hq)->head;
if((*hq)->;head == NULL){
printf("Q is empty ..\n");
return;
}
printf("Deleted element is %d\n", temp->data);
/* delete from head */
(*hq)->head = (*hq)->head->next;
free(temp);
}
(*hq)->head = (*hq)->head->next;
free(temp);
}
/* Display elements in the queue */
void display(H hq){
Q temp;
if(hq->head == NULL){
printf("Empty Queue\n");
return;
}
temp=hq->head;
printf("Elements in Queue are \n");
while(temp){
printf("%d\n", temp->data);
temp=temp->next;
}
}
Q temp;
if(hq->head == NULL){
printf("Empty Queue\n");
return;
}
temp=hq->head;
printf("Elements in Queue are \n");
while(temp){
printf("%d\n", temp->data);
temp=temp->next;
}
}
/* main program to help simulate queue operations */
int main()
{
struct qhead_struct *qhead;
int ch;
qhead = malloc(sizeof(struct qhead_struct));
qhead->head = NULL;
qhead->tail = NULL;
while(1) {
system("clear");
printf("1 Insert\n2 del\n3 display\n4 exit\n");
printf("Enter you choice\n");
scanf("%d", &ch);
switch(ch) {
case 1: insert(&qhead);
break;
case 2: del(&qhead);
break;
case 3: display(qhead);
break;
case 4: exit(0);
default: printf("Invalid choice\n");
}
getchar();
getchar();
}
{
struct qhead_struct *qhead;
int ch;
qhead = malloc(sizeof(struct qhead_struct));
qhead->head = NULL;
qhead->tail = NULL;
while(1) {
system("clear");
printf("1 Insert\n2 del\n3 display\n4 exit\n");
printf("Enter you choice\n");
scanf("%d", &ch);
switch(ch) {
case 1: insert(&qhead);
break;
case 2: del(&qhead);
break;
case 3: display(qhead);
break;
case 4: exit(0);
default: printf("Invalid choice\n");
}
getchar();
getchar();
}
return 0;
}
/* end of the program */
}
/* end of the program */
Comments
Post a Comment