0% found this document useful (0 votes)
13 views

Assignment 2 Dsa

The document discusses implementing stack and queue data structures using linked lists and arrays respectively. It includes code to perform push, pop and display operations on a stack implemented using linked lists and enqueue, dequeue and display operations on a queue implemented using an array.

Uploaded by

bhait7265
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment 2 Dsa

The document discusses implementing stack and queue data structures using linked lists and arrays respectively. It includes code to perform push, pop and display operations on a stack implemented using linked lists and enqueue, dequeue and display operations on a queue implemented using an array.

Uploaded by

bhait7265
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Rahul Bameta MCA 60 B

QUE: Implementing stack using linked list

#include<stdio.h>
#include<stdlib.h>

typedef struct stack{


int info;
struct stack * next;
}node_type;

void push(node_type **top);


void pop(node_type **top);
void display(node_type **top);

int main(){
node_type *Top=NULL;
int ch1;
char ch;
printf("Enter your choice");
do
{
printf("\n1. Push \n2. Pop \n3. Display ");
scanf("%d", &ch1);
switch (ch1)
{
case 1:
Rahul Bameta MCA 60 B

push(&Top);
break;
case 2:
pop(&Top);
break;
case 3:
display(&Top);
break;
}
printf("Do you want to continue press y or n to exit ");
scanf(" %c", &ch);
switch (ch)
{
case 'y':
continue;
case 'n':
return 0;
}
} while (ch == 'y');
}

void push(node_type **tp){


node_type *p=(node_type*) malloc(sizeof(node_type));
if(p==NULL){
printf("Not enough memory to create dynamic memory ");
return;
Rahul Bameta MCA 60 B

}
int n;
printf("Enter the value to input ");
scanf("%d",&n);
p->info=n;
if(*tp==NULL){
p->next=NULL;
*tp=p;
}
else{
p->next=*tp;
*tp=p;
}
printf("The value %d is inserted \n",n);
}

void pop(node_type **tp){


if(*tp==NULL){
printf("The stack is empty\n");
return;
}
node_type *p =*tp;
printf("The popped element is %d \n",(*tp)->info);
(*tp)=(*tp)->next;
free(p);
}
Rahul Bameta MCA 60 B

void display (node_type **tp){


if(*tp==NULL){
printf("The stack is empty\n");
return;
}
node_type *p=*tp;
printf("element are ");
while(p!=NULL){
printf("%d ",p->info);
p=p->next;
}
printf("\n");
}
Rahul Bameta MCA 60 B
Rahul Bameta MCA 60 B
Rahul Bameta MCA 60 B

Que: Implementing Queue using array

#include<stdio.h>
#define MAX 3

void enqueue(int [],int* ,int* );


void dequeue(int [],int* ,int* );
void display(int [],int ,int);
int main(){
int arr[MAX],front=-1,rear=-1,ch1;
char ch;
printf("Enter your choice");
do{
printf("\n1. Push \n2. Pop \n3. Display ");
scanf("%d", &ch1);
switch (ch1) {
case 1:
enqueue(arr,&front,&rear);
break;
case 2:
dequeue(arr,&front,&rear);
break;
case 3:
display(arr,front,rear);
break;
}
Rahul Bameta MCA 60 B

printf("Do you want to continue press y or n to exit ");


scanf(" %c", &ch);
switch (ch) {
case 'y':
continue;
case 'n':
return 0;
}
} while (ch == 'y');
}

void enqueue(int arr[],int *front,int *rear){


int num;
if((*rear+1)%MAX==*front){
printf("The queue is full \n");
return ;
}
printf("Enter the number to inserted ");
scanf("%d",&num);
*rear=(*rear+1)%MAX;
arr[*rear]=num;
if(*front==-1){
*front=0;
}
}
Rahul Bameta MCA 60 B

void dequeue(int arr[], int* front, int* rear) {


if (*front == -1) {
printf("The queue is empty \n");
return;
} else {
printf("The popped element is %d \n", arr[*front]);
if (*front == *rear) {
*front = *rear = -1;
} else {
*front = (*front + 1) % MAX;
}
}
}
void display(int arr [],int front,int rear){
if(front==-1){
printf("The queue is empty \n");
return ;
}
printf("The elements are ");
while(front!=rear){
printf("%d ",arr[front]);
front=(front+1)%MAX;
}
printf("%d ",arr[front]);
printf("\n");
}
Rahul Bameta MCA 60 B

You might also like