[go: up one dir, main page]

0% found this document useful (0 votes)
15 views28 pages

DSA 1-6 Exp

The document contains multiple C programming experiments demonstrating the implementation of data structures such as stacks and queues using both arrays and linked lists. It includes dynamic and static input methods for both stacks and queues, as well as algorithms for converting infix expressions to postfix and evaluating postfix expressions. Additionally, it features a circular queue implementation and showcases various stack operations.

Uploaded by

syedazarali
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)
15 views28 pages

DSA 1-6 Exp

The document contains multiple C programming experiments demonstrating the implementation of data structures such as stacks and queues using both arrays and linked lists. It includes dynamic and static input methods for both stacks and queues, as well as algorithms for converting infix expressions to postfix and evaluating postfix expressions. Additionally, it features a circular queue implementation and showcases various stack operations.

Uploaded by

syedazarali
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/ 28

(EXPERIMENT-1a)

AIM: Creating stack using array.


1.(a) stack using array with dynamic input
#include<stdio.h>

#include<stdlib.h>

struct stack{

int size;

int top;

int *s;

};

void create(struct stack *st){

printf("Enter size: ");

scanf("%d",&st->size);

st-> top = -1;

st->s = (int*)malloc(st->size*sizeof(int));

void display(struct stack st){

int i;

for(i= st.top;i >= 0; i--){

printf("%d ",st.s[i]);

void push(struct stack *st){

int i;

for(i = 0; i < st->size;i++){


int x;

printf("enter element: ");

scanf("%d",&x);

if(st->top == st->size -1){

printf("stack overflow can't store element: %d\n",x);

else{

st->top++;

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

int pop(struct stack *st){

int x = -1;

if(st -> top == -1){

printf("stack underflow\n");

else{

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

return x;

void spacecheck(struct stack *st){

if(st->top == -1){

printf("stack is empty\n");
}

else if(st->top == st->size-1){

printf("stack is full\n");

else{

printf("there are %d spaces available\n",(st->size- st->top -1));

int main(){

struct stack st;

create(&st);

push(&st);

printf("Elements in stack: ");

display(st);

printf("\n");

printf("deleted element is: %d\n",pop(&st));

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;

};

int create(struct stack *st)

printf("enter the size of stack you want to create\n");

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;

void push (struct stack *st,int x)

if (isFull(st))

printf("stack overflow\n");

else

st->top++;

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

}
int isEmpty(struct stack *st)

if (st->top==-1)

printf("now your stack is empty");

return -1;

else

return 0;

int pop (struct stack *st)

int x=-1;

if (isEmpty(st))

printf("stack underflow\n");

else

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

printf("the deleted element is %d\n",x);


return x;

void Display( struct stack st)

int i;

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

printf("remaining element on stack is %d\n",st.s[i]);

int main()

struct stack st;

create (&st);

push (&st,1);

push (&st,3);

pop (&st);

//printf("deleted element is %d\n",pop (&st));

//printf("deleted element is %d\n",pop (&st));

Display (st);

}
Output:

AIM: to create Queue using array.


2(a). Queue using array dynamic input
#include<stdio.h>

#include<stdlib.h>

struct queue

int size;

int front;

int rear;

int *Q;

};

create (struct queue *q){

int size;
printf("enter size: ");

scanf("%d",&size);

q -> size = size;

q -> front = q -> rear = -1;

q -> Q = (int*)malloc(q->size*sizeof(int));

void enqu (struct queue *q){

int i;

for(i = 0;i < q->size;i++){

int x;

printf("Enter element: ");

scanf("%d",&x);

if(q->rear == q ->size-1){

printf("Queue is full");

else{

q -> rear++;

q ->Q[q->rear] = x;

int dequ (struct queue *q){

int x = -1;

if(q -> front == q ->rear){

printf("Queue is empty");
}

else{

q -> front++;

x = q->Q[q->front];

return x;

void display(struct queue q){

int i;

printf("Elements in Queue:");

for(i=q.front + 1; i<= q.rear;i++){

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;

};

int create(struct queue *q,int size)

q->size=size;

q->front=q->rear=-1;

q->Q=(int *)malloc(q->size*sizeof(int));

void enqueue( struct queue *q,int X)

if (q->rear==q->size-1)

printf("Queue is full");

}
else

q->rear++;

q->Q[q->rear] =X;

int dequeue(struct queue *q)

int X=-1;

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

printf("Queue is empty");

else

q->front++;

X=q->Q[q->front];

return X;

void Display(struct queue q)

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>

#define MAX 100

char stack[MAX];

int top = -1;

void push(char c) {

stack[++top] = c;

char pop() {

return stack[top--];

char peek() {

return stack[top];

int precedence(char c) {

return (c == '+' || c == '-') ? 1 : (c == '*' || c == '/') ? 2 : (c == '^') ? 3 : 0;

void infixToPostfix(char *infix, char *postfix) {

int j = 0;
int i = 0;

for (i = 0; infix[i]; i++) {

if (isalnum(infix[i])) {

postfix[j++] = infix[i];

} else if (infix[i] == '(') {

push(infix[i]);

} else if (infix[i] == ')') {

while (top != -1 && peek() != '(') {

postfix[j++] = pop();

pop(); // pop '('

} else {

while (top != -1 && precedence(peek()) >= precedence(infix[i])) {

postfix[j++] = pop();

push(infix[i]);

while (top != -1) {

postfix[j++] = pop();

postfix[j] = '\0';

int main() {
char infix[MAX], postfix[MAX];

printf("Enter an infix expression: ");

scanf("%s", infix);

infixToPostfix(infix, postfix);

printf("Postfix expression: %s\n", postfix);

return 0;

Output:

// C program to evaluate value of a postfix expression

#include <ctype.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Stack type

struct Stack {

int top;

unsigned capacity;

int* array;

};
// Stack Operations

struct Stack* createStack(unsigned capacity)

struct Stack* stack

= (struct Stack*)malloc(sizeof(struct Stack));

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;

int isEmpty(struct Stack* stack)

return stack->top == -1;

}
char peek(struct Stack* stack)

return stack->array[stack->top];

char pop(struct Stack* stack)

if (!isEmpty(stack))

return stack->array[stack->top--];

return '$';

void push(struct Stack* stack, char op)

stack->array[++stack->top] = op;

// The main function that returns value

// of a given postfix expression

int evaluatePostfix(char* exp)

// Create a stack of capacity equal to expression size

struct Stack* stack = createStack(strlen(exp));

int i;
// See if stack was created successfully

if (!stack)

return -1;

// Scan all characters one by one

for (i = 0; exp[i]; ++i) {

// If the scanned character is an operand

// (number here), push it to the stack.

if (isdigit(exp[i]))

push(stack, exp[i] - '0');

// If the scanned character is an operator,

// pop two elements from stack apply the operator

else {

int val1 = pop(stack);

int val2 = pop(stack);

switch (exp[i]) {

case '+':

push(stack, val2 + val1);

break;

case '-':

push(stack, val2 - val1);

break;
case '*':

push(stack, val2 * val1);

break;

case '/':

push(stack, val2 / val1);

break;

return pop(stack);

// Driver code

int main()

char exp[50];

printf("enter postfix expression: ");

gets(exp);

// Function call

printf("postfix evaluation: %d", evaluatePostfix(exp));

return 0;

Output:
#include <stdio.h>

#include <stdlib.h>

#define MAX 5 // Maximum size of the queue

typedef struct {

int items[MAX];

int front;

int rear;

} CircularQueue;

// Function to create a circular queue

CircularQueue* createQueue() {

CircularQueue* q = (CircularQueue*)malloc(sizeof(CircularQueue));

q->front = -1;

q->rear = -1;

return q;

// Function to check if the queue is full

int isFull(CircularQueue* q) {
return (q->front == (q->rear + 1) % MAX);

// Function to check if the queue is empty

int isEmpty(CircularQueue* q) {

return (q->front == -1);

// Function to add an element to the queue

void enqueue(CircularQueue* q, int value) {

if (isFull(q)){

printf("Queue is full!\n");

return;

if (isEmpty(q)) {

q->front = 0; // Initialize front on first enqueue

q->rear = (q->rear + 1) % MAX; // Circular increment

q->items[q->rear] = value;

printf("Enqueued: %d\n", value);

// Function to remove an element from the queue

int dequeue(CircularQueue* q) {

if (isEmpty(q)) {
printf("Queue is empty!\n");

return -1; // Return -1 if queue is empty

int value = q->items[q->front];

if (q->front == q->rear) {

q->front = -1; // Queue becomes empty

q->rear = -1;

} else {

q->front = (q->front + 1) % MAX; // Circular increment

printf("Dequeued: %d\n", value);

return value;

// Function to display the queue

void display(CircularQueue* q) {

if (isEmpty(q)) {

printf("Queue is empty!\n");

return;

printf("Queue elements: ");

int i = q->front;

while (1) {

printf("%d ", q->items[i]);

if (i == q->rear) break;
i = (i + 1) % MAX; // Circular increment

printf("\n");

// Main function to demonstrate the circular queue

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 allocated memory

free(q);

return 0;

}
Output:

6. Impementation of stack using linked list


#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node *next;


} *top = NULL;

void push(int x) {

struct Node *t;

t = (struct Node *)malloc(sizeof(struct Node));

if (t == NULL) {

printf("Stack is full\n");

} else {

t->data = x;

t->next = top;

top = t;

int pop() {

int x = -1;

struct Node *t;

if (top == NULL) {

printf("Stack is empty\n");

} else {

t = top;

top = top->next;

x = t->data;

free(t);

}
return x;

void display() {

struct Node *p;

p = top;

while (p != NULL) {

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

p = p->next;

printf("\n");

int main() {

push(10);

push(20);

display();

printf("%d\n", pop());

display();

return 0;

Output:

You might also like