0% found this document useful (0 votes)
49 views6 pages

Notes of Ac N Ds

The document contains code for implementing a circular queue using an array in C. It defines the size of the queue, initializes the front and rear pointers, and includes functions to insert elements into the queue, delete elements from the queue, and display the queue's contents. The main function uses a do-while loop to repeatedly get a user choice to insert, delete or quit and calls the corresponding functions, displaying the queue after each operation.

Uploaded by

Sahil Jain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views6 pages

Notes of Ac N Ds

The document contains code for implementing a circular queue using an array in C. It defines the size of the queue, initializes the front and rear pointers, and includes functions to insert elements into the queue, delete elements from the queue, and display the queue's contents. The main function uses a do-while loop to repeatedly get a user choice to insert, delete or quit and calls the corresponding functions, displaying the queue after each operation.

Uploaded by

Sahil Jain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*PROGRAM TO INSERT ELEMENTS IN CIRCULAR QUEUE*/ #include<conio.h> #include<iostream.h> #include<stdlib.

h> #define MAX 5 void main() { int front=-1,rear=-1,i,a[MAX],item; char ch; clrscr(); do { if((front==0 && rear==MAX-1)||(front==rear+1)) { cout<<Queue is full<<endl; break; } else if(front==-1 && rear==-1) { rear=0; front=0; cout<<Enter the element you want to insert: ; cin>>item; a[rear]=item; } else if(rear==MAX-1 && front!=0) { rear=0; cout<<Enter the element you want to insert: ; cin>>item; a[rear]=item; } else { rear++; cout<<Enter the element you want to insert: ; cin>>item; a[rear]=item; } cout<<Do you want to insert more elements: ; cin>>ch; }while(ch==y'); cout<<Elements in the queue are: <<endl; for(i=front;i<=rear;i++) { cout<<a[i]<<\t; } getch(); }
/* INSERTION AND DELETION IN A CIRCULAR QUEUE ARRAY IMPLEMENTATION */ /* QUEUEID.C */

# # # #

include<stdio.h> include<string.h> include<ctype.h> include<stdlib.h>

# define size 6 int L, U; /* L means lower bound U means upper bound */

int rear, front; int ch; int q[size]; int rear = -1; int front = -1; void Insert_queue(); void Delete_queue(); void Display_queue(); /* Function to create queue */ void Insert_queue() { if ((front == 0) && (rear == size-1)) { printf("\n Overflow"); rear = 1; return; } else if (front < 0) /* Insert first element */ { front = 0; rear = 0; printf("\nInput the element :"); scanf("%d", &ch); q[rear] = ch ; } else if (rear == size-1) { rear = 0; printf("\n Input the element :"); scanf("%d", &ch); q[rear] = ch ; } else { rear ++; printf("\n Input the element :"); scanf("%d", &ch); q[rear] = ch ; } } /* Function to perform delete operation */ void Delete_queue() { if (front < 0) { printf("\nUnderflow"); return ; }

ch = q[front]; q[front] = NULL; printf("\n Element deleted :", ch); if(front == rear) { front = -1; rear = -1; } else if ( front == size-1) { front = 0; } else { front++ ; } } /* Output function */ void Display_queue() { int i; if (front < 0) return; if ( rear >= front) { for(i = front; i <= rear; i++) { printf("\n i = %d", i); printf(" %d ", q[i]); } } else { for(i = front; i < size; i++) { printf("\n i = %d", i); printf(" %d", q[i]); } for(i = 0; i <= rear; i++) { printf("\n i = %d", i); printf(" %d ", q[i]); } } } /* Function main */

void main() { int k = 0; char choice; do { printf("\nInsert->i Delete->d Quit->q:"); printf("\nInput the choice : "); do { choice = getchar(); choice = tolower(choice); }while(strchr("idq",choice)==NULL);

printf("Your choice is: %c",choice); switch(choice) { case 'i' : Insert_queue(); printf("\nQueue after inserting "); Display_queue(); break; case 'd' : Delete_queue(); printf("\nQueue content after deleteion is as follows:\n"); Display_queue(); break; case 'q': k = 1; } } while(!k); }

Code:

#include"stdio.h" #include"conio.h" #include"alloc.h" #include"stdlib.h" #define n 10 struct stack { int x[10]; int top; }; typedef struct stack stack; void push(stack *,int); void peek(stack *,int); void display(stack); void change(stack *,int,int); void pop(stack *); void main() { stack s; clrscr(); s.top=0; push(&s,2); push(&s,4); push(&s,6); push(&s,8); display(s);

peek(&s,1); change(&s,2,3); display(s); pop(&s); display(s); getch(); } void push(stack *s,int x) { if(s->top==(n-1)) {printf("overflow"); return; } s->top++; s->x[s->top]=x; } void display(stack s) { int i=1; printf("\n"); while(i<=s.top) {printf("%d",s.x[i]); i++;} } void pop(stack *s) { if((s->top)<0) { printf("underflow"); return;} s->x[s->top]=0; s->top--; } void peek(stack *s,int p) { if((s->top)-p<0) { printf(" underflow"); return;} printf("\n%d",s->x[p]); } /* change ith element */ void change(stack *s,int p,int x) { if((s->top)-p<0) { printf(" underflow"); return;} s->x[p]=x; }

You might also like