Thursday, 1 September 2022

Stack and Queue operation in C language

IMPLEMENTATION OF STACK AND ITS OPERATION

Exercise.No:3 



TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : Code with u - YouTube

AIM: 

To implement the program for Push, Pop and Display operations in Stack. 

ALGORITHM: 

PUSH OPERATION: 

Step 1: If Top=Max-1 

Print “Overflow : Stack is full” and Exit 

End If 

Step 2: Top=Top+1 

Step 3: Stack[TOP]=Element 

Step 4: End 

POP OPEARTION: 

Step 1: If TOP=-1 

Print “Underflow: Stack is empty” and Exit 

End if 

Step 2: Set Del element=Stack[Top] 

Step 3: Top=Top-1 

Step 4: Del Element 

Step 5: End

NOTE : 

        In the below content incase of some error problem the program was slightly modified, so to remodel it the following steps should be taken 

            1.)In "in it" remove the space between "in" and "it".

            2.)In "print f" remove the space between "print" and "f".

            3.)In "scan f" remove the space between "scan" and "f".

            4.)In "std io. h" remove the space between "std" and "io" then space between "io." and "h".

            5.)In "con io. h" remove the space between "con" and "io" then space between "io." and "h".

PROGRAM :

#include<std io. h>

#include<con io. h>

#define MAX 15

typedef struct stack

{

int data[MAX];

int top;

}stack;

stack s;

void in it(stack*s);

void push(stack*s, int x);

void pop(stack*s);

int full(stack*s);

int empty(stack*s);

void print(stack*s);

int main()

{

int op,op1;

do

{

print f("\n\n1)Initialize\n2)Push\n3)Pop\n4)Full");

print f("\n5)Empty\n6)Print\n7)Quit");

print f("\n Enter your Choice:");

scan f("%d", &op);

switch(op)

{

case 1:init(&s);

break;

case 2:printf("\n Enter your element to push : ");

scan f("%d",&op1);

push(&s,op1);

break;

case 3:pop(&s);

break;

case 4:full(&s);

break;

case 5:empty(&s);

break;

case 6:print(&s);

break;

}

}while(op<7);

}

void in it(stack*s)

{

s->top=-1;

}

void push(stack*s, int x)

{

if(s->top==MAX-1)

{

print f("\n Stack is full");

}

else

{

s->top=s->top+1;

s->data[s->top]=x;

}

}

void pop(stack*s)

{

if(s->top==-1)

{

print f("\n Stack is empty");

}

else

{

if(s->top==0)

{

s->top=-1;

}

else

{

s->top=s->top-1;

}

}

}

int empty(stack*s)

{

if(s->top==-1)

{

print f("\n Stack is empty");

}

else

{

print f("\n Stack is not empty");

}

return(0);

}

int full(stack*s)

{

if(s->top==MAX-1)

{

print f("\n Stack is full");

}

else

{

print f("\n Stack is not full");

}

return(0);

}

void print(stack*s)

{

int a;

if(s->top==-1)

{

print f("Stack is empty");

}

else

{

for(a=s->top; a>=0;a--)

{

print f("\n %d", s->data[a]);

}

}

}

OUTPUT :

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:1

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:2

Enter your element to push : 12

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:2

Enter your element to push : 13

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:2

Enter your element to push : 14

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:6

14

13

12

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:3

1)Initialize

2)Push

3)Pop

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:6

13

12

RESULT: 

Thus the program for Push, Pop and Display operations in Stack was implemented and 

executed successfully.



IMPLEMENTATION OF QUEUE AND ITS OPERATION

Exercise.No:4 



TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : Code with u - YouTube

AIM: 

To implement the program for Queue and its operations. 

ALGORITHM: 

ENQUEUE OPERATION: 

Step 1 − Check if the queue is full. 

Step 2 − If the queue is full, produce overflow error and exit. 

Step 3 − If the queue is not full, increment rear pointer to point the next empty space. 

