0% found this document useful (0 votes)
78 views21 pages

Ds File Extended

The program implements insert and delete operations on a queue represented using a linked list. It defines a node structure with data and link fields. Functions are declared to insert a node, delete a node, and display the queue. The main function initializes the front and rear pointers to NULL and uses a switch case to call the appropriate function based on user input, inserting or deleting nodes as needed and displaying the queue.

Uploaded by

Hardik Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views21 pages

Ds File Extended

The program implements insert and delete operations on a queue represented using a linked list. It defines a node structure with data and link fields. Functions are declared to insert a node, delete a node, and display the queue. The main function initializes the front and rear pointers to NULL and uses a switch case to call the appropriate function based on user input, inserting or deleting nodes as needed and displaying the queue.

Uploaded by

Hardik Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

20. Write a program In C language to Implement a Queue using circular llnked list.

#include<stdio.h>
#define max 3
int q[l0l,front-0,rcor=-l:
void main()

int ch;
void insert():
void delet();
void display();
clrscr();
printf("\nCircular Queue operations\n");
printf("1.insert\n2.delete\n3.display\n4.exit\n");
wh.ilw (1)

printf("Enter your
choice:"); scanf("%d",&ch);
switch (ch)

case 1: insert();
reolme

break;
case 2: delet(),
break:
case 3:display();
break;
case 4:exit();
default:printf("Invalid option\n");

void insert()

int x;
i f((front==OGGrCar - x-1)||(front>0&Grcar’-front-1))

piintf(”Aitcr element to bC inrnrt:");

rear—0; qtrcarl
—x;
l
‹l'-•

if((front=—0&&rear==-1)| l (rear!=front-1))
q[++rear]=x;

void delet()

int a;
i-((front==0)&&(rear==-1))

printf("Queue is underflow\n")
getch();
exit();

if(front==rear)

a=q[front);
rear=-1;
front=0;

. (front==max-1)
a=q(front)}
front 0.

void dlsplay()

i1 t i,j;
(front==0s rear==-l)

piintf(”Queue is underflow\
n"); getch():
exit():

if(front>rear)

for(i=0;i<=rear,i++)
printf("\t%d",qli));
for(j=front;j<=max-l;j++)
printf("\t%d",q{j]);
printf("\nrear is at %d\
n",q[rear]), printf("\nfront is at
%d\n",q[front]);

for(i=front;i<=rear,i++)

printf("\t%d“,q[il);

printf("\nrear is at %d\n",qtrear]);
printf("\nfront is at %d\
n“,qlfront]);
getch(),

21. Write a program In C language to Implement the push operation In STACK using arrays

# i11 cltl dCc s ttl i O.Ill

#1Iicltide<c iljO.1 >

//include<stdlib.li>

#defit e size 3
Stl”\ict stack (

int s[size);

int stfull() (
if(st.top >= size - 1)
retuni 1;
else
return 0;

void push(int item) {


st.top++;
st.s[st.top]' item;

int stempty() {
if(st.top == -1)
return 1;
else

return 0;
ilJt pop()

( iilt item;

itcn = st.s[st.toyj;
st.top--;

void display() {

if(stciupty())
print("\nStaclt 1s Einptyl");
else (
for (i = st.top; i >= 0; i--)
priiitf{"\n%d", st.s[i]);

int iiiain() (
int item, choice;
char ans;
st.top = -I ;

printf("\n\tIinplementation Of Stack");
do (

printf("\nMain Menu");
printf("\n1.Push \n2.Pop \n3.Display \n4.exit");
r‹m N
printf("\nEnter Your Choice"); mel
scanf("%d", &choice);

switch (choice) {
case 1:

printf("\nEnter The item to be pushed");


scant("%d", &item);
if(stfull())

priiitf("\nStack is Full!");
' (st*"'l°*â’())
)3l‘il1l("\riEiupty stacl‹! Underflosv !!");

print(”\iiTlie popped el cnaent is %d“, item);

break;
case 3:
display();
break;
ease 4:
exit(0);

printf("\iiDo You want To Continue?");


ans = getche();
} while (ans — 'Y' || ans == 'y');

realme
return 0;
22. Wr1te a program in C language to implement the pop operation in STACK USi0g 8Y Fa 'Y 5

* i I1Cl \\dc*std io.I

' ncllidc<col› ic.] J>

*ii cludc<stjlit›.I >

dc fine size 5
struct stack (

} st;

int stfull() (
if(st.top >= size - 1)
return I ;
else
retuni 0;

void push(int item) {

st.top+-r ;
st.s[st.top] = item;

int steirpty()
{ if(st.top == -
l)
return 1;
else
return 0;

int pop() (
int item;
item' st.s[st.t°p]
i lit i:

int main() I
int item, choice;
char airs;
st.top = - I ;

pi intf("\n\tImplernentation Of Stack");
do (
printf("\nMain Menu");
printf(”\n1.Push \n2.Pop \n3.Display \n4.exit”);
printf("\nEnter Your Choice");
scanf("%d", &clioice);
switch (choice) { realme
case I:
printf("\nEnter The item to be pushed");
scaiif("%d", &itein);
if(stfull())
printf("\nStack is Filll!");
if(stent Jity())
prilitf( \uLiiiJity stnck1 Un derfiosv 11");

break;
case 3:
display();
bicak;
case 4:
exit(0);

print("\nDo You want To Continue†");


ans = getclie();
} while (ans 'Y' || ans == 'y');

return 0;
fl4.Writ e a program in C language to implemen t the pop operation in STACK using

' u clucle< st‹llili.li>


* ' uel ut1c"fialloc.lii

x'oid I*usli(int. noble **);


’cial Display (mode **);

!8t $CI11Q()'(llotlC *);

t}’l›cdcF struct stnck (


int data;
struct stack *next;
} node;

node *top;
int data, iterri, choice;
char ans, ch;

clrscr();

top = NULL;

printf("\nStack Using Linked List : nn");


do {
printf("\n\n The main menu");
printf("\n 1.Push \n2.Pop \n3.Display \n4.Exit");
printf("\ii Enter Your Choice");
scanf("%d", &choice);

switch (choise) (
case l:
printf("\nEnter the data");
scalif("%d", &data);
Push(data, &top);
break;
case 2:
if(Sein pty(top))
printf("\nStack underfiow!");
else (
item = Pop(&top);
printf(”\nTlie popped node is%d", item);

break;
case 3:
Display(&top);
bi cnk;
cosc 4:
Jii’intf("\iiDo You ivoiil T’o Quil?(y/n)")'
cli = gctclic();
if(cli == 'y’)
exit(0);

pi intf(”\iiDo you want to coiitilulc†");

} while (ans - 'Y' || ans == 'y');


/C ICl1();

void Push(int Item, node **top) (


node *New;
node * get_iiode(int);
New = get_node(Itein);
New-bnext = *top;
*top = New;

node * get_iiode(tilt item) (


node * teiiip;
temp = (node *) inalloc(sizeof(node));
if(temp == NULL)
printf("\nMeinory Cannot be allocated");
temp-bdata = item;
temp- next = NULL;
return (temp);

ilit Sempty(node *temp)


( if(temp =— NULL)
return 1;
else
return 0;

int Pop(node **top)


{ int item;
node *temp;
itein = (*top)-
>data; temp = *top;
*top = (*top)->Hext;
free(temp);
return (item);

void Display(node **head) {


25. Write a program in C language to insert and delete the element in queue represente d using
arrays.

1 nC}mde<stdio.h>

int queue[MAX1, front —-1,rear=-


1; void insert_element();
void
delete_element();
void display queue();

int main()

int option;
printf(">>> c program to implement queue operations <<<"),
de

printf("\n\n 1.Insert an
element"); printf("\n 2.Delete an
element”); printf("\n 3.Display
queue"); printf{"\n 4.Exit");
printf("\n Enter your choice: ");
scanf("%d",&option);
-:‹itc:h(option)

casc 1: insert_element();
break;
case. 2: delete_eIement();
break;
case 3: display_queue();
break;
e 4: retuir 0;
)*Mni 1' (option!-4):

Void insert_elemeut()

int num;
plintf("\n Enter the number to inourtcd: "):
bc
s can I ( " ?, ” , r nun) .-
f(front---0 && rear—-MAX-1)
plintf("\n Queue Overflow Occurod”);
1.' (front= =- l &&rear =--1)

front=rear=0;

e1 if(rear==MAX-1 & front!=0)

reo r= 0 ;
queue { rear =num i

rear++;
queue re ar —num i

oid delete_element()

int element;
if(front==-1)

printf("\n Underflow");

element=queue frontJ ;
if(front==rear)
front=rear=-1;

if(front==MAX-1)
front=0:
else
front++;
printf("\n The deleted element is: %d",element);

display_queue()

int i;
f(front-=-1)
printf("\n loo elements to display”);

printf("\n The queue elements are:\n ");


26. Write a program in C language to insert and delete the element iC
queue represented using linked list

typedet struct nodc (


int data;
s t r u c t node *1ink;
) NO DE ;

void
Insert(int); int
Delete(); void
Display();
NODE *front, *rear; /* Global Declarations */

main() {
/* Main Program
*/ int opn, elem;
front - rear =
NULL; do {
clrscr();
printf("\n ### Linked List Implementation of QUEUE Operations #„
\n\n");
printf("\n Press 1-Insert, 2-Delete, 3-Display,4-Exit\n"):
printf("\n Your option ? “);
scanf("%d", &opn);
switch (opn)
{ case 1:
printf("\n\nRead the Element to be Inserted ?");
scanf("%d",
&elem);
Insert(elem);
break;
case 2:
elem = Delete();
lii;tf(”Linked List Implementation of Queue: Otatun:\n") :
Display();
Lleak;
case 4:
printf("\n\n Terminating \n\n");
Lreak;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n”);
break;

printf("\n\n\n\n Press a Key to Continue .


getch();
} while (opn != 4);

void Insert(int info)


{ NODE *temp;
temp = (NODE *)
malloc(sizeof(NODE)); if (temp ==
NULL)
printf(" Out of Memory !! Overflow !!!");
else (
temp->data = info;
temp->link = NULL;
if (front == NULL)
(
front = rear = temp;
) /* First Node? */
else (
rear->link = temp;
rear = t exp:
j / • I nse rt End * /
printf(" Node has been inserted at End Successfully !!");

int Delete()
{ int info;
NODE *t;
if (front == NULL)
( printf(" Underflow! !
!"); return -l;
} else {
t front;
info = front->data;
if (front == rear)
rear = NULL;
front = front->link;
t->link = NULL;
free(t);
return (info);

void Display()
( NODE *t;
if (front == NULL)
printf("Empty Queue\n");
else {
t front;
printf("Front->");
while lt) (
printf("[%d)->", t->data);
t - t->link;

printf("Rear\n");
reolme
27. Write a program in C language to implement the circular queue.

#define SIZE 5 /* Sizc of Circular Que°+ ’/


int CQ(sIZEl, f -l, r -1; /* G1OLa1 declara tions
’/

CQinsert(int elem) ( /* Function for insert operation ’/


if (CQfull())
printf("\n\n Overflow! ! !!\n\n"):
elSC (
if (f == -1)
f 0;
r = (r + 1) % SIZE;
CQ[r) = elem;

int CQdelete() ( /* Function for Delete operation */


int elem;
if (CQempty()) {
printf("\n\nUnderflow!!!!\n\n");
return (-1);
) else (
elem = CQ(f];
if (f == r) {

r -1;
} /* Q has only one element ? */
else
f = (f + 1) % SIZE;
return (elem);

int CQfull() ( /* Function to Check Circular Queue


Full */ if ((f == r + 1) || (f == 0 && r == SIZE -
1))
return 1;
return 0;
i t # e T@ t@() ( /* Function to Check Circular Que ue Empty ’/
if (f == -
return 1;
return 0;

display() { /* function to display status of Circular Queuo */


int i.
if (CQempty())
printf(" \n Empty Queue\n"):
else {
printf("Front{%d l ->", f);
for (i = f: i != r; i = (i + 1) %
SIZE) printf("%d ", CQ[i]);
printf("%d ", CQ[i1);
printf("<-(%d]Rear", r);

main() { /* Main Program


*/ int opn, elem;
do {
clrscr();
printf("\n ### Circular Queue Operations ### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n”);
printf{"\n Your option ? ");
scanf("%d", &opn);
switch (opn)
{ case 1:
printf("\n\nRead the element to be Inserted ?");
scanf("%d",
&elem);
CQinsert(elem);
break;
case 2:
elem =
CQdelete(); if
(elem != -1)
printf("\n\nDeleted Element is %d \n", elem);
break;
case
3:
printf("\n\nStatus of Circular Queue\n\n"),
break?
case 4:
pr i nt f("\n\n Terminating \n\n”),
g1eak•
default:
printf(” \n\nInvalid Option !!! Try Again !! \n\n”);
break;

printf("\n\n\n\n Press a Key to Continue .


getch();
} while (opn 4);
1
realme

You might also like