0% found this document useful (0 votes)
17 views20 pages

Stack's and Queue's

Using data structures and algorithms

Uploaded by

oyezoroo
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)
17 views20 pages

Stack's and Queue's

Using data structures and algorithms

Uploaded by

oyezoroo
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/ 20

Write a program that implement stack (its operations) using

i) Arrays ii) Pointers


Def :
A Stack is a linear data structure that holds a linear, ordered sequence of elements. It is an abstract data
type. A Stack works on the LIFO process (Last In First Out), i.e., the element that was inserted last will be
removed first.

Here below is the program for the Stack implementation using arrays. The PUSH, POP and DISPLAY operations
are as follows.
#include<stdio.h>

#include<process.h>

#define SIZE 10

void push();

void pop();

void display();

int stack[SIZE], top = -1;

void main()

int choice;

while(1)

printf("\n\n***** STACK USING ARRAY MENU *****\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Display\n");
printf("4. Exit\n");

printf("\nEnter your choice: ");

scanf("%d",&choice);

switch(choice)

case 1: push();

break;

case 2: pop();

break;

case 3: display();

break;

case 4: exit(0);

default: printf("\nWrong selection!!! Try again!!!");

void push( )

int value;

if(top == SIZE-1)

printf("\nStack is Full!!! Insertion is not possible!!!");

else

printf("Enter the value to be insert: ");

scanf("%d",&value);

top++;

stack[top] = value;

printf("\nInsertion success!!!");

void pop()

{
if(top == -1)

printf("\nStack is Empty!!! Deletion is not possible!!!");

else

printf("\nDeleted : %d", stack[top]);

top--;

void display()

int i;

if(top == -1)

printf("\nStack is Empty!!!");

else

printf("\nStack elements are:\n");

for(i=top; i>=0; i--)

printf("%d\n",stack[i]);

Result -
ii ) Stack using Linked List ( Pointers )
#include<stdio.h>

#include<stdlib.h>

#include<process.h>

struct node

int data;

struct node *next;

};

struct node *top = NULL;

void push();

void pop();

void display();

void main()

int choice, value;

printf("\n:: Stack using Linked List ::\n");

while(1)

printf("\n\n***** MENU *****\n");

printf("1. Push\n");

printf("2. Pop\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("\nEnter your choice: ");

scanf("%d",&choice);

switch(choice)

case 1: push();

break;

case 2: pop();
break;

case 3: display();

break;

case 4: exit(0);

default: printf("\nWrong selection!!! Try again!!!");

void push()

struct node *ptr;

int value;

ptr = (struct node*)malloc(sizeof(struct node));

printf("Enter the value to be insert: ");

scanf("%d", &value);

ptr->data = value;

if(top == NULL)

ptr->next = NULL;

else

ptr->next = top;

top = ptr;

printf("\nInsertion is Success!!!\n");

void pop()

if(top == NULL)

printf("\nStack is Empty!!!Deletion is not possible!\n");

else

struct node *temp;

temp=top;

printf("\nDeleted element: %d", temp->data);


top = temp->next;

free(temp);

void display()

if(top == NULL)

printf("\nStack is Empty!!!\n");

else

struct node *temp;

temp=top;

while(temp->next != NULL)

printf("%d--->",temp->data);

temp = temp -> next;

printf("%d--->NULL",temp->data);

Result –
Program -2
A queue is linear data structure that consists of a collection is of items that follow a first-in-first-
out sequence. This implies that the first item to be inserted will be the first to be removed. You
can also say that items are removed in the order they were inserted.

Write a program that implement Queue (its operations) using

i)Arrays ii) Pointers

i) Program for implementation of Queues using Arrays -

#include<stdio.h>

#include<conio.h>

#include<process.h>

#define SIZE 10

void enQueue( );

void deQueue();

void display();

int queue[SIZE], front = -1, rear = -1;

void main()

int choice;

while(1)

printf("\n\n***** MENU *****\n");

printf("1. Insertion\n");

printf("2. Deletion\n");

printf("3. Display\n");

printf("4. Exit\n");

printf("\nEnter your choice: ");


scanf("%d",&choice);

switch(choice)

case 1: enQueue( );

break;

case 2: deQueue();

break;

case 3: display();

break;

case 4: exit(0);

default: printf("\nWrong selection!!! Try again!!!");

void enQueue()

int value;

if(rear == SIZE-1)

printf("\nQueue is Full!!! Insertion is not possible!!!");

else

if(front == -1)

front = 0;

else

printf("Enter the value to be insert: ");

scanf("%d",&value);

rear++;
queue[rear] = value;

printf("\nInsertion success!!!");

void deQueue()

if(rear== -1 || front > rear)

printf("\nQueue is Empty!!! Deletion is not possible!!!");

else

printf("\nDeleted : %d", queue[front]);

front++;

void display()

if(rear== -1 || front > rear)

printf("\nQueue is Empty!!!");

else

int i;

printf("\nQueue elements are:\n");

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

printf("%d\t",queue[i]);

}
}

Result –
ii ) Queue using Linked list ( or ) Pointers
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front = NULL,*rear = NULL;
void enQueue();
void deQueue();
void display();
void main()
{
int choice;
while(1)
{
printf("\n\n***** QUEUE USING LINKED LIST MENU *****\n");
printf("1. enQueue\n");
printf("2. deQueue\n"); printf("3. Display\n");
printf("4. Exit\n"); printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: enQueue();
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue()
{
struct node *ptr;
int value;
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter the value to be insert: ");
scanf("%d", &value);
ptr->data = value;
ptr -> next = NULL;
if(front == NULL)
{
front=ptr;
rear=ptr;
}
else
{

rear -> next = ptr;

rear = ptr;

printf("\nInsertion is Success!!!\n");

void deQueue()

if(front == NULL)

printf("\nQueue is Empty!!!Deletion is not possible!\n");


else

struct node *temp;

temp=front;

front = front -> next;

printf("\nDeleted element: %d\n", temp->data);

free(temp);

void display()

if(front == NULL)

printf("\nQueue is Empty!!!\n");

else

struct node *temp;

temp=front;

while(temp->next != NULL)

printf("%d--->",temp->data);

temp = temp -> next;

printf("%d--->NULL",temp->data);

Results –

You might also like