Step 4 − Add data element to the queue location, where the rear is pointing. 

Step 5 − return success. 

DEQUEUE OPERATION: 

Step 1 − Check if the queue is empty. 

Step 2 − If the queue is empty, produce underflow error and exit. 

Step 3 − If the queue is not empty, access the data where front is pointing. 

Step 4 − Increment front pointer to point to the next available data element. 

Step 5 − Return success.

NOTE : 

        In the below content incase of some error problem the program was slightly modified, so to remodel it the following steps should be taken 

            1.)In "in it" remove the space between "in" and "it".

            2.)In "print f" remove the space between "print" and "f".

            3.)In "scan f" remove the space between "scan" and "f".

            4.)In "std io. h" remove the space between "std" and "io" then space between "io." and "h".

            5.)In "con io. h" remove the space between "con" and "io" then space between "io." and "h".

PROGRAM :

#include<std io. h>

#include<con io. h>

#define MAX 15

typedef struct queue

{

int data[MAX];

int front, rear;

}queue;

queue s;

void in it(queue*s);

void enqueue(queue*s, int x);

void dequeue(queue*s);

int full(queue*s);

int empty(queue*s);

void print(queue*s);

int main()

{

int op,op1;

do

{

print f("\n\n1)Initialize\n2)Enqueue\n3)Dequeue\n4)Full\n5)Empty\n6)Print\n7)Quit ");

print f("\n Enter your Choice:");

scan f("%d", &op);

switch(op)

{

case 1:init(&s); break;

case 2:printf("\n Enter your element to add : ");

scan f("%d",&op1);

enqueue(&s,op1);

break;

case 3:dequeue(&s); break;

case 4:full(&s); break;

case 5:empty(&s); break;

case 6:print(&s); break;

}

}while(op<7);

}

void in it(queue*s)

{

s->rear=-1;

s->front=-1;

}

void enqueue(queue*s, int x)

{

if(s->rear==MAX-1)

{

print f("\nQueue is full");

}

else {

if(s->front==-1&&s->rear==-1)

{

s->front=s->front+1;

s->rear=s->rear+1;

s->data[s->rear]=x;

}

else {

s->rear=s->rear+1;

s->data[s->rear]=x;

}}}

void dequeue(queue*s)

{

if(s->front==-1)

{

print f("\nQueue is empty");

}

else {

if(s->front==s->rear)

{

s->front=-1;

s->rear=-1;

}

else

{

s->front=s->front+1;

}}}

int empty(queue*s)

{

if(s->front==-1&&s->rear==-1)

{

print f("\nQueue is empty");

}

else {

print f("\nQueue is not empty");

}

return(0);

}

int full(queue*s)

{

if(s->rear==MAX-1)

{

print f("\nQueue is full");

}

else {

print f("\nQueue is not full");

}

return(0);

}

void print(queue*s)

{

int a;

if(s->front==-1&&s->rear==-1)

{

print f("Queue is empty");

}

else {

for(a=s->front; a<=s->rear; a++)

{

print f(" %d", s->data[a]);

}

}

}

OUTPUT :

1)Initialize

2)Enqueue

3)Dequeue

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:1

1)Initialize

2)Enqueue

3)Dequeue

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:2

Enter your element to add : 45

1)Initialize

2)Enqueue

3)Dequeue

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:2

Enter your element to add : 46

1)Initialize

2)Enqueue

3)Dequeue

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:6

45 46

1)Initialize

2)Enqueue

3)Dequeue

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:3

1)Initialize

2)Enqueue

3)Dequeue

4)Full

5)Empty

6)Print

7)Quit

Enter your Choice:6

46

RESULT: 

Thus the program for Queue operations was implemented and it was executed successfully.


No comments:

Post a Comment

FRIENDSHIP & GOALS

WHAT IS FRIENDSHIP:  For all the attention we pay to love stories, some of the most compelling stories (in fiction or not) are about best fr...