DSA 1-6 Exp
DSA 1-6 Exp
#include<stdlib.h>
struct stack{
int size;
int top;
int *s;
};
scanf("%d",&st->size);
st->s = (int*)malloc(st->size*sizeof(int));
int i;
printf("%d ",st.s[i]);
int i;
scanf("%d",&x);
else{
st->top++;
st->s[st->top] = x;
int x = -1;
printf("stack underflow\n");
else{
x = st->s[st->top--];
return x;
if(st->top == -1){
printf("stack is empty\n");
}
printf("stack is full\n");
else{
int main(){
create(&st);
push(&st);
display(st);
printf("\n");
pop(&st);
pop(&st);
spacecheck(&st); }
Output:
(EXPERIMENT-1b)
AIM : Implimentation of stack
1.(b) stack using array with static input
#include<stdio.h>
#include<stdlib.h>
struct stack
int size;
int top;
int *s;
};
scanf("%d",&st->size);
st->top=-1;
st->s=(int *)malloc(st->size*(sizeof(int)));
}
int isFull(struct stack *st)
if (st->top==st->size-1)
printf("stack is full\n");
return 1;
else
return 0;
if (isFull(st))
printf("stack overflow\n");
else
st->top++;
st->s[st->top]=x;
}
int isEmpty(struct stack *st)
if (st->top==-1)
return -1;
else
return 0;
int x=-1;
if (isEmpty(st))
printf("stack underflow\n");
else
x=st->s[st->top--];
int i;
for (i=st.top;i>=0;i--)
int main()
create (&st);
push (&st,1);
push (&st,3);
pop (&st);
Display (st);
}
Output:
#include<stdlib.h>
struct queue
int size;
int front;
int rear;
int *Q;
};
int size;
printf("enter size: ");
scanf("%d",&size);
q -> Q = (int*)malloc(q->size*sizeof(int));
int i;
int x;
scanf("%d",&x);
if(q->rear == q ->size-1){
printf("Queue is full");
else{
q -> rear++;
q ->Q[q->rear] = x;
int x = -1;
printf("Queue is empty");
}
else{
q -> front++;
x = q->Q[q->front];
return x;
int i;
printf("Elements in Queue:");
printf(" %d",q.Q[i]);
int main(){
struct queue q;
create(&q);
enqu(&q);
display(q);
dequ(&q);
Output :
(EXPERIMENT-1b)
2(b). Queue using array static input
#include<stdio.h>
#include<stdlib.h>
struct queue
int size,front,rear;
int *Q;
};
q->size=size;
q->front=q->rear=-1;
q->Q=(int *)malloc(q->size*sizeof(int));
if (q->rear==q->size-1)
printf("Queue is full");
}
else
q->rear++;
q->Q[q->rear] =X;
int X=-1;
if (q->front == q->rear)
printf("Queue is empty");
else
q->front++;
X=q->Q[q->front];
return X;
int i;
for (i=q.front+1;i<=q.rear;i++)
printf("%d\n",q.Q[i]);
int main()
struct queue q;
create (&q,1);
//enqueue (&q,10);
//enqueue (&q,545);
dequeue (&q);
Display (q);
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char stack[MAX];
void push(char c) {
stack[++top] = c;
char pop() {
return stack[top--];
char peek() {
return stack[top];
int precedence(char c) {
int j = 0;
int i = 0;
if (isalnum(infix[i])) {
postfix[j++] = infix[i];
push(infix[i]);
postfix[j++] = pop();
} else {
postfix[j++] = pop();
push(infix[i]);
postfix[j++] = pop();
postfix[j] = '\0';
int main() {
char infix[MAX], postfix[MAX];
scanf("%s", infix);
infixToPostfix(infix, postfix);
return 0;
Output:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Stack type
struct Stack {
int top;
unsigned capacity;
int* array;
};
// Stack Operations
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
stack->array
= (int*)malloc(stack->capacity * sizeof(int));
if (!stack->array)
return NULL;
return stack;
}
char peek(struct Stack* stack)
return stack->array[stack->top];
if (!isEmpty(stack))
return stack->array[stack->top--];
return '$';
stack->array[++stack->top] = op;
int i;
// See if stack was created successfully
if (!stack)
return -1;
if (isdigit(exp[i]))
else {
switch (exp[i]) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
return pop(stack);
// Driver code
int main()
char exp[50];
gets(exp);
// Function call
return 0;
Output:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int items[MAX];
int front;
int rear;
} CircularQueue;
CircularQueue* createQueue() {
CircularQueue* q = (CircularQueue*)malloc(sizeof(CircularQueue));
q->front = -1;
q->rear = -1;
return q;
int isFull(CircularQueue* q) {
return (q->front == (q->rear + 1) % MAX);
int isEmpty(CircularQueue* q) {
if (isFull(q)){
printf("Queue is full!\n");
return;
if (isEmpty(q)) {
q->items[q->rear] = value;
int dequeue(CircularQueue* q) {
if (isEmpty(q)) {
printf("Queue is empty!\n");
if (q->front == q->rear) {
q->rear = -1;
} else {
return value;
void display(CircularQueue* q) {
if (isEmpty(q)) {
printf("Queue is empty!\n");
return;
int i = q->front;
while (1) {
if (i == q->rear) break;
i = (i + 1) % MAX; // Circular increment
printf("\n");
int main() {
CircularQueue* q = createQueue();
enqueue(q, 10);
enqueue(q, 20);
enqueue(q, 30);
enqueue(q, 40);
enqueue(q, 50);
display(q);
dequeue(q);
dequeue(q);
display(q);
enqueue(q, 60);
enqueue(q, 70); // This should show "Queue is full!" since the queue is circular
display(q);
free(q);
return 0;
}
Output:
#include <stdlib.h>
struct Node {
int data;
void push(int x) {
if (t == NULL) {
printf("Stack is full\n");
} else {
t->data = x;
t->next = top;
top = t;
int pop() {
int x = -1;
if (top == NULL) {
printf("Stack is empty\n");
} else {
t = top;
top = top->next;
x = t->data;
free(t);
}
return x;
void display() {
p = top;
while (p != NULL) {
p = p->next;
printf("\n");
int main() {
push(10);
push(20);
display();
printf("%d\n", pop());
display();
return 0;
Output